我正在尝试使用Node.js库创建一个GCP存储桶。我一直在使用这里的步骤:https://cloud.google.com/storage/docs/creating-buckets storage-create-bucket-nodejs
和代码粘贴在下面。挑战在于我的bucket总是在错误的项目中创建。我的项目设置在我的gcloud cli,它设置在我的节点环境,它设置在我的脚本。是否有某种方法可以将项目设置为传递给库的createBucket函数的值?
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const storageClass = 'Name of a storage class, e.g. coldline';
// const location = 'Name of a location, e.g. ASIA';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function createBucketWithStorageClassAndLocation() {
// For default values see: https://cloud.google.com/storage/docs/locations and
// https://cloud.google.com/storage/docs/storage-classes
const [bucket] = await storage.createBucket(bucketName, {
location,
[storageClass]: true,
});
console.log(
`${bucket.name} created with ${storageClass} class in ${location}.`
);
}
createBucketWithStorageClassAndLocation();
您可以在初始化Storage类时指定projectId:
const storage = new Storage({
projectId: 'my-project-id',
keyFilename: '/path/to/keyfile.json'
});
来源