如何将存储文件夹复制到新文件夹?(云存储)

huangapple go评论77阅读模式
英文:

How to copy storage folder into a new one? (Cloud storage)

问题

我正在尝试将存储中的数据从 documents/docId/file.name 迁移到 users/userId/documents/docId/file.name。是否可以复制类似 documents/docId 这样的文件夹而无需定义 "file.name"?

我的代码目前只在我提供确切的文件名时才能工作。它看起来是这样的:

const updateStorageData = async () => {
  return admin
    .storage()
    .bucket()
    .file('documents/1/Screenshot 2023-05-09 at 00.33.45.png')
    .copy(
      admin
        .storage()
        .bucket()
        .file('users/testID/documents/1/Screenshot 2023-05-09 at 00.33.45.png'),
    );
};

我想尝试复制文件夹中的所有文件,而无需提供确切的文件名。这是否可能?

英文:

I am trying to migrate data in storage from documents/docId/file.name to users/userId/documents/docId/file.name. Is it possible to copy folders like documents/docId without needing to define "file.name"?

My code currently only works when I provide the exact file name. It looks like this:

const updateStorageData = async () => {
  return admin
    .storage()
    .bucket()
    .file('documents/1/Screenshot 2023-05-09 at 00.33.45.png')
    .copy(
      admin
        .storage()
        .bucket()
        .file('users/testID/documents/1/Screenshot 2023-05-09 at 00.33.45.png'),
    );
};

I would like to try to copy all files in the folder without needing to provide the exact file name. Is this possible?

答案1

得分: 1

没有一个单独的API可以复制文件夹中的所有文件,但你可以列出文件夹中的文件,然后在循环中逐个复制。

英文:

There no single API to copy all files in a folder, but you can list the files in a folder and then copy them one-by-one in a loop.

答案2

得分: 1

你需要使用getFiles()move()方法,按照以下方式操作:

const getFilesResponse = await admin
    .storage()
    .bucket()
    .getFiles();
const arrayOfFiles = getFilesResponse[0];

const promises = [];
arrayOfFiles.forEach(file => {
    // 根据文件的路径(或元数据)检查是否需要移动文件
    if (fileToBeMoved) {
        promises.push(file.move('....png'))
    }
});

await Promise.all(promises)
英文:

You need to use the getFiles() and move() methods along the following lines:

const getFilesResponse = await admin
    .storage()
    .bucket()
    .getFiles();
const arrayOfFiles = getFilesResponse[0];

const promises = [];
arrayOfFiles.forEach(file => {
    // Check if the file needs to be moved based on its path (or metadata)
    if (fileToBeMoved) {
        promises.push(file.move('....png'))
    }
});

await Promise.all(promises)

huangapple
  • 本文由 发表于 2023年5月17日 21:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定