google-cloud TypeError:gcs.bucket不是函数 [英] google-cloud TypeError: gcs.bucket is not a function
本文介绍了google-cloud TypeError:gcs.bucket不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现云功能,但是如果我遇到错误 要求这样
I am trying to implement cloud function but getting error if i require it like this
var storage =require('@google-cloud/storage')();
部署时
像这样
like this when deploying
var storage = require('@google-cloud/storage');
所以我决定使用上述方法,但是尝试上传图片时出现错误"TypeError:gcs.bucket不是函数"
so i resolved to using it as above but tried uploading a picture i am getting error "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节点SDK的2.x版本中进行了更改.根据文档 ,您可以这样导入SDK:
The API changed in version 2.x of the Cloud Storage node SDK. According to the documentation, you import the SDK like this:
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
然后您可以创建一个新的存储对象:
Then you can create a new Storage object:
// Creates a client
const storage = new Storage({
projectId: projectId,
});
然后您可以进入水桶:
const bucket = storage.bucket()
这篇关于google-cloud TypeError:gcs.bucket不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文