gpt4 book ai didi

javascript - google-cloud TypeError : gcs. bucket 不是函数

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:22 25 4
gpt4 key购买 nike

我正在尝试实现云功能,但如果我遇到错误像这样要求

   var storage =require('@google-cloud/storage')(); 

部署时像这样

var storage = require('@google-cloud/storage');

所以我决定像上面那样使用它,但尝试上传图片时出现错误“TypeError: gcs.bucket is not a function”

const os = require('os');
const path = require('path');

///

exports.onFileChange = functions.storage.object().onFinalize((event) => {
const bucket = event.bucket;
const contentType = event.contentType;
const filePath = event.name;
console.log('Changes made to bucket');

///

 if(path.basename(filePath).startsWith('renamed-')){
console.log("File was previously renamed");
return;
}
const gcs = storage({
projectId: 'clfapi'
});

///

 const destBucket = gcs.bucket(bucket);
const tmFiilePath = path.join(os.tmpdir(), path.basename(filePath));
const metadata = {contentType: contentType};

///

 return destBucket.file(filePath).download({
destination: tmFiilePath
}).then(() => {
return destBucket.upload(tmFiilePath, {
destination: 'renamed-' + path.basename(filePath),
metadata: metadata
})
});
});

最佳答案

API 在 Cloud Storage Node SDK 的 2.x 版中发生了变化。根据documentation ,您像这样导入 SDK:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

然后你可以创建一个新的存储对象:

// Creates a client
const storage = new Storage();

然后你可以 Handlebars 伸进一个桶里:

const bucket = storage.bucket()

关于javascript - google-cloud TypeError : gcs. bucket 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com