Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a folder in Firebase Storage using Admin API

Aim: to upload a file into a folder within Firebase Storage

E.g.

default_bucket/folder1/file1
default_bucket/folder1/file2
default_bucket/folder2/file3

Using Firebase client-side I am able to upload a file to a folder within Firebase Storage like this:

    const storageRef = firebase.storage().ref();
    const fileRef = storageRef.child(`${folder}/${filename}`);
    const metadata = {
      contentType: file.type,
      customMetadata: { }
    };
    return fileRef.put(file, metadata);

If the folder does not exist, it get's created.

However I have not managed to do the same server-side using the Admin SDK.

The code below, uploads the file into the default bucket.

But, I want to upload the file into a named folder within the default bucket.

The client side makes a POST request to the GCF, sending the file and a folder name.

Busboy is used to extra the folder name and file and pass them to the upload function; which uploads the file, then returns a donwnload link for it.

index.js

const task = require('./tasks/upload-file-to-storage');

app.post('/upload', (req, res, next) => {
  try {
    let uploadedFilename;
    let folder;

    if (req.method === 'OPTIONS') {
      optionsHelper.doOptions(res);
    } else if (req.method === 'POST') {
      res.set('Access-Control-Allow-Origin', '*');

      const busboy = new Busboy({ headers: req.headers });
      const uploads = [];

      busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
        uploadedFilename = `${folder}^${filename}`;

        const filepath = path.join(os.tmpdir(), uploadedFilename);
        uploads.push({ file: filepath, filename: filename, folder: folder });
        file.pipe(fs.createWriteStream(filepath));
      });

      busboy.on('field', (fieldname, val) => {
        if (fieldname === 'folder') {
          folder = val;
        } 
      });

      busboy.on('finish', () => {
        if (uploads.length === 0) {
          res.end('no files found');
        }
        for (let i = 0; i < uploads.length; i++) {
          const upload = uploads[i];
          const file = upload.file;

          task.uploadFile(helpers.fbAdmin, upload.folder, upload.file, uploadedFilename).then(downloadLink => {
            res.write(`${downloadLink}\n`);
            fs.unlinkSync(file);
            res.end();
          });
        }
      });
      busboy.end(req.rawBody);
    } else {
      // Client error - only support POST
      res.status(405).end();
    }
  } catch (e) {
    console.error(e);
    res.sendStatus(500);
  }
});

const api = functions.https.onRequest(app);

module.exports = {
  api
;

upload-file-to-storage.js

exports.uploadFile = (fbAdmin, folder, filepath, filename) => {
  // get the bucket to upload to
  const bucket = fbAdmin.storage().bucket(); //`venture-spec-sheet.appspot.com/${folder}`

const uuid = uuid();
  // Uploads a local file to the bucket
  return bucket
    .upload(filepath, {
      gzip: true,
      metadata: {
        //destination: `/${folder}/${filename}`,
        cacheControl: 'public, max-age=31536000',
        firebaseStorageDownloadTokens: uuid
      }
    })
    .then(() => {
      const d = new Date();
      const expires = d.setFullYear(d.getFullYear() + 50);

      // get file from the bucket
      const myFile = fbAdmin
        .storage()
        .bucket()
        .file(filename);

      // generate a download link and return it
      return myFile.getSignedUrl({ action: 'read', expires: expires }).then(urls => {
        const signedUrl = urls[0];
        return signedUrl;
      });
    });
};

I've tried a few things

Setting the bucket name to default and a folder. This resulted in a server error.

const bucket = fbAdmin.storage().bucket(`${defaultName}/${folder}`); 

Setting the bucket name to the folder. This resulted in a server error.

const bucket = fbAdmin.storage().bucket(folder); 

And, I've also tried using the destination property of uploadOptions. But this still puts the file in the default bucket.

    .upload(filepath, {
      gzip: true,
      metadata: {
        destination: `${folder}/${filename}`, // and /${folder}/${filename}
      }
    })

Is it possible to upload to a folder using the Admin SDK?

E.g. I want to upload a file so that is is placed in a named "folder".

I.e. so I can reference the file at the path: bucket/folder/file.jpg

In the example below, each "folder" is named with a firebase key.

enter image description here

like image 375
Kildareflare Avatar asked Feb 28 '19 19:02

Kildareflare


1 Answers

Found the problem. I stupidly declared the destination option in the wrong place.

Instead of in the metadata object:

 return bucket
    .upload(filepath, {
      gzip: true,
      metadata: {
        destination: `${folder}/${filename}`,
        cacheControl: 'public, max-age=31536000',
        firebaseStorageDownloadTokens: uuid
      }
    })

It should have been on the options object:

 return bucket
    .upload(filepath, {
      gzip: true,
      destination: `${folder}/${filename}`,
      metadata: {   
        cacheControl: 'public, max-age=31536000',
        firebaseStorageDownloadTokens: uuid
      }
    })

With this change made the file now gets uploaded into a named "folder".

like image 178
Kildareflare Avatar answered Sep 17 '22 20:09

Kildareflare