Google云端存储:下载具有不同名称的文件

dab*_*lak 5 google-app-engine google-cloud-storage

我想知道是否可以从谷歌云存储中下载一个文件,其名称不同于存储桶中的文件.

例如,在谷歌云存储中我存储了一个名为123-file.txt但是当我下载它的文件时,我想选择一个不同的名称,让我们说file.txt

我注意到它的下载链接如下:https: //storage.cloud.google.com/bucket_name%2F123-file.txt ?response-content-disposition=attachment; %20filename = 123-file.txt

所以我试图将其更改为:https: //storage.cloud.google.com/bucket_name%2F123-file.txt ?response-content-disposition=attachment; %20filename = file.txt

但它仍然继续下载为123-file.txt而不是file.txt

Bra*_*ugh 6

response-content-disposition参数只能由授权请求使用.匿名链接无法使用它.你有几个选择:

  1. 特定对象的内容处置是其元数据的一部分,可以永久设置.如果您始终希望使用特定名称下载特定文件,则可以永久设置该对象的内容处置元数据.

  2. 您还可以生成包含response-content-disposition查询参数的签名URL.然后,用户将发出授权请求以下载资源.

  • 签名的网址为我工作.虽然,我试图用参数response_headers和值response-content-disposition调用generate_url(),但是我收到了格式错误的签名网址.所以我的解决方案是将'&response-content-disposition = attachment%3B%20filename%3D"{}"'.format(file_name)连接到签名的URL并且它工作正常. (2认同)
  • 响应内容处置查询参数记录在哪里?其他有效的查询参数是什么? (2认同)

vit*_*ytv 5

带有 javascript 库的示例(第一个选项 Brandon Yarbrough):

const storage = new Storage()
const fileBucket = storage.bucket('myBucket')
const file = fileBucket.file('MyOriginalFile.txt')
const newName = "NewName.txt"
await file.save(content, {
    metadata: {
      contentDisposition: `inline; filename="${newName}"`
    }
  })
Run Code Online (Sandbox Code Playgroud)