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?
2
Answers
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.
You need to use the
getFiles()
andmove()
methods along the following lines: