Node.js Google-cloud storage upload destination sp

2019-07-11 12:36发布

I have a Node.js server and I'm using google-cloud package to upload some image files to Firebase Storage.

Upload itself works fine, but google-cloud API seem to be only able to upload files to Firebase Storage root folder.

Is there any way to specify remote location to upload image to?

What I'm doing now:

const serviceAccount = require('./serviceAccount.json');

const storage = gcloud.storage({
   projectId: '*projectId*',
   credentials: serviceAccount
});

const storageBucket = storage.bucket('*bucket*');

storageBucket.upload(localFsPath)
   .then(data => /* Some stuff */)
   .catch(err => console.log(err));

Is there a way to do something like this?

storageBucket.upload(path, { destination: 'a/b/c/file.jpg' })
   .then(data => /* Some stuff */)
   .catch(err => console.log(err));

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-11 13:14

As @Jérôme pointed out, the approach was correct. I overlooked wrong format of destination path string which was '/x/y//z'.

There are obviously 2 mistakes.

Firstly, the path must not start with '/', but a name of node (file/folder).

Second problem (the '//' part) interestingly didn't cause API to throw, but caused Firebase to recursively create endless nested structure of nodes (folders) named '/'.

查看更多
登录 后发表回答