如何在 Google Cloud Storage 中设置 file.download() 的目的地?

Tho*_*hoe 0 google-cloud-storage firebase google-cloud-functions firebase-storage

该谷歌云存储文件download()建议,目标文件夹可以指定:

file.download({
  destination: '/Users/me/Desktop/file-backup.txt'
}, function(err) {});
Run Code Online (Sandbox Code Playgroud)

无论我在文件中输入什么值,它都会在根级别下载到 Firebase Cloud Storage。这个问题说路径不能有初始斜杠但将示例更改为

file.download({
  destination: 'Users/me/Desktop/file-backup.txt'
}, function(err) {});
Run Code Online (Sandbox Code Playgroud)

没什么区别。

将目的地更改为

file.download({
      destination: ".child('Test_Folder')",
    })
Run Code Online (Sandbox Code Playgroud)

导致错误消息:

EROFS: read-only file system, open '.child('Test_Folder')'
Run Code Online (Sandbox Code Playgroud)

云存储destination(文件夹和文件名)的正确语法是什么?

将存储桶从 更改myapp.appspot.commyapp.appspot.com/Test_Folder导致错误消息:

Cannot parse JSON response
Run Code Online (Sandbox Code Playgroud)

此外,示例路径似乎指定了个人计算机硬盘驱动器上的位置。为Desktop. 这是否意味着有一种方法可以在 Cloud Storage 之外的某个地方指定目的地?

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {

  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage();
  const bucket = storage.bucket('myapp.appspot.com');

  bucket.upload('./hello_world.ogg')
  .then(function(data) {
    const file = data[0];
    file.download({
    destination: 'Test_Folder/hello_dog.ogg',
  })
    .then(function(data) {
      const contents = data[0];
      console.log("File uploaded.");
    })
    .catch(error => {
      console.error(error);
    });
  })
  .catch(error => {
    console.error(error);
  });
  return 0;
});
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 5

根据文档

文件系统唯一可写的部分是 /tmp 目录,您可以使用它在函数实例中存储临时文件。这是一个本地磁盘安装点,称为“tmpfs”卷,其中写入卷的数据存储在内存中。请注意,它将消耗为该函数提供的内存资源。

文件系统的其余部分是只读的,可供函数访问。

您应该使用os.tmpdir()获取当前运行时的最佳可写目录。