如何设置Amazon S3的,曲别针,和ENV变量曲别针、变量、如何设置、Amazon

由网友(笗菇頭)分享简介:我已经尝试了许多不同的方法来设置S3使用ENV变量图片上传并不能得到它的工作。我知道我的钥匙和水桶名的工作,因为当我把它们直接进入code,我的图片正确上传。然而,当我尝试切换到ENV变量,事情不工作。我用费加罗的宝石,创造application.yml。在该文件中,我有:S3_BUCKET_NAMEXXXXXAWS...

我已经尝试了许多不同的方法来设置S3使用ENV变量图片上传并不能得到它的工作。我知道我的钥匙和水桶名的工作,因为当我把它们直接进入code,我的图片正确上传。然而,当我尝试切换到ENV变量,事情不工作。

我用费加罗的宝石,创造application.yml。在该文件中,我有:

  S3_BUCKET_NAMEXXXXX
AWS_ACCESS_KEY_ID:AAAAAAAAA
AWS_SECRET_ACCESS_KEY:BBBbbbBBBB
 

不知道是否应该有任何引号有或没有,但现在,我有他们在,我都试过没有,也。

在我的模型(listing.rb),我有:

  has_​​attached_file:图像,
  :风格=> {:中等=> 200X,:拇指=&​​GT; 100×100},
  :default_url => 为Default.png,
  :存储=> :S3,
  :s3_credentials => Proc.new {| A | a.instance.s3_credentials}

高清s3_credentials
  {:斗=> ENV [S3_BUCKET_NAME]:access_key_id => ENV [AWS_ACCESS_KEY_ID],
  :secret_access_key => ENV [AWS_SECRET_ACCESS_KEY]
结束
 
如何使用 Amazon S3 多区域访问点提高多区域应用程序的性能速度和可用性

就像我说的,当时我很难code的值到高清s3_credentials,一切工作正常。这只是当我试图换出ENV变量,事情土崩瓦解。

在paperclip.rb,我有:

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

我也有这个code两production.rb和development.rb:

  config.paperclip_defaults = {
 :存储=> :S3,
 :s3_credentials => {
 :斗=> ENV ['S3_BUCKET_NAME'],
 :access_key_id => ENV ['AWS_ACCESS_KEY_ID'],
 :secret_access_key => ENV ['AWS_SECRET_ACCESS_KEY']
 }
}
 

下面是我上传了新的图像时出现错误信息:我们计算请求签名不您提供的签名匹配检查你的密钥和签名方法。在行:如果@ listing.save。上载的瓦特/凭证硬codeD上的人仍然可以在我的应用程序,以待观察。

我是相当新的轨道,并在这里已经显得和其他地方,包括S3和回形针文档,并不能找到,将工作的解决方案。请让我知道如果你需要看到任何其他code。我打算部署到Heroku上,如果该事项,并看到费加罗应该与Heroku的发挥很好。谢谢。

编辑/更新:对于任何人读这在未来,下面萨钦的答复工作。然而,在我的钥匙ID之一是+。当通过命令行试图添加的ENV变量后的+(并包括它)的所有字符被切断。简单地包装在,你应该是好去。

另外,我放弃了使用费加罗的宝石,并设立aws.rb初始化文件(每亚马逊的说明)。下面是该文件的内容:

  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']]
 

我不知道这是否作出任何区别,但我development.rb和production.rb文件现在有以下的回形针默认值:

  config.paperclip_defaults = {
 :存储=> :S3,
 :斗=> 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']
 }
}
 

另外,请注意开关指ENV桶名称S3_BUCKET与S3_BUCKET_NAME。

而code在我的模型(listing.rb)现在是这样的:

  has_​​attached_file:图像:风格=> {:中等=> 200X,:拇指100×100}:default_url => 为Default.png:存储=> :S3,:斗=> your_real_bucket_name_here_in_quotes

validates_attachment_content_type:图像:CONTENT_TYPE => [图像/ JPG,图像/ JPEG,图像/ PNG,图像/ GIF]
 

解决方案

您可以做一件事:

您可在此配置你的 development.rb production.rb

  config.paperclip_defaults = {
  :存储=> :S3,
  :s3_credentials => {
    :斗=> ENV ['S3_BUCKET_NAME'],
    :access_key_id => ENV ['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV ['AWS_SECRET_ACCESS_KEY']
  }
}
 

如果你想要设置这个环境变量到本地,然后用这样的:

 须藤纳米〜/ .profile文件
 

然后在这里添加您的变量

 出口S3_BUCKET_NAME =你的水桶名
出口AWS_ACCESS_KEY_ID =您的访问密钥ID
出口AWS_SECRET_ACCESS_KEY =你的秘密访问键
 

然后重新装入你的〜/ .profile文件与。 〜/ .profile文件

检查添加变量,回声$ S3_BUCKET_NAME

和为的Heroku

您可以将您的变量,如:

  Heroku的配置:设置S3_BUCKET_NAME =你的水桶名
Heroku的配置:设置AWS_ACCESS_KEY_ID =您的访问密钥ID
Heroku的配置:设置AWS_SECRET_ACCESS_KEY =你的秘密访问键
 

检查添加的变量或不Heroku的与 Heroku的配置

有关详细可以参考表格这里

让我知道,如果你需要我更多......

I have tried many different ways to set up S3 using ENV variables for image uploads and cannot get it to work. 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.

I used the figaro gem, which created 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:

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. It's just when I try to swap out ENV variables that things fall apart.

In paperclip.rb, I have:

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:

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". 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. 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. THANK YOU.

EDIT/UPDATE: For anyone else reading this in the future, Sachin's answer below worked. However, there was a '+' in one of my key IDs. When trying to added the ENV variable via command line, all the characters after the '+' (and including it) were cut off. 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). 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:

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.

And the code in my model (listing.rb) is now this:

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

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:

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

Check added variable with echo $S3_BUCKET_NAME

And for 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

For more detail you can refer form here.

Let me know if you need me more..

阅读全文

相关推荐

最新文章