简体   繁体   English

如何设置Amazon S3,paperclip和ENV变量

[英]How to set up Amazon S3, paperclip, and ENV variables

I have tried many different ways to set up S3 using ENV variables for image uploads and cannot get it to work. 我已经尝试了许多不同的方法来使用ENV变量设置S3以进行图像上传,并且无法使其工作。 I know my keys and bucket name work, because when I put them straight into the code, my images upload correctly. 我知道我的密钥和存储桶名称有用,因为当我将它们直接放入代码时,我的图像正确上传。 However, when I try to switch to ENV variables, things do not work. 但是,当我尝试切换到ENV变量时,事情不起作用。

I used the figaro gem, which created application.yml. 我使用了figaro gem,它创建了application.yml。 In that file, I have: 在那个文件中,我有:

S3_BUCKET_NAME "xxxxx"
AWS_ACCESS_KEY_ID: "AAAAAAAAA"
AWS_SECRET_ACCESS_KEY: "BBBbbbBBBB"

Not sure if there should be any quotation marks there or not, but right now, I have them in. I have tried without, also. 不确定是否应该有引号,但是现在,我已将它们放进去了。我也没试过。

In my model (listing.rb), I have: 在我的模型(listing.rb)中,我有:

has_attached_file :image, 
  :styles => { :medium => "200x" , :thumb => "100x100" }, 
  :default_url => "default.png",
  :storage => :s3,
  :s3_credentials => Proc.new{|a| a.instance.s3_credentials }

def s3_credentials
  {:bucket => ENV["S3_BUCKET_NAME"], :access_key_id => ENV["AWS_ACCESS_KEY_ID"], 
  :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
end

Like I said, when I hard code the values into def s3_credentials, everything works fine. 就像我说的,当我将值硬编码到def s3_credentials时,一切正常。 It's just when I try to swap out ENV variables that things fall apart. 就在我尝试换掉ENV变量时,事情就会崩溃。

In paperclip.rb, I have: 在paperclip.rb中,我有:

Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'

I also have this code in both production.rb and development.rb: 我在production.rb和development.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']
 }
}

Here is the error message I get when uploading a new image: "The request signature we calculated does not match the signature you provided. Check your key and signing method." 以下是上传新图片时收到的错误消息:“我们计算的请求签名与您提供的签名不符。请检查您的密钥和签名方法。” On line: "if @listing.save". 在线:“if @ listing.save”。 The ones that were uploaded w/ the credentials hard-coded are still able to be seen in my app. 使用硬编码的凭据上传的那些仍然可以在我的应用程序中看到。

I'm fairly new to rails and have looked here and other places, including the S3 and paperclip docs, and cannot find a solution that will work. 我对rails很新,看过这里和其他地方,包括S3和回形针文档,找不到可行的解决方案。 Please let me know if you need to see any other code. 如果您需要查看任何其他代码,请与我们联系。 I plan on deploying to heroku, if that matters, and saw that figaro is supposed to play nicely with heroku. 我计划部署到heroku,如果这很重要,并且看到figaro应该与heroku很好地配合。 THANK YOU. 谢谢。


EDIT/UPDATE: For anyone else reading this in the future, Sachin's answer below worked. 编辑/更新:对于将来阅读此内容的其他人,Sachin的答案在下面有效。 However, there was a '+' in one of my key IDs. 但是,我的一个密钥ID中有一个“+”。 When trying to added the ENV variable via command line, all the characters after the '+' (and including it) were cut off. 当试图通过命令行添加ENV变量时,“+”(包括它)之后的所有字符都被切断了。 Simply wrap them in "", and you should be good to go. 只需将它们包裹在“”中,你就应该好好去。

Also, I dropped use of the figaro gem, and set up an aws.rb initializer file (per Amazon's instructions). 此外,我放弃了使用figaro gem,并设置了aws.rb初始化文件(根据亚马逊的说明)。 Here are the contents of that file: 以下是该文件的内容:

AWS.config(
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)

S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]

And I don't know if this made any difference, but my development.rb and production.rb files now have the following as paperclip defaults: 我不知道这是否有任何区别,但我的development.rb和production.rb文件现在具有以下作为回形针默认值:

config.paperclip_defaults = {
 :storage => :s3,
 :bucket => "your_real_bucket_name_here_in_quotes",
 :s3_credentials => {
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
 }
}

Also note the switch to referring to the ENV bucket name as S3_BUCKET vs. S3_BUCKET_NAME. 另请注意,切换到将ENV桶名称称为S3_BUCKET与S3_BUCKET_NAME。

And the code in my model (listing.rb) is now this: 我的模型(listing.rb)中的代码现在是这样的:

has_attached_file :image, :styles => { :medium => "200x", :thumb  "100x100"}, :default_url => "default.png", :storage => :s3, :bucket => "your_real_bucket_name_here_in_quotes"

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] 

You can do one thing: 你可以做一件事:

You can set this configuration in your development.rb or production.rb 您可以在development.rbproduction.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']
  }
}

And If you want to set this environment variables into local then use this: 如果要将此环境变量设置为local,请使用以下命令:

sudo nano ~/.profile

Then add your variables over here 然后在这里添加变量

export S3_BUCKET_NAME="your bucket name"
export AWS_ACCESS_KEY_ID="your access key id"
export AWS_SECRET_ACCESS_KEY="your secret access key"

And then reload your ~/.profile with . ~/.profile 然后重新加载你的〜/ .profile . ~/.profile . ~/.profile

Check added variable with echo $S3_BUCKET_NAME 使用echo $S3_BUCKET_NAME检查添加的变量

And for Heroku 而对于Heroku

You can set your variable like: 您可以将变量设置为:

heroku config:set S3_BUCKET_NAME="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"

Check that variables added or not in heroku with heroku config 使用heroku config检查heroku中是否添加了变量

For more detail you can refer form here. 有关详细信息,请参阅此处的表格

Let me know if you need me more.. 如果您需要我更多,请告诉我..

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

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