应用程序
我正在编写一个需要从用户那里获取图片并将它们保存在 AWS S3 存储桶中的应用程序。
应用程序/模型/图片.rb
class Picture < ActiveRecord::Base
include Paperclip::Glue
belongs_to :user
scope :active_objects, -> { where(is_deleted: false)}
has_attached_file :image,
:styles => {:medium => "800>", :small => "480>", :thumb => "100>"},
:convert_options => {:medium =>'-quality 90', :small =>'-quality 80', :thumb => '-quality 50' },
:storage => :s3,
:url => ":s3_domain_url",
:path => 'pictures/:id/image/:style/:basename.:extension',
# :s3_permissions => :private,
:s3_region => 's3-ap-southeast-1.amazonaws.com',
:s3_endpoint => 's3-ap-southeast-1.amazonaws.com',
:s3_credentials => Proc.new { |a| a.instance.s3_credentials }
def s3_credentials
{:bucket => App.secrets.bucket_name, :access_key_id => App.secrets.aws_access_key_id,
:secret_access_key => App.secrets.aws_secret_access_key}
end
def url(style_name = :original, time = 30.minutes.to_i)
image.s3_permissions == :private ? (image.expiring_url(time, style_name)) : (image.url(style_name))
end
end
问题
如您所见,我已将 s3_permissions 设置为 true,并且为了访问这些图片,我使用了 expiring_url 方法。现在,我想公开这些图片,所以我评论了 s3_permissions。
上传新图片后,我可以在不使用expiring_url方法的情况下访问图片
>> Picture.last.url
并且响应中的 url 在浏览器选项卡中打开了一个图像,但仍然无法访问之前上传的图像(Picture.first)。当我尝试打开网址时,它给了我这个
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>6423906A78A3FDFF</RequestId>
<HostId>
fowfNEi6+mM265iGig+jhT1/ih2P7yhPzNegHiS9Q6NrP4mnGNKkXFDefLva85tjAQ0uNbenYew=
</HostId>
</Error>
此外,他们的 s3_permissions 以 public_read 的形式出现。
>> Picture.first.image.s3_permissions
A, [2018-05-01T01:18:05.171381 #94266] ANY -- : 2018-05-01 01:18:05 +0530 severity=DEBUG, Picture Load (0.7ms) SELECT "pictures".* FROM "pictures" ORDER BY "pictures"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> :public_read