使用s3_direct_upload,paperclip和aws-sdk进行上传。从临时文件夹复制时失败

时间:2014-10-29 22:30:38

标签: ruby-on-rails ruby heroku paperclip aws-sdk

我有一个问题(或两个)使用s3_direct_upload,paperclip和aws-sdk gems上传到s3。

在我的本地开发环境中,上传时,我遇到错误"没有这样的密钥"来自aws-sdk宝石。

[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):

请帮忙!!非常感谢您阅读整篇文章,任何输入都非常有用。

1 个答案:

答案 0 :(得分:0)

我通过从sub_source变量sub_source = CGI.unescape(raw_source[bucket_length+1..-1])手动删除存储桶的字符长度来解决此问题。然后我在heroku中整理了环境变量,以便在生产中获得相同的结果。