通过回形针通过Heroku将图像上传到S3

时间:2015-10-18 04:59:53

标签: ruby-on-rails heroku amazon-s3 paperclip grape-api

我想通过回形针通过Heroku将图像从iOS上传到S3。我已将此网站作为参考。 https://devcenter.heroku.com/articles/paperclip-s3

然而,它返回错误"状态代码:500,标题"。我不确定是什么问题。我期待着您的回复。

#terminal
$heroku config:set S3_BUCKET_NAME="kenja.jp"
$heroku config:set AWS_ACCESS_KEY_ID=my_access_key_id
$heroku config:set AWS_SECRET_ACCESS_KEY=my_secret_access_key

#app/models/user.rb
has_attached_file :photo, 
  :styles => { medium: "300x300>", thumb: "100x100>" },
  :storage => :s3,
  :bucket => 'kenja.jp',
  :s3_permissions => :public,
  :url => ":s3_domain_url",
  :path => "/assets/photos/:id/:style/:basename.:extension",
  :s3_host_name => "s3-ap-northeast-1.amazonaws.com",
  :s3_credentials => :"#{Rails.root}/config/s3.yml"


#config/environments/staging.rb
config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['kenja.jp'],
    :access_key_id => ENV['my_access_key_id'],
    :secret_access_key => ENV['my_secret_access_key']
  }
}


#config/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] ='/:class/:attachment/:id/:style.:extenstion'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-ap-northeast-1.amazonaws.com'


#app/apis/v1/users.rb
  params do  
    requires :id, type:Integer
    requires :icon, type:Rack::Multipart::UploadedFile
  end
  post '/post_picture' do
    photo_file = ActionDispatch::Http::UploadedFile.new(params[:icon])
    @user = User.find(params[:id])
    @user.update(photo: photo_file)
    @user.photo
  end

日志

我检查了heroku日志,然后说了以下内容。

2015-10-18T06:33:10.144859+00:00 app[web.1]:  app/apis/v1/users.rb:112:in `block (2 levels) in <class:Users>'
2015-10-18T06:33:10.144858+00:00 app[web.1]: NameError (uninitialized constant Paperclip::Storage::S3::AWS):
2015-10-18T06:33:10.128265+00:00 app[web.1]:  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 15]]

这个链接告诉我,我需要降级aws-sdk的版本。 NameError (uninitialized constant Paperclip::Storage::S3::AWS):

gem 'aws-sdk', '< 2.0'

1 个答案:

答案 0 :(得分:1)

这是您的错误:

  

NameError(未初始化的常量Paperclip :: Storage :: S3 :: AWS)

在线查看后,这主要是由aws-sdk v2 gem没有定义AWS模块引起的。简而言之,这意味着Paperclip无法调用将其数据保存到S3所需的模块。

解决方法是将您的aws-sdk版本降级为1

#Gemfile
gem 'aws-sdk', '< 2.0'

...但是,谁喜欢变通方法?

如果你pull from the Paperclip master branch

#Gemfile
gem "paperclip", git: "https://github.com/thoughtbot/paperclip.git", branch: "master"