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

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

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"?

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

  1. const updateStorageData = async () => {
  2. return admin
  3. .storage()
  4. .bucket()
  5. .file('documents/1/Screenshot 2023-05-09 at 00.33.45.png')
  6. .copy(
  7. admin
  8. .storage()
  9. .bucket()
  10. .file('users/testID/documents/1/Screenshot 2023-05-09 at 00.33.45.png'),
  11. );
  12. };

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

英文:

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:

  1. const updateStorageData = async () => {
  2. return admin
  3. .storage()
  4. .bucket()
  5. .file('documents/1/Screenshot 2023-05-09 at 00.33.45.png')
  6. .copy(
  7. admin
  8. .storage()
  9. .bucket()
  10. .file('users/testID/documents/1/Screenshot 2023-05-09 at 00.33.45.png'),
  11. );
  12. };

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()方法,按照以下方式操作:

  1. const getFilesResponse = await admin
  2. .storage()
  3. .bucket()
  4. .getFiles();
  5. const arrayOfFiles = getFilesResponse[0];
  6. const promises = [];
  7. arrayOfFiles.forEach(file => {
  8. // 根据文件的路径(或元数据)检查是否需要移动文件
  9. if (fileToBeMoved) {
  10. promises.push(file.move('....png'))
  11. }
  12. });
  13. await Promise.all(promises)
英文:

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

  1. const getFilesResponse = await admin
  2. .storage()
  3. .bucket()
  4. .getFiles();
  5. const arrayOfFiles = getFilesResponse[0];
  6. const promises = [];
  7. arrayOfFiles.forEach(file => {
  8. // Check if the file needs to be moved based on its path (or metadata)
  9. if (fileToBeMoved) {
  10. promises.push(file.move('....png'))
  11. }
  12. });
  13. 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:

确定