【问题标题】:rails 4 paperclip upload to amazon s3 not workingrails 4回形针上传到亚马逊s3不起作用
【发布时间】:2023-03-12 01:12:01
【问题描述】:

我一直在关注 railstutorial.org 上的 rails 4 教程。我已经完成了大部分工作,该项目托管在 heroku 上,但现在想将图像上传添加到 Amazon S3。我已按照 heroku 网站上的指南进行操作,但无法将任何内容上传到 S3(欧洲)上的存储桶。

我使用的是回形针 3.5.2。

后模型

  has_attached_file :post_photo,
                    styles: {
                        thumb: '100x100>',
                        square: '200x200#',
                        medium: '300x300>'
                    },
                    :storage => :s3,
                    :s3_credentials => {
                        :access_key_id => ENV['S3_KEY'],
                        :secret_access_key => ENV['S3_SECRET'] },
                    :s3_protocol => "https",
                    :path => ":class/:id/:basename_:style.:extension",
                    :url => ':s3_eu_url',
                    :bucket => 'bucket_name' 

后控制器

def post_params
  params.require(:post).permit(:post_photo, :user_username, :title, :comment, :location, :user_id)
end

config/initializers/Paperclip.rb

Paperclip.interpolates(:s3_eu_url) { |attachment, style|
  "#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}

config/environment.rb

require 'aws/s3'
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"

config/environments/production.rb

 # config/environments/production.rb
  config.paperclip_defaults = {
      :storage => :s3,
      :s3_credentials => {
          :bucket => ENV['S3_BUCKET_NAME'],
          :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
      },
      :url => ':s3_eu_url',
      :path => ":class/:id/:basename_:style.:extension"
  }

【问题讨论】:

  • 你在使用 gem 'aws-sdk' 吗?

标签: ruby-on-rails heroku amazon-s3 ruby-on-rails-4 paperclip


【解决方案1】:

所以,我已经完成了这项工作(不是使用欧洲 S3,但这不重要)在 config/environments/production.rb 中没有任何内容——主要是因为我使用 ENV 变量来帮助控制我指向的存储桶、API 密钥等。

这是我的配置:

config/environments/production.rb:

只有标准配置——与回形针无关。

config/initializers/paperclip.rb:

Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_protocol] = 'http'
Paperclip::Attachment.default_options[:s3_credentials] =
  { :bucket => ENV['AWS_BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] }

对于上述内容,您需要添加:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'

那么,您的 Post 模型应该只需要具有以下内容:

has_attached_file :post_photo,
                    styles: {
                        thumb: '100x100>',
                        square: '200x200#',
                        medium: '300x300>'
                    }

这可能很明显,但也要确保您的 Gemfile 中加载了 aws-sdk gem。

如果您有任何问题,请告诉我。我已经设置了很多次,完全可以帮助解决问题。 :)

【讨论】:

  • 是的,我已将 aws-sdk 包含在我的 gemfile 中,我进行了更改,但它仍未上传到我的存储桶。此外,当我运行 heroku 日志时,我没有看到任何有关上传到存储桶的错误消息,但我可以看到该文件正在由应用程序处理。
  • 这是在生产中吗?还是本地环境?此外,可能值得将日志记录提高到 DEBUG 级别以查看可能发生的其他情况。
  • 抱歉回来晚了,一直没能看这个。这是我部署到heroku的时候所以我猜那是生产?我不确定如何为我的本地环境设置亚马逊凭据。我应该用 's3-eu-west-1.amazonaws.com' 替换 ':s3_domain_url' 吗?
猜你喜欢
  • 2014-08-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 2013-10-29
  • 2012-03-30
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多