【发布时间】:2019-04-01 15:25:41
【问题描述】:
我正在尝试实现云功能,但如果我遇到错误 像这样要求它
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
})
});
});
【问题讨论】:
-
您使用的是哪个版本的 @google-cloud/storage 模块?
-
我正在使用 ^2.2.0 没有版本,我只是在没有它的情况下运行安装,所以我猜是最新的。
标签: javascript node.js google-cloud-functions