在 Firebase 存储上复制文件?

Copy a file on firebase storage?

是否可以复制 Firebase 存储中的现有文件而无需再次上传?

我需要它来设置我的应用程序的 published/working 版本。

Firebase 存储 API 中没有方法可以复制您已上传的文件。

但是 Firebase Storage 构建在 Google Cloud Storage 之上,这意味着您也可以使用后者的 API。看起来 gsutil cp 就是您要查找的内容。来自 the docs:

The gsutil cp command allows you to copy data between your local file system and the cloud, copy data within the cloud, and copy data between cloud storage providers.

请记住,gsutil 可以完全访问您的存储桶。所以它意味着 运行 在您完全信任的设备上(例如服务器或您自己的开发机器)。

这是我最终为我的项目采用的方法。

虽然它涵盖了更广泛的案例并将文件夹 fromFolder 下的所有文件复制到 toFolder,但它可以很容易地从问题中采纳(仅复制文件可以通过定界符 = “/”- 请参阅 the docs 了解更多详情)

const {Storage} = require('@google-cloud/storage');


module.exports = class StorageManager{

    constructor() {
        this.storage = new Storage();
        this.bucket = this.storage.bucket(<bucket-name-here>)
    }

    listFiles(prefix, delimiter){
        return this.bucket.getFiles({prefix, delimiter});
    }
    deleteFiles(prefix, delimiter){
        return this.bucket.deleteFiles({prefix, delimiter, force: true});
    }

    copyFilesInFolder(fromFolder, toFolder){
        return this.listFiles(fromFolder)
            .then(([files]) => {
                let promiseArray = files.map(file => {
                    let fileName = file.name
                    let destination = fileName.replace(fromFolder, toFolder)
                    console.log("fileName = ", fileName, ", destination = ", destination)
                    return file.copy(destination)
                })
                return Promise.all(promiseArray)
            })
    }
}