node.js - node.js 上的 Google Storage API 自定义 header

标签 node.js google-cloud-storage

我正在使用 googleapis.auth.JWT 进行身份验证并请求分段上传以将 JSON 文件上传到谷歌存储,它按预期工作。

代码如下:

  var data = JSON.stringify(json);
  var metadata = {
      name: "name"
      contentLanguage: "en",
      acl: [...]
    };

  authClient.authorize(function(err, tokens) {
      if (err) {...}
      request.post({
        'url': 'https://....',
        'qs': {
          'uploadType': 'multipart'
        },  
        'headers' : { 
          'Authorization': 'Bearer ' + tokens.access_token
        },  
        'multipart':  [{  
          'Content-Type': 'application/json; charset=UTF-8',
          'body': JSON.stringify(metadata)
        },{ 
          'Content-Type': 'application/json',
          'body': data
        }]
      }, done);      
    });
  });
}

根据谷歌here如果我想包含自定义 header ,我需要以“x-goog-meta-mycustomheader”的形式添加它

当我将上述元数据对象更改为:

 var metadata = {
      name: "name"
      contentLanguage: "en",
      "x-goog-meta-something": "completely different",
      acl: [...]
  };

没有任何影响。

将对象上传到 Google Storage 时如何添加自定义 header ?

编辑:

请注意,这是一个分段上传,使用第一部分正文作为第二部分(实际部分)的元数据查看详细信息 here

特别是:

If you have metadata that you want to send along with the data to upload, you can make a single multipart/related request. As with simple, media-only requests, this is a good choice if the data you are sending is small enough to upload again in its entirety if the connection fails.

Metadata part: Must come first, and Content-Type must match one of the accepted metadata formats.

Media part: Must come second, and Content-Type must match one the method's accepted media MIME types.

这就是我使用元数据作为标题部分的原因,我还尝试了所有其他组合,例如将“x-goog-meta-something”放在所有其他地方

最佳答案

在这里查看 JSON 请求生成器: https://developers.google.com/storage/docs/json_api/v1/objects/insert

您会注意到 metadata 是正文中的一个单独键。所以你会想要这样的东西:

var metadata = {
      name: "name"
      contentLanguage: "en",
      metadata: {
        "something": "completely different",
      },
      acl: [...]
  };

关于node.js - node.js 上的 Google Storage API 自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378179/

相关文章:

node.js - 如果 IIS 客户端证书身份验证正常,则检查 nodeJS Express

python - 我可以将 Google 存储 blob 作为 n-d 数组下载到 VM 中吗?

javascript - 如何为目录中的每个文件创建元素 | Javascript | NodeJS |埃杰斯

javascript - 是否可以在Node.js上使用js资源或者react Native?

node.js - 无法从 fs.readdir 获取 fs.Dirent 数组

templates - 从现有的 JS Node 上下文中编译 Jade

python-3.x - 将图形写入 Google Cloud Storage 而不是本地驱动器

Android:是否可以将存储在 Google Cloud Storage 中的图像分享到 Instagram?

java - 如何测试应用程序与 Google Cloud Storage 的集成?

java - 使用元素计数通过 Dataflow 写入 GCS