繁体   English   中英

使用http cloud云功能从Firebase云存储下载文件时,没有此类文件或目录

[英]no such file or directory when downloading a file from firebase cloud storage using http cloud cloud functions

我在Firebase云存储中编写了一个.txt文件,我想下载该.txt文件并将其发送给另一台服务器验证。 我尝试了很多下载该文件的方法,但出现了类似的错误

"Error: ENOENT: no such file or directory, open '/Users/vijayasrivuddanti/Downloads/download.txt'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/vijayasrivuddanti/Downloads/download.txt'"

这是我尝试的代码:

function writetofile(){
const storage = new Storage();
const  bucket = storage.bucket('deyapay-192704.appspot.com'); 
console.log("date",Date.now());
var datetime = new Date();
console.log("iso", new Date().toISOString().slice(0,10));
const file = bucket.file(new Date().toISOString().slice(0,10)+".txt"); // It create the file in the bucket in ascii format and save as today date format.
const a = new Date().toISOString().slice(0,10)+".ascii"
console.log("filename",a);
var storageRef = admin.storage().bucket();
const uploadStream = file.createWriteStream();
uploadStream.on('error', function(err) {
console.log("write err",err);
});
finalnachaformat.forEach(function(v) { // It takes the each value in 
                                       finalnachaformat in every loop.
    console.log("v",v)
    console.log("gii");
    uploadStream.write(v+ '\n'); // and write data into file.
    console.log("looping function",v);   
}) 
 console.log("successfully uploaded")
uploadStream.end();// writing end
//-
// Download a file into memory. The contents will be available as the second
// argument in the demonstration below, `contents`.
//-
file.download(function(err, contents) {});

//-
// Download a file to a local destination.
//-
file.download({
  destination: '/Users/vijayasrivuddanti/Downloads/download.txt'

}, function(err) {

    console.log("error",err);
});

//-
// If the callback is omitted, we'll return a Promise.
//-
file.download().then(function(data) {
  const contents = data[0];
  console.log("entred");
  res.send("ok");
});

如何解决该错误? 有什么方法可以从云存储下载该文件,以及如何获取存储的文件URL作为云功能的响应。

您正在尝试将文件下载到Cloud Functions运行时中的不可写(和不存在)文件夹中。 没有/ Users这样的文件夹。 那只存在于您自己的计算机上。 Cloud Functions中唯一可写的文件夹是os.tmpdir()或/ tmp。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM