一尘不染

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

node.js

我正在尝试实现云功能,但是如果我需要这样会出错

   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
     })
   });
});

阅读 250

收藏
2020-07-07

共1个答案

一尘不染

API在Cloud
Storage节点SDK的2.x版本中进行了更改。根据文档,您可以这样导入SDK:

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

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

// Creates a client
const storage = new Storage({
  projectId: projectId,
});

然后您可以进入一个桶:

const bucket = storage.bucket()
2020-07-07