我有一個問題(或兩個)使用s3_direct_upload,paperclip和aws-sdk gems上傳到s3。使用s3_direct_upload,paperclip和aws-sdk上傳。從臨時文件夾複製時失敗
在我的本地開發環境中,當上傳時,我遇到aws-sdk gem中的「No such Key」錯誤。
[AWS S3 404 0.115585 0 retries]
copy_object(:bucket_name=>"development_bucket",
:copy_source=>"/development_bucket/development_bucket/uploads/1414619713713-5w01e7m7a7-32cae45599f962288e52878b1fe562ae/jpgvpkMV1rAJb.jpg",
:key=>"discoveries/uploads/62/original/jpgvpkMV1rAJb.jpg",
:metadata_directive=>"COPY",
:storage_class=>"STANDARD")
AWS::S3::Errors::NoSuchKey No Such Key
Completed 500 Internal Server Error in 296ms
AWS::S3::Errors::NoSuchKey (No Such Key):
app/models/discovery.rb:12:in `copy_and_delete'
app/controllers/discoveries_controller.rb:38:in `block in create'
app/controllers/discoveries_controller.rb:32:in `create'
第一個問題來了,爲什麼是BUCKET_NAME在copy_source
值複製,以及如何修改呢?
參數轉儲:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HTEPpuBVQ1x2xvvuVsJXpvKAt+u2+y35PjfA2U/4rdU=",
"discovery"=>{"direct_upload_url"=>"https://s3.amazonaws.com/development_bucket/uploads%2F1414619713713-5w01e7m7a7-32cae45599f962288e52878b1fe562ae%2FjpgvpkMV1rAJb.jpg",
"upload_file_name"=>"jpgvpkMV1rAJb.jpg", "upload_file_size"=>"187584",
"upload_content_type"=>"image/jpeg",
"upload_file_path"=>"/development_bucket/uploads%2F1414619713713-5w01e7m7a7-32cae45599f962288e52878b1fe562ae%2FjpgvpkMV1rAJb.jpg"},
"commit"=>"Upload"}
爲什麼direct_upload_url
轉義的休息嗎?意思是爲什麼有%2F
而不是/
?
我發現模型是這樣的:
class Discovery < ActiveRecord::Base
belongs_to :caseload
has_attached_file :upload
def self.copy_and_delete(paperclip_file_path, raw_source)
s3 = AWS::S3.new #create new s3 object
destination = s3.buckets[ENV["S3_BUCKET"]].objects[paperclip_file_path]
sub_source = CGI.unescape(raw_source)
sub_source.slice!(0) # the upload_file_path ends up adding an extra "/" in the beginning. We've removed this.
source = s3.buckets[ENV["S3_BUCKET"]].objects["#{sub_source}"]
source.copy_to(destination) #copy_to is a method originating from the aws-sdk gem.
source.delete #delete temp file.
end
我的AWS-SDK初始化:
require 'aws-sdk'
# Rails.configuration.aws is used by AWS, Paperclip, and S3DirectUpload
Rails.configuration.aws = YAML.load(ERB.new(File.read("#{Rails.root}/config/aws.yml")).result)[Rails.env].symbolize_keys!
AWS.config(logger: Rails.logger)
AWS.config(Rails.configuration.aws)
我的回形針初始化:
Paperclip::Attachment.default_options.merge!(
url: ':s3_domain_url',
path: ':class/:attachment/:id/:style/:filename',
storage: :s3,
s3_credentials: Rails.configuration.aws,
s3_permissions: :public_read,
s3_protocol: 'https'
)
我s3_direct_upload初始化:
S3DirectUpload.config do |c|
c.access_key_id = Rails.configuration.aws[:access_key_id]
c.secret_access_key = Rails.configuration.aws[:secret_access_key]
c.bucket = Rails.configuration.aws[:bucket]
c.region = "s3"
end
我爲發現控制新觀點:
<%= s3_uploader_form callback_url: discoveries_url,
id: "s3_uploader",
acl: "public-read",
callback_param: "direct_upload_url",
max_file_size: 40.megabytes do %>
<%= file_field_tag :file, multiple: true %>
<% end %>
<br/>
<div id="uploads_container"></div>
<script id="template-upload" type="text/x-tmpl">
<div id="upload_{%=o.unique_id%}" class="upload">
<h5>{%=o.name%}</h5>
<div class="progress progress-striped active"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
<br/>
<%= form_for(@discovery) do |f| %>
<% if @discovery.errors.any? %>
<ul>
<% @discovery.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.hidden_field :direct_upload_url %>
<%= f.hidden_field :upload_file_name %>
<%= f.hidden_field :upload_file_size %>
<%= f.hidden_field :upload_content_type %>
<%= f.hidden_field :upload_file_path %>
<%= f.submit "Upload" %>
<br/>
<% end %>
然後,當上傳到Heroku的,copy_source
不重複,但我仍然需要編輯的條目,因此copy_source
有鑰匙以及。
[AWS S3 400 0.105105 0 retries]
copy_object(:bucket_name=>"production_bucket",
:copy_source=>"/production_bucket/",
:key=>"discoveries/uploads/9/original/",
:metadata_directive=>"COPY",
:storage_class=>"STANDARD")
AWS::S3::Errors::InvalidArgument Copy Source must mention the source bucket and key: sourcebucket/sourcekey
Completed 500 Internal Server Error in 711ms
AWS::S3::Errors::InvalidArgument (Copy Source must mention the source bucket and key: sourcebucket/sourcekey):
請幫忙!!非常感謝您閱讀整篇文章,任何意見都非常有幫助。