最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - google-cloud TypeError: gcs.bucket is not a function - Stack Overflow

programmeradmin5浏览0评论

I am trying to implement cloud function but getting error if i require it like this

   var storage =require('@google-cloud/storage')(); 

like this when deploying

var storage = require('@google-cloud/storage');

so i resolved to using it as above but tried uploading a picture i am getting error "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
     })
   });
});

I am trying to implement cloud function but getting error if i require it like this

   var storage =require('@google-cloud/storage')(); 

like this when deploying

var storage = require('@google-cloud/storage');

so i resolved to using it as above but tried uploading a picture i am getting error "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
     })
   });
});
Share Improve this question edited Oct 28, 2018 at 15:46 Doug Stevenson 318k36 gold badges454 silver badges472 bronze badges asked Oct 28, 2018 at 15:05 R.OvieR.Ovie 3394 silver badges14 bronze badges 2
  • What version of the @google-cloud/storage module are you using? – Doug Stevenson Commented Oct 28, 2018 at 15:45
  • i am using ^2.2.0 didnt version it i just ran install without it so i guess latest. – R.Ovie Commented Oct 28, 2018 at 15:47
Add a ment  | 

1 Answer 1

Reset to default 18

The API changed in version 2.x of the Cloud Storage node SDK. According to the documentation, you import the SDK like this:

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

Then you can create a new Storage object:

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

Then you can reach into a bucket:

const bucket = storage.bucket()
发布评论

评论列表(0)

  1. 暂无评论