我正在尝试实现云功能,但如果需要像这样的
var storage =require('@google-cloud/storage')();
像这样部署
var storage = require('@google-cloud/storage');
所以我决定如上所述使用它,但尝试上传一张图片,我收到错误"TypeError:gcs.bucket不是一个函数">
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在云存储节点SDK的2.x版本中发生了更改。根据文档,您导入的SDK如下:
// Imports the Google Cloud client library const {Storage} = require('@google-cloud/storage');
然后您可以创建一个新的存储对象:
// Creates a client const storage = new Storage();
然后你可以进入一个桶:
const bucket = storage.bucket()