【问题标题】:Google Cloud Storage: download a file with a different nameGoogle Cloud Storage:下载具有不同名称的文件
【发布时间】:2014-01-28 08:28:01
【问题描述】:

我想知道是否可以从 Google Cloud Storage 下载名称与存储桶中名称不同的文件。

例如,在 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

【问题讨论】:

    标签: google-app-engine google-cloud-storage


    【解决方案1】:

    response-content-disposition 参数只能由授权请求使用。匿名链接不适用于它。您有几个选择:

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

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

    【讨论】:

    • 签名的网址对我有用。虽然,我尝试使用参数 response_headers 和值 response-content-disposition 调用 generate_url() 但我得到了格式错误的签名 URL。所以我的解决方案是将 '&response-content-disposition=attachment%3B%20filename%3D"{}"'.format(file_name) 连接到签名的 url 并且它有效。
    • response-content-disposition 查询参数记录在哪里?其他有效的查询参数是什么?
    • 有效 XML API 参数的完整列表位于 cloud.google.com/storage/docs/reference-headers
    • @BrandonYarbrough 我正在使用 .net 客户端库(截至今天的最新版本)来上传文件。您能否让我知道我需要在代码中的哪个位置设置 content-disposition 哪个类和我需要使用哪种方法/属性。我在整个谷歌搜索,但找不到任何例子。你能帮忙吗
    【解决方案2】:

    带有 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}"`
        }
      })
    

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2020-09-30
      相关资源
      最近更新 更多