Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I zip files in Firebase Storage via Firebase Cloud Functions?

Is it possible to compress multiple files in Firebase Storage using Cloud Functions?

For example, there are 5 images uploaded by users and Firebase Cloud Functions will create a zip file for these 5 images

like image 402
Booker Chan Avatar asked Jul 27 '18 18:07

Booker Chan


People also ask

How do I move files in Firebase Storage?

Create a Cloud Storage bucket to hold the data from your source project. Export the data from your source project to the bucket. Give your destination project permission to read from the bucket. Import the data from the bucket into your destination project.

Is Firebase Storage same as Google Cloud Storage?

The new Firebase Storage is powered by Google Cloud Storage, giving it massive scalability and allowing stored files to be easily accessed by other projects running on Google Cloud Platform. Firebase now uses the same underlying account system as GCP, which means you can use any GCP product with your Firebase app.

How do I upload folders to Firebase Storage?

There is no way to upload an entire folder to Cloud Storage for Firebase in one go. You will have to upload the individual files in the folder. The is no concept of an empty folder in Cloud Storage for Firebase. Folders only exist by the fact that they have files in them.


1 Answers

Could not find e2e guide for similar scenario in functions myself, so had to combine solutions for zipping, accessing files in cloud storage etc. See result below:

import * as functions from 'firebase-functions';
import admin from 'firebase-admin';
import archiver from 'archiver';
import { v4 as uuidv4 } from 'uuid';

export const createZip = functions.https.onCall(async () => {
  const storage = admin.storage();
  const bucket = storage.bucket('bucket-name');

  // generate random name for a file
  const filePath = uuidv4();
  const file = bucket.file(filePath);

  const outputStreamBuffer = file.createWriteStream({
    gzip: true,
    contentType: 'application/zip',
  });

  const archive = archiver('zip', {
    gzip: true,
    zlib: { level: 9 },
  });

  archive.on('error', (err) => {
    throw err;
  });

  archive.pipe(outputStreamBuffer);

  // use firestore, request data etc. to get file names and their full path in storage
  // file path can not start with '/' 
  const userFilePath = 'user-file-path';
  const userFileName = 'user-file-name';

  const userFile = await bucket.file(userFilePath).download();
  archive.append(userFile[0], {
    name: userFileName, // if you want to have directory structure inside zip file, add prefix to name -> /folder/ + userFileName
  });

  archive.on('finish', async () => {
    console.log('uploaded zip', filePath);

    // get url to download zip file
    await bucket
      .file(filePath)
      .getSignedUrl({ expires: '03-09-2491', action: 'read' })
      .then((signedUrls) => console.log(signedUrls[0]));
  });

  await archive.finalize();
});
like image 187
Yegor Androsov Avatar answered Sep 21 '22 00:09

Yegor Androsov