我正在使用 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/