如何使用 GCS Ruby SDK 生成具有内容类型处置的预签名 URL



在 Amazon S3 开发工具包中,在生成预签名 URL 时,我可以生成一个设置内容处置的 URL,但我找不到使用 Google 配置这些选项的类似方法。

我们可用的方法调用是:

https://www.rubydoc.info/gems/google-cloud-storage/0.20.0/Google/Cloud/Storage/File#signed_url-instance_method

我正在尝试将文件直接上传到Google存储桶,而不是通过我的Rails服务器,并且想要设置通过预签名URL上传的文件的内容处置。有没有办法做到这一点?有没有办法通过预签名 URL 设置对象元数据?

require "google/cloud"
gcloud = Google::Cloud.new
storage = gcloud.storage
bucket = storage.bucket "my-todo-app"
file = bucket.file "avatars/heidi/400x400.png"
shared_url = file.signed_url
# The parameters it takes is: signed_url(method: nil, expires: nil, content_type: nil, content_md5: nil, issuer: nil, client_email: nil, signing_key: nil, private_key: nil)
# how do we add content_disposition?
# how do we add object meta data?

我正在寻找相同的答案,到目前为止,我已经找到了另一篇 Stack Overflow 文章,它没有直接解决 Ruby SDK,但它确实提供了一条很好的信息。

Google Cloud Storage:下载具有不同名称的文件

您还可以生成包含响应内容处置查询参数的签名 URL。然后,用户将提出下载资源的授权请求。

因此,您似乎可以将查询参数?response-content-disposition=[disposition-string-urlencoded]添加到生成的有符号字符串中。

因此,我查看了Ruby SDK中的文件对象定义,似乎您可以这样做:

file.content_dispostion = "attachment" # or whatever your disposition is
url = file.signed_url

注意:如果您在我编辑之前阅读了我的条目,我很抱歉。

最新更新