Copy a file on firebase storage?
Asked Answered
S

4

12

Is it possible to copy an existing file on firebase storage without needing to uploading it again?

I need it for a published/working version setup of my app.

Scotia answered 17/9, 2016 at 11:1 Comment(3)
Copy in what sense ? From your storage to device or some other way ?Crackerbarrel
sorry I meant copy on the firebase storage itself.Scotia
UPDATE 2021 : cloud.google.com/storage/docs/…Breadnut
H
10

There is no method in the Firebase Storage API to make a copy of a file that you've already uploaded.

But Firebase Storage is built on top of Google Cloud Storage, which means that you can use the latter's API too. It looks like gsutil cp is what you're looking for. From 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.

Keep in mind that gsutil has full access to your storage bucket. So it is meant to be run on devices you fully trust (such as a server or your own development machine).

Harwill answered 17/9, 2016 at 15:4 Comment(1)
I guess one can use this node library to copy files (not sure if it allows copying folders though yet): github.com/googleapis/nodejs-storageO
O
5

Here is an approach I ended up with for my project.

While it covers a broader case and copies all files under folder fromFolder to toFolder, it can be easily adopted to the case from the question (to copy only files one can pass delimiter = "/" - refer to the docs for more details)

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)
            })
    }
}
O answered 9/2, 2020 at 11:37 Comment(0)
P
3

I needed to copy folders (including all it's descendants) in Google Cloud Storage. Here is the solution proposed by @vir-us, simplified and for NodeJS.

import type { Bucket } from '@google-cloud/storage';

/** Copy Cloud Storage resources from one folder to another. */
export const copyStorageFiles = async (input: {
  bucket: Bucket;
  fromFolder: string;
  toFolder: string;
  logger?: Console['info'];
}) => {
  const { bucket, fromFolder, toFolder, logger = console.info } = input;

  const [files] = await bucket.getFiles({ prefix: fromFolder });
  const promiseArray = files.map((file) => {
    const destination = file.name.replace(fromFolder, toFolder);
    logger("fileName = ", file.name, ", destination = ", destination);
    return file.copy(destination);
  });
  return Promise.all(promiseArray);
};
Punch answered 4/10, 2022 at 12:7 Comment(0)
W
0

Firebase Storage clients support file copying inside the same bucket:

await bucketFileRef.copy('new-path/to/file');

Directly with Google Cloud Storage

Direct access is possible with @google-cloud/storage.

From firebase-admin

firebase-admin package, which should be used in a function backend, references @google-cloud/storage and can be used in a similar way.

Full example:

import { getStorage } from 'firebase-admin/storage';


const firebaseApp = initializeApp({
  storageBucket: 'YOUR-BUCKET-PATH',
});

const main = async() => {
  const bucket = getStorage(firebaseApp).bucket();

  const bucketFileRefToCopy = bucket.file('path/file-to-copy');

  // COPY
  await bucketFileRefToCopy.copy('new-path/to/file');
};


main();
Went answered 15/5, 2024 at 22:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.