简体   繁体   English

AWS S3,Paperclip缺少必需:bucket选项

[英]AWS S3, Paperclip missing required :bucket option

I'm trying to use Paperclip and SWS S3 on Heroku to let users upload images. 我正在尝试在Heroku上使用Paperclip和SWS S3来让用户上传图像。

I do not have my credentials stored in a yml file. 我没有将凭据存储在yml文件中。 I've followed the instructions on the Heroku page: https://devcenter.heroku.com/articles/paperclip-s3 我按照Heroku页面上的说明操作: https//devcenter.heroku.com/articles/paperclip-s3

But, when I try to run my app and upload an image I'm getting this message: 但是,当我尝试运行我的应用程序并上传图像时,我收到此消息:

missing required :bucket option
Rails.root: /Users/scottsipiora/Sites/clycss

Application Trace | Framework Trace | Full Trace
app/controllers/instructors_controller.rb:63:in `block in update'
app/controllers/instructors_controller.rb:62:in `update'

The instructions don't mention anything about making a change in my controller. 说明中没有提到有关在我的控制器中进行更改的任何内容。 I have seen some examples telling me to put in something like: 我看到一些例子告诉我要加入类似的东西:

In my model I have the following code: 在我的模型中,我有以下代码:

class Instructor < ActiveRecord::Base
  attr_accessible :bio, :hometown, :name, :school, :sort_order, :started_sailing, :started_teaching, :photo
  has_attached_file :photo, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }
end

In my production.rb I have (obviously replacing my real credentials with mock credentials): 在我的production.rb中我(显然用模拟凭证替换了我的真实凭证):

  config.paperclip_defaults = {
    :storage => :s3,
    :s3_credentials => {
      :bucket => ENV['bucket name'],
      :access_key_id => ENV['key_id'],
      :secret_access_key => ENV['access_key']
    }
  }

I've also created separate buckets for Production and Dev so things are cleaner. 我还为Production和Dev创建了单独的存储桶,因此事情更清晰。

Any ideas? 有任何想法吗? I'm relatively new and this should be pretty easy. 我比较新,这应该很容易。

Thanks in advance. 提前致谢。

I think you may have made the same mistake I did. 我想你可能犯了同样的错误。 In your production.rb file, do not edit the text to add your specific S3 keys. 在您的production.rb文件中,不要编辑文本以添加特定的S3密钥。 Just copy-paste the text directly as is listed in the tutorial. 只需按照教程中列出的方式直接复制粘贴文本即可。

#production.rb
config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['AWS_BUCKET'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

Then, set the environmental variables AWS_BUCKET, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY as described by the author of the dev center article. 然后,按照开发人员中心文章的作者所述设置环境变量AWS_BUCKET,AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY。

Do heroku config to check your environment variables. heroku config来检查你的环境变量。

Normally they are all caps and have underbars instead of spaces. 通常它们都是帽子并且有下垫而不是空格。

If it is not set, you should set the environment variable with 如果未设置,则应使用设置环境变量

heroku config:add BUCKET_NAME=my_bucket_name

Update your code: 更新你的代码:

:bucket => ENV['BUCKET_NAME'],

Heroku reference Heroku参考

Scott, sorry if there was any confusion here. 斯科特,对不起,如果这里有任何混淆。

I am the author of the Dev Center article. 我是开发中心文章的作者。 As B Seven stated above the AWS Config Vars need to be set on the Heroku application. 正如上面提到的B Seven所述,需要在Heroku应用程序上设置AWS Config Vars。

Heroku has recently updated their documentation ( https://devcenter.heroku.com/articles/config-vars#example ) and set is preferred over add going forward. Heroku最近更新了他们的文档( https://devcenter.heroku.com/articles/config-vars#example ),并且set比首先add受欢迎。


$ heroku config:set AWS_BUCKET=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key

Make sure you restart your CLI of choice when setting values with environment variables. 在使用环境变量设置值时,请确保重新启动CLI。 Don't be like me and set up Paperclip and AWS correctly with ENV variables and then waste a bunch of time on Google only to fix the issue by quitting and reopening Terminal. 不要像我一样使用ENV变量正确设置Paperclip和AWS,然后在Google上浪费大量时间,以通过退出并重新打开终端来解决问题。 😞 😞

  • Insert into schema.rb table "instructors" this line 插入schema.rb表“教师”这一行
 create_table "instructors" do |t| ... t.string "bucket" end 
  • Add to controller @instructor[:bucket] = ENV['S3_BUCKET_NAME'] 添加到控制器@instructor[:bucket] = ENV['S3_BUCKET_NAME']
  • Run "rake db:setup" 运行“rake db:setup”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM