heroku 回形针S3 -可以上传图像,但无法查看

lf5gs5x2  于 2023-02-16  发布在  其他
关注(0)|答案(2)|浏览(121)

我创建了一个运行在Heroku上的Rails应用程序,有纸夹和S3。我已经设法通过网站将图像上传到我的S3桶中(我可以看到它们显示在Amazon控制面板上的桶中)。
但是当我添加一个Image标签,即<%= 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"

为什么我看不到图像,即使它们在桶中?

jhdbpxl9

jhdbpxl91#

创建一个名为回形针初始化器的文件:

# 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',
}
gmol1639

gmol16392#

首先,您尝试在代码中使用的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的错误。
有大量的文章介绍如何让回形针,S3和欧洲桶一起玩得很好。
不过我发现,自从我开始使用asset_sync gem(它使用Fog而不是aws-s3 gem)以来,我在使用回形针和S3方面没有遇到任何麻烦。
所以我怀疑Fog在解决这个问题上有一定的作用。如果你在使用其他的东西,我建议你换用Fog。

相关问题