简体   繁体   English

Paperclip和Amazon S3问题

[英]Paperclip and Amazon S3 Issue

I have a rails app running on Heroku. 我有一个在Heroku上运行的rails应用程序。 I am using paperclip for some simple image uploads for user avatars and some other things, I have S3 set as my backend and everything seems to be working fine except when trying to push to S3 I get the following error: 我正在使用回形针为用户头像和其他一些简单的图像上传,我将S3设置为我的后端,一切似乎都正常工作,除非尝试推送到S3时出现以下错误:

The AWS Access Key Id you provided does not exist in our records. 

Thinking I mis-pasted my access key and secret key, I tried again, still no luck. 我以为我错误地粘贴了我的访问密钥和密钥,我再次尝试,仍然没有运气。 Thinking maybe it was just a buggy key I deactivated it and generated a new one. 想想也许它只是一个有缺陷的钥匙我停用它并生成一个新的。 Still no luck. 仍然没有运气。

Now for both keys I have used the S3 browser app on OS X and have been able to connect to each and view my current buckets and add/delete buckets. 现在,对于这两个键,我在OS X上使用了S3浏览器应用程序,并且能够连接到每个键并查看我当前的存储桶并添加/删除存储桶。 Is there something I should be looking out for? 有什么我应该注意的吗? I have my application's S3 and paperclip setup like so 我有我的应用程序的S3和回形针设置就像这样

development:
  bucket: (unique name)
  access_key_id: ENV['S3_KEY']
  secret_access_key: ENV['S3_SECRET']

test:
  bucket: (unique name)
  access_key_id: ENV['S3_KEY']
  secret_access_key: ENV['S3_SECRET']

production:
  bucket: (unique_name)
  access_key_id: ENV['S3_KEY']
  secret_access_key: ENV['S3_SECRET']

has_attached_file :cover,
    :styles => {
      :thumb => "50x50"
    },
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => ":class/:id/:style/:filename"

EDIT NOTE: The ENV['S3_KEY'] and ENV['S3_SECRET'] are environment variables in heroku which i have tried even using my keys directly and it still doesn't work 编辑注意:ENV ['S3_KEY']和ENV ['S3_SECRET']是heroku中的环境变量,我甚至试过直接使用我的键,它仍然不起作用

Note: I just added the (unique name) bits, those aren't actually there--I have also verified bucket names, but I don't even think this is getting that far. 注意:我刚刚添加了(唯一名称)位,那些实际上并不存在 - 我还验证了存储桶名称,但我甚至认为这没有达到那么远。 I also have my heroku environment vars setup correctly and have them setup on dev 我也正确设置了我的heroku环境变量,并在dev上进行设置

You aren't setting a bucket. 你没有设置一个桶。 It's in your s3.yml file, but you aren't reading that value from your call to has_attached_file . 它位于你的s3.yml文件中,但是你没有从你对has_attached_file的调用中读取这个值。

Paperclip S3 docs: http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method Paperclip S3 docs: http//rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method

Also, pay attention to those people who are telling you not to use a s3.yml file with Heroku. 另外,请注意那些告诉您不要使用Heroku的s3.yml文件的人。 It's a waste and just added abstraction that buys you nothing. 这是一种浪费,只是添加了抽象,无需购买任何东西。 You already have your ENV set up with the values you need, so use them. 您已经使用所需的值设置了ENV,因此请使用它们。

I've done this before where I don't want to push an s3.yml file to Heroku, but I do want to use one for test and development. 我之前已经完成了这个,我不想将s3.yml文件推送到Heroku,但我确实想用一个来测试和开发。 In an initializer you can do something like this: 在初始化程序中,您可以执行以下操作:

# If an s3.yml file exists, use the key, secret key, and bucket values from there.
# Otherwise, pull them from the environment.
if File.exists?("#{Rails.root}/config/s3.yml")
  s3_config = YAML.load_file("#{Rails.root}/config/s3.yml")
  S3[:key] = s3_config[Rails.env]['key']
  S3[:secret] = s3_config[Rails.env]['secret']
  S3[:bucket] = s3_config[Rails.env]['bucket']
else
  S3[:key] = ENV['S3_KEY']
  S3[:secret] = ENV['S3_SECRET']
  S3[:bucket] = ENV['S3_BUCKET']
end

Then when you're setting up Paperclip in your model, you reference the value like this: 然后,当您在模型中设置Paperclip时,可以像这样引用值:

...
:s3_credentials => {
  :access_key_id => S3[:key],
  :secret_access_key => S3[:secret]
},
:bucket => S3[:bucket]

Obviously, this means that you do not want to have your s3.yml file in your git repository (which really, you shouldn't anyway). 显然,这意味着您不希望在您的git存储库中拥有您的s3.yml文件(实际上,您不应该这样做)。

I kept getting the same AWS::S3::InvalidAccessKeyId error, and had a very similar s3.yml file. 我一直得到相同的AWS::S3::InvalidAccessKeyId错误,并且有一个非常相似的s3.yml文件。 As x1a4 recommended, I used ERB in my yaml file and it worked. 正如x1a4推荐的那样,我在我的yaml文件中使用了ERB并且它有效。 Here's what it looks like now: 这是现在的样子:

# myapp/config/s3.yml

development: &DEFAULTS
  bucket: myapp_dev
  access_key_id: <%= ENV['S3_KEY'] %>
  secret_access_key: <%= ENV['S3_SECRET'] %>

test:
  <<: *DEFAULTS
  bucket: myapp_test

production:
  <<: *DEFAULTS
  bucket: myapp

staging:
  <<: *DEFAULTS
  bucket: myapp_staging

I guess this might a tad too indirect for some folks, but it seemed like the cleanest implementation to me. 我想这对某些人来说可能有点间接,但对我来说这似乎是最干净的实施。

Your s3 yaml file is actually using the strings ENV['S3_KEY'] and ENV['S3_SECRET'] as the auth info for s3. 你的s3 yaml文件实际上是使用字符串ENV['S3_KEY']ENV['S3_SECRET']作为s3的auth信息。 They are not being evaluated as ruby code. 它们没有被评估为ruby代码。

There are a couple of things at least that you can do outside of putting that actual info into the yaml file. 除了将实际信息放入yaml文件之外,至少还有一些事情可以做。 You can look into enabling ERB in your yaml configs or just not use a yaml file for your credentials at all, because you're always pulling from the environment in every one of your rails_envs, so the yaml file is just an extra layer of indirection in your case that is useless. 您可以在yaml配置中查看启用ERB,或者根本不使用yaml文件作为凭证,因为您总是从每个rails_env中的环境中提取,因此yaml文件只是一个额外的间接层在你的情况下是没用的。

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

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