You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP Node.js客户端中设置请求头避免decompressive transcoding?

解决GCP Node.js客户端下载文件时的解压缩转码问题

问题背景

我在GCP存储桶中存储了gzip格式的JSON文件,已配置以下元数据:

  • Content-type: application/json
  • Content-encoding: gzip
  • Cache-control: no-transform

按官方说明,no-transform应能避免下载时的自动解压缩转码,但实际无效。用curl命令添加Accept-Encoding: gzip请求头后,可成功获取原始压缩文件,但使用GCP Node.js客户端的File.download方法时,不知道如何添加请求头实现同样效果。

解决方案

@google-cloud/storage客户端的File.download方法支持通过配置参数自定义请求头,调用时传入headers字段即可:

import { Storage, File } from '@google-cloud/storage'

export const downloadFile = async (file: File, destination: string) => {
    try {
        await file.download({
            destination,
            headers: {
                'Accept-Encoding': 'gzip'
            }
        })
    } catch (err) {
        console.error('下载失败:', err)
        // 自定义错误处理逻辑
    }
}

若你的客户端版本较旧导致headers参数不生效,可尝试通过requestOptions嵌套传递请求头:

await file.download({
    destination,
    requestOptions: {
        headers: {
            'Accept-Encoding': 'gzip'
        }
    }
})

添加该请求头后,GCP存储会直接返回原始的gzip压缩文件,不会触发自动解压缩转码,效果与curl请求完全一致。

内容的提问来源于stack exchange,提问作者Joao Leme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:53:25