node.js 上的 Google Storage API 自定义标头 [英] Google Storage API custom header on node.js

查看:22
本文介绍了node.js 上的 Google Storage API 自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am using googleapis.auth.JWT to authenticate and request multipart upload to upload JSON files into google storage, it is working as expected.

Here is the code:

  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);      
    });
  });
}

According to google here if I want to include custom headers I need to add it in the form of "x-goog-meta-mycustomheader"

When I change my above metadata object to this:

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

It doesn't have any affect.

How do I add custom headers when I upload an object to Google Storage?

EDIT:

Please notice that this is a multipart upload that uses the first part body as the metadata of the second part (which is the actual part) see details here

Especially:

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.

This is why I use the metadata as the header section, I also tried all other combinations like putting the "x-goog-meta-something" in all other places

解决方案

Take a look at the JSON request builder here: https://developers.google.com/storage/docs/json_api/v1/objects/insert

You'll notice that metadata is a separate key in the body. So you'll want something like:

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

这篇关于node.js 上的 Google Storage API 自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆