AnA*_*ice 26 ruby-on-rails amazon-s3 paperclip ruby-on-rails-3
我一直在使用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
Run Code Online (Sandbox Code Playgroud)
新的回形针使用AWS-SDK gem,它会打破这个错误:
undefined method `url_for' for AWS::S3:Class
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何使用此方法来使用新的AWS-SDK gem?
Tre*_*owe 30
要使用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
Run Code Online (Sandbox Code Playgroud)
Vik*_*nov 14
最近我升级到适用于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
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到更多信息: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)
Run Code Online (Sandbox Code Playgroud)
这些选项与您指定的选项略有不同。
选项哈希(选项):
:expires(对象)—设置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(字符串)—在返回的URL上执行HTTP GET时,设置响应的Content-Disposition标头。
:response_content_encoding(字符串)—在返回的URL上执行HTTP GET时,设置响应的Content-Encoding标头。
归档时间: |
|
查看次数: |
14491 次 |
最近记录: |