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

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

问题描述

我正在使用googleapis.auth.JWT进行身份验证,并且使用request分段上传将JSON文件上传到Google存储设备,它按预期工作.

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

这是代码:

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

根据Google 此处包括自定义标头,我需要以"x-goog-meta-mycustomheader"形式添加它

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: [...]
  };

没有任何影响.

当我将对象上传到Google Storage时如何添加自定义标题?

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

编辑:

请注意,这是分段上传,使用第一部分主体作为第二部分(实际部分)的元数据,请参见详细信息

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

特别是:

如果您具有要与要上传的数据一起发送的元数据,则可以提出一个与多部分/相关的请求.与简单的仅纯媒体请求一样,如果您要发送的数据足够小,如果连接失败,则可以重新上载整个数据,这是一个不错的选择.

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.

元数据部分:必须位于第一位,并且Content-Type必须与一种可接受的元数据格式匹配.

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

媒体部分:必须排在第二位,并且Content-Type必须与方法接受的媒体MIME类型之一相匹配.

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

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

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

推荐答案

在此处查看JSON请求构建器: https://developers.google.com/storage/docs/json_api/v1 /objects/insert

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

您会注意到metadata是主体中的单独键.所以你会想要像这样的东西:

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天全站免登陆