Node.js 如何使用服务帐户凭据初始化Google云存储NodeJS客户端库
因此,我在代码中将服务帐户凭据json作为字符串或json对象:Node.js 如何使用服务帐户凭据初始化Google云存储NodeJS客户端库,node.js,google-cloud-platform,google-cloud-storage,google-authentication,Node.js,Google Cloud Platform,Google Cloud Storage,Google Authentication,因此,我在代码中将服务帐户凭据json作为字符串或json对象: { "type": "service_account", "project_id": "myproject", "private_key_id": "lkjshdjhskas", //.... etc } 然后我有一个简单的云存储上传片段: const {Storage} = require('@google-cloud/storage'); const creds = {service_account_obj
{
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
然后我有一个简单的云存储上传片段:
const {Storage} = require('@google-cloud/storage');
const creds = {service_account_object:"creds are here"};
const storage = new Storage();
const bucketName = 'my-bucket';
const filename = 'my-file.txt';
storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
这给了我一个无效的\u grant错误,当然:
(node:15920) UnhandledPromiseRejectionWarning: Error: invalid_grant
at Gaxios.request (C:\Users\Yuri\Documents\Code\btests\node_modules\gaxios\build\src\gaxios.js:70:23)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:15920) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我尝试将creds作为参数提供给存储构造函数,这是Storagecreds,甚至是StorageJSON.stringifycreds,但它只会给我带来奇怪的错误
如何使用服务帐户凭据实例化Google客户端库对象?凭据数据应在传递给构造函数的对象的凭据属性中传递:
const creds = {
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
const storage = new Storage({
credentials: creds
});
一定要熟悉链接的API文档,因为它们将解释如何使用API。凭证数据应在传递给构造函数的对象的credentials属性中传递:
const creds = {
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
const storage = new Storage({
credentials: creds
});
一定要熟悉链接的API文档,因为它们将解释如何使用API