首页 文章

Paperclip S3 - 可以上传图像但无法查看图像

提问于
浏览
16

我创建了一个在Heroku上运行的Rails应用程序,使用Paperclip和S3 . 我已经设法通过网站将图像上传到我的S3存储桶(我可以看到它们显示在亚马逊控制面板上的存储桶中) .

但是当我添加一个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”

请帮忙!为什么我不能看到图像,即使它们在桶中?

非常感谢你们

2 回答

  • 12

    首先,您尝试在代码中使用的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>
    

    因此,如果我们使用正确的 endpoints 修改url,我们得到:

    http://hiphotos.s3.amazonaws.com/ads/photos/000/000/015/original/test_tree.jpg
    

    哪个会返回正确的图像 .

    如果您正在使用欧洲桶,可能会发生这种情况,这可能是您用来将东西推送到s3的宝石的错误 .

    有大量关于如何让Paperclip,S3和欧洲水桶一起玩得很好的文章 .

    我发现,自从我开始使用asset_sync gem,它使用Fog而不是aws-s3 gem时,我对paperclip和S3没有任何问题 .

    所以我怀疑Fog与让我解决这个问题有关 . 如果您正在使用其他东西,我建议您切换到它 .

  • 18

    创建一个名为paperclip初始化程序的文件:

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

相关问题