【问题标题】:google-cloud TypeError: gcs.bucket is not a functiongoogle-cloud TypeError:gcs.bucket 不是函数
【发布时间】: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


【解决方案1】:

在 Cloud Storage 节点 SDK 2.x 版中更改了 API。根据documentation,你导入SDK是这样的:

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

然后就可以新建一个Storage对象了:

// Creates a client
const storage = new Storage();

然后你可以伸手去拿一个桶:

const bucket = storage.bucket()

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2018-10-11
    • 2018-10-23
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    相关资源
    最近更新 更多