![](/img/trans.png)
[英]How can I stub a chain of calls to an AWS client object using AWS SDK V2 tests?
[英]How do I create a presigned link to a particular version of an object using the Ruby AWS SDK v2?
我正在使用Ruby AWS SDK(v2)将日志文件上传到版本为S3的存储桶。 日志文件不是公开的,但是我想生成一个指向日志的预签名链接,以便通过聊天集成在有限的时间内可用。 我想链接到特定版本, 此答案表示可以通过S3控制台进行链接。
有关Aws::S3::Presigner
文档, Aws::S3::Presigner
了如何对未版本控制的对象(或版本控制的对象的Aws::S3::Presigner
执行此操作,但不针对特定版本执行此操作。 #presigned_url
的可能参数没有得到很好的记录,读取源代码看起来像参数只是传递给了不是S3特定的Seahorse::Client::Base#build_request
。
我想我终于解决了这个问题,尽管我仍然不确定是否可以跟踪整个代码路径。 简而言之:您可以将options参数中的:version_id
传递给presigned_url
。
#
# Uploads a log to S3 at the given key, returning a URL
# to the file that's good for one hour.
#
# @param [String] bucket
# @param [String] key
# @param [String] body of the log to be uploaded
# @param [Hash] options
# @return [String] public URL of uploaded log, valid for one hour
# @raise [Exception] if the S3 upload fails
#
def upload_log(bucket, key, body, options={})
# Upload log
result = AWS::S3.create_client.put_object(
options.merge(
bucket: bucket,
key: key,
body: body
)
)
# Get presigned URL that expires in one hour
options = {bucket: bucket, key: key, expires_in: 3600}
options[:version_id] = result[:version_id] unless result[:version_id].nil?
Aws::S3::Presigner.new.presigned_url(:get_object, options)
end
在这里,我可以追踪到一切的原因:
presigned_url
将其params
参数传递给@client.build_request
( presigner.rb#L48 )。
build_request
最终将这些参数推送到它返回的请求的request.context.params
上(记录在client / base_spec.rb#L91中 )。
从这里我的理解是模糊的。 我希望类似Aws :: Rest :: Request :: Builder的所有参数一起创建Endpoint
并且该操作的特定规则(我无法找到)允许将version_id
添加到查询字符串中。
无论如何,它都在工作。 感谢迈克尔的指导!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.