【问题标题】:no such file or directory when downloading a file from firebase cloud storage using http cloud cloud functions使用http云云功能从firebase云存储下载文件时没有这样的文件或目录
【发布时间】:2019-02-20 07:10:28
【问题描述】:

我在 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 作为云函数的响应。

【问题讨论】:

    标签: node.js firebase filesystems google-cloud-storage google-cloud-functions


    【解决方案1】:

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

    【讨论】:

    • 但是当我们使用/tmp时我们找不到文件地址。我使用/tmp /download.txt。我收到此错误错误:下载的数据与来自服务器的数据不匹配。为确保内容相同,您应重新下载文件。
    • 我不知道那是什么意思。
    • 如果我们使用 /tmp 那么我们不知道 /tmp 目录是在哪里创建的。我说的对吗?
    • 我们可以获取存储的文件下载 url 作为响应吗?
    • 还是不明白你的意思。 /tmp 是 /tmp。 cloud.google.com/functions/docs/concepts/exec#file_system
    猜你喜欢
    • 2019-08-08
    • 2021-01-12
    • 2018-11-17
    • 2017-12-21
    • 2019-04-16
    • 2018-11-13
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多