我创建了一款运行在Heroku上的Rails应用程序,使用的是回形针和S3。我已经设法通过网站将图片上传到我的S3存储桶中(我可以在亚马逊控制面板上看到它们显示在我的存储桶中)。
但是,当我添加一个图像标记,即<%= image_tag x.photo.url %>时,我得到以下html (这里省略了标记),没有显示任何图像!
img alt="Test_tree“src="http://s3.amazonaws.com/hiphotos/ads/photos/000/000/015/original/test_tree.jpg?1344661020”
请帮帮我!为什么我看不到图片,即使它们在桶里?
非常感谢,伙计们
发布于 2012-08-11 14:38:25
首先,你尝试在你的代码中使用的url是:
http://s3.amazonaws.com/hiphotos/ads/photos/000/000/015/original/test_tree.jpg当您在浏览器中访问该链接时,您将看到以下内容:
<message>
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
</Message>
<RequestId>810A6AE1D141304C</RequestId>
<Bucket>hiphotos</Bucket>
<HostId>
XXZ+s+slgZLsRWy5NiU/G0yAKBLftw0oT2dDKpas532qXJEPSrISVPqfZsEgpb2J
</HostId>
<Endpoint>hiphotos.s3.amazonaws.com</Endpoint>因此,如果我们使用正确的端点修改url,我们会得到如下结果:
http://hiphotos.s3.amazonaws.com/ads/photos/000/000/015/original/test_tree.jpg它确实返回正确的图像。
如果您使用的是欧洲存储桶,则可能会发生这种情况,这可能是您用来将内容推送到s3的gem的错误。
有大量的文章关于如何让回形针,S3和欧洲桶一起玩得很好。
但是我发现,自从我开始使用aws-S3gem,它使用的是Fog而不是asset_sync -S3gem,我在使用回形针和S3时就不再有任何问题了。
因此,我怀疑Fog与让我摆脱这个问题有关。如果你在使用其他东西,我建议你改用它。
发布于 2013-10-01 02:53:30
创建一个名为回形针initializer的文件:
# config/initializers/paperclip.rb
# We are actually setting this to 's3_domain_url',
# so it's not a placeholder for something else.
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'或者您也可以将此代码放入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']
},
:url =>':s3_domain_url',
:path => '/:class/:attachment/:id_partition/:style/:filename',
}https://stackoverflow.com/questions/11912201
复制相似问题