我使用这段代码创建一个经过身份验证的Amazon Urlhttp://developer.longtailvideo.com/trac/browser/testing/files/s3-generator.php
是否有可能以某种方式让Amazon知道该文件需要通过添加Content-Disposition头来强制下载?
在收集了这个谜题的各个部分之后,我能够创建这个Ruby方法,该方法使用aws秘密密钥正确地签署查询字符串url。
My resources for this:
- <
- RESTObjectGET文档/gh>
- 签名和认证REST请求
同样,从S3返回的响应也很有帮助,因为当我创建一个带有错误签名的url时,响应显示了AWS S3通过解密我生成的url生成的string_to_sign。经过几次迭代,我能够收敛于string_to_sign的正确格式,之后它是相当标准的东西。
下面是Ruby的方法:##############################################################################
# Create a signed query-string URL that supports setting response headers
##############################################################################
def s3_signed_url(bucket, pathname, verb, content_md5=nil, content_type=nil, response_headers = {})
expires = Time.now + 5.minutes
response_headers_canonicalized = response_headers.sort_by{|key, value| key.downcase}.collect{|key, value| "#{key}=#{value}"}.join("&").to_s
string_to_sign = "#{verb.upcase}n#{content_md5}n#{content_type}n#{expires.to_i}n/#{bucket}/#{pathname}?#{response_headers_canonicalized}"
digest = OpenSSL::Digest::Digest.new('sha1')
hmac = OpenSSL::HMAC.digest(digest, aws_secret_key, string_to_sign)
signature = Base64.encode64(hmac).chomp
url = "http://s3.amazonaws.com/#{bucket}/#{pathname}?"
if response_headers.count > 0
response_headers.each do |key, value|
url += "#{key}=#{value}&"
end
end
url += "AWSAccessKeyId=#{aws_access_key}&Expires=#{expires.to_i}&Signature=#{CGI.escape(signature)}";
return url
end
你像这样调用这个方法:
file_url_s3 = s3_signed_url(file_bucket, file_path, 'GET', nil, nil, {'response-content-disposition' => 'attachment'})
当前版本的PHP和Ruby的AWS SDK都有方法参数,允许您在生成签名链接时指定内容处置头。
PHP:- PHP: get_object_url -
response - array - Optional -允许调整特定的响应头。传递一个关联数组,其中每个键是以下其中一个:缓存控制、内容处理、内容编码、内容语言、内容类型、过期。
- Ruby: S3Object url_for instance method
Options hash::response_content_disposition (String) -当对返回的URL执行HTTP GET时,设置响应的Content-Disposition报头。