[英]How do I create a presigned link to a particular version of an object using the Ruby AWS SDK v2?
I am using the Ruby AWS SDK (v2) to upload log files to a versioned S3 bucket. 我正在使用Ruby AWS SDK(v2)将日志文件上传到版本为S3的存储桶。 The log files are not public, but I would like to generate a presigned link to the log to make it available for a limited time via a chat integration.
日志文件不是公开的,但是我想生成一个指向日志的预签名链接,以便通过聊天集成在有限的时间内可用。 I want to link to a particular version, which this answer says is possible via the S3 console.
我想链接到特定版本, 此答案表示可以通过S3控制台进行链接。
Documentation on Aws::S3::Presigner
shows how to do this for an unversioned object (or the head version of a versioned object) but not for a particular version. 有关
Aws::S3::Presigner
文档, Aws::S3::Presigner
了如何对未版本控制的对象(或版本控制的对象的Aws::S3::Presigner
执行此操作,但不针对特定版本执行此操作。 The possible parameters to #presigned_url
are not well documented, and reading the source it looks like the parameters are just passed to Seahorse::Client::Base#build_request
which is not S3-specific. #presigned_url
的可能参数没有得到很好的记录,读取源代码看起来像参数只是传递给了不是S3特定的Seahorse::Client::Base#build_request
。
I think I've finally worked this out, though I'm still not sure I could trace the entire code path. 我想我终于解决了这个问题,尽管我仍然不确定是否可以跟踪整个代码路径。 In short: You can pass
:version_id
in the options parameter to presigned_url
. 简而言之:您可以将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
And here, everything I could trace about why this works: 在这里,我可以追踪到一切的原因:
presigned_url
passes its params
argument through to @client.build_request
( presigner.rb#L48 ). presigned_url
将其params
参数传递给@client.build_request
( presigner.rb#L48 )。
build_request
eventually pushes those parameters onto request.context.params
of the request it returns (documented in client/base_spec.rb#L91 ). build_request
最终将这些参数推送到它返回的请求的request.context.params
上(记录在client / base_spec.rb#L91中 )。
From here my understanding is fuzzy; 从这里我的理解是模糊的。 I expect that something like Aws::Rest::Request::Builder passes all the params along to create the
Endpoint
and the particular rules for this operation (which I'm unable to find) allow version_id
to be added to the querystring. 我希望类似Aws :: Rest :: Request :: Builder的所有参数一起创建
Endpoint
并且该操作的特定规则(我无法找到)允许将version_id
添加到查询字符串中。
In any case, it's working. 无论如何,它都在工作。 Thanks for the pointer Michael!
感谢迈克尔的指导!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.