S3使用回形针直接上传到rails

时间:2016-01-04 12:03:51

标签: ruby-on-rails amazon-web-services amazon-s3 paperclip

我想直接在S3上传文件。但是找不到与此相关的明确教程。我找到了一个教程,但我不清楚

S3 direct Upload

我想要一些有关使用paperclip进行S3直接上传的帮助

1 个答案:

答案 0 :(得分:3)

如果您想在整个项目中使用s3,请按照以下步骤进行操作:

development.rb

等任何环境文件中添加以下代码
config.paperclip_defaults = {
  :storage => :s3,
  :path => "/resources/:class/:id_partition/:style/:id-:basename.:extension",
  :s3_credentials => {
    :bucket => ENV["AWS_BUCKET"],
    :access_key_id => ENV["AWS_ACCESS_KEY_ID"],
    :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
  }
}

如果您想将其与特定型号一起使用,请遵循以下代码:

has_attached_file :avatar,
        :styles => { :small => "45x45#" },
       :path => "/resources/:class/:attachment/:id/:style/:filename",
        :storage => :s3,
        :s3_host_name => ENV["S3_HOST_NAME"],
        :s3_credentials => {
          :bucket => ENV["AWS_BUCKET"],
          :access_key_id => ENV["AWS_ACCESS_KEY_ID"],
          :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
        }