AWS :: S3 :: S3Object.url_for - 如何使用新的AWS SDK宝石做到这一点?这一点、如何使用、AWS、S3Object

2023-09-11 23:38:54 作者:演员

我一直用这个永远和回形针和AWS-S3已经:

I've been using this forever with paperclip and aws-s3:

  def authenticated_url(style = nil, expires_in = 90.minutes)
      AWS::S3::S3Object.url_for(attachment.path(style || attachment.default_style), attachment.bucket_name, :expires_in => expires_in, :use_ssl => true)
  end

新曲别针使用AWS-SDK的宝石,打破这个给错误:

The new paperclip uses the AWS-SDK gem, which breaks this giving the error:

undefined method `url_for' for AWS::S3:Class

任何人都知道如何让这个方法与新的AWS-SDK宝石工作?

Anyone know how to get this method to work with the new AWS-SDK gem?

感谢

推荐答案

要使用AWS-SDK的宝石,你应该使用生成URL的AWS::S3Object#url_for方法。照片 您可以使用#s3_object回形针附件访问S3Object实例。下面的代码段应该解决您的问题。

To generate a url using the aws-sdk gem you should use the AWS::S3Object#url_for method. You can access the S3Object instance from a paperclip attachment using #s3_object. The snippet below should resolve your issue.

def authenticated_url(style = nil, expires_in = 90.minutes)
  attachment.s3_object(style).url_for(:read, :secure => true, :expires => expires_in).to_s
end