R.O*_*vie 2 javascript node.js google-cloud-functions
我正在尝试实现云功能,但是如果我需要这样会出错
var storage =require('@google-cloud/storage')();
Run Code Online (Sandbox Code Playgroud)
部署时是这样的
var storage = require('@google-cloud/storage');
Run Code Online (Sandbox Code Playgroud)
所以我决定使用上面的方法,但是尝试上传图片时出现错误“ TypeError:gcs.bucket不是函数”
const os = require('os');
const path = require('path');
Run Code Online (Sandbox Code Playgroud)
///
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');
Run Code Online (Sandbox Code Playgroud)
///
if(path.basename(filePath).startsWith('renamed-')){
console.log("File was previously renamed");
return;
}
const gcs = storage({
projectId: 'clfapi'
});
Run Code Online (Sandbox Code Playgroud)
///
const destBucket = gcs.bucket(bucket);
const tmFiilePath = path.join(os.tmpdir(), path.basename(filePath));
const metadata = {contentType: contentType};
Run Code Online (Sandbox Code Playgroud)
///
return destBucket.file(filePath).download({
destination: tmFiilePath
}).then(() => {
return destBucket.upload(tmFiilePath, {
destination: 'renamed-' + path.basename(filePath),
metadata: metadata
})
});
});
Run Code Online (Sandbox Code Playgroud)
Dou*_*son 11
API在Cloud Storage节点SDK的2.x版本中进行了更改。根据文档,您可以这样导入SDK:
Run Code Online (Sandbox Code Playgroud)// Imports the Google Cloud client library const {Storage} = require('@google-cloud/storage');
然后,您可以创建一个新的存储对象:
Run Code Online (Sandbox Code Playgroud)// Creates a client const storage = new Storage({ projectId: projectId, });
然后您可以进入一个桶:
const bucket = storage.bucket()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2289 次 |
| 最近记录: |