AWS :: S3 :: S3Object.url_for – 如何使用新的AWS SDK Gem执行此操作?

我一直在使用paperclip和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 gem,它打破了这个错误:

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

任何人都知道如何使用此方法来使用新的AWS-SDK gem?

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

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

最近我升级到适用于Ruby的AWS SDK 2的最新gem(aws-sdk-2.1.13),并且此SDK版本中的预签名url已更改。

得到它的方式:

 presigner = Aws::S3::Presigner.new presigner.presigned_url(:get_object, #method bucket: 'bucket-name', #name of the bucket key: "key-name", #key name expires_in: 7.days.to_i #time should be in seconds ).to_s 

您可以在此处找到更多信息: http : //docs.aws.amazon.com/sdkforruby/api/Aws/S3/Presigner.html

查看文档后 ,url_for是一个实例方法,而不是类方法。

要使用aws-sdk生成URL,您需要执行以下操作:

 bucket = AWS::S3::Bucket.new(attachment.bucket_name) s3object = AWS::S3::S3Object.new(bucket, attachment.path(style || attachment.default_style)) s3object.url_for(:read, :expires => expires_in) 

选项与您指定的选项略有不同。

选项哈希(选项):

:expires(Object) – 设置URL的到期时间; 在这段时间之后,如果使用URL,S3将返回错误。 这可以是整数(指定当前时间之后的秒数),字符串(使用Time#parse解析为日期),Time或DateTime对象。 此选项默认为当前时间后一小时。

:secure(String) – 是生成安全(HTTPS)URL还是纯HTTP URL。

:response_content_type(String) – 在返回的URL上执行HTTP GET时设置响应的Content-Type标头。

:response_content_language(String) – 在返回的URL上执行HTTP GET时设置响应的Content-Language标头。

:response_expires(String) – 在返回的URL上执行HTTP GET时设置响应的Expires标头。

:response_cache_control(String) – 在返回的URL上执行HTTP GET时设置响应的Cache-Control标头。

:response_content_disposition(String) – 在返回的URL上执行HTTP GET时设置响应的Content-Disposition标头。

:response_content_encoding(String) – 在返回的URL上执行HTTP GET时设置响应的Content-Encoding标头。

我最近也从aws-s3切换到了aws-sdk。 我用以下内容替换了所有url_for:

  def authenticated_url(style = nil, expires_in = 90.minutes) self.attachment.expiring_url(expires_in, (style || attachment.default_style)) end 

您可以在回形针问题线程中看到讨论: https : //github.com/thoughtbot/paperclip/issues/732