Node.js Google云存储上传目标规范 [英] Node.js Google-cloud storage upload destination specification

查看:73
本文介绍了Node.js Google云存储上传目标规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node.js服务器,并且正在使用 google-cloud 包将一些图像文件上传到 Firebase存储.

上传本身可以正常工作,但是google-cloud API似乎只能将文件上传到Firebase存储根文件夹.

是否可以指定将图像上传到的远程位置?

我现在正在做什么:

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

有没有办法做这样的事情?

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

解决方案

正如@Jérôme所指出的那样,该方法是正确的.我忽略了目标路径字符串的错误格式,即"/x/y//z".

显然有两个错误.

首先,路径不能以'/'开头,而必须是节点名称(文件/文件夹).

有趣的是,第二个问题("//"部分)没有引发API,而是导致Firebase递归地创建了名为"/"的节点(文件夹)的 endless 嵌套结构.

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

解决方案

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 '/'.

这篇关于Node.js Google云存储上传目标规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆