Uploading files from Firebase Cloud Functions to Cloud Storage

firebase functions upload file to storage
upload file to firebase storage javascript
upload file to firebase storage nodejs
firebase-admin storage upload
firebase storage android
firebase storage upload multiple files
google cloud function write to cloud storage
firebase.storage is not a function

The documentation is too complex for me to understand. It shows how to download a file from Cloud Storage to Cloud Functions, manipulate the file, and then upload the new file to Cloud Storage. I just want to see the basic, minimum instructions for uploading a file from Cloud Functions to Cloud Storage. Why doesn't this work:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

exports.storage = functions.firestore.document('Test_Value').onUpdate((change, context) => {

  var metadata = {
    contentType: 'text',
  };

  admin.storage().ref().put( {'test': 'test'}, metadata)
  .then(function() {
    console.log("Document written.");
  })
  .catch(function(error) {
    console.error(error);
  })

});

The error message is admin.storage(...).ref is not a function. I'm guessing that firebase-admin includes Firestore but not Storage? Instead of firebase-admin should I use @google-cloud/storage? Why doesn't this work:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

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

admin.initializeApp();

exports.storage = functions.firestore.document('Test_Value').onUpdate((change, context) => {

  storage.bucket().upload( {'test': 'test'} , {
    metadata: {
      contentType: 'text'
    }
  })

});

I can't even deploy this code, the error message is

Error parsing triggers: Cannot find module './clone.js'

Apparently a npm module dependency is missing? But the module isn't called clone.js? I tried requiring child-process-promise, path, os, and fs; none fixed the missing clone.js error.

Why does admin.initializeApp(); lack parameters, when in my index.html file I have:

firebase.initializeApp({
    apiKey: 'swordfish',
    authDomain: 'myapp.firebaseapp.com',
    databaseURL: "https://myapp.firebaseio.com",
    projectId: 'myapp',
    storageBucket: "myapp.appspot.com"
  });

Another issue I'm seeing:

npm list -g --depth=0       

/Users/TDK/.nvm/versions/node/v6.11.2/lib
├── child_process@1.0.2
├── UNMET PEER DEPENDENCY  error: ENOENT: no such file or directory, open '/Users/TDK/.nvm/versions/node/v6.11.2/lib/node_modules/firebase-admin/package.json
├── firebase-functions@2.1.0
├── firebase-tools@6.0.1
├── firestore-backup-restore@1.3.1
├── fs@0.0.2
├── npm@6.4.1
├── npm-check@5.9.0
├── protractor@5.4.1
├── request@2.88.0
└── watson-developer-cloud@3.13.0

In other words, there's something wrong with firebase-admin, or with Node 6.11.2. Should I use a Node Version Manager to revert to an older version of Node?

I uploaded a file from my hard drive to Firebase Cloud Storage via Google Cloud Functions. First, I found the documentation for Google Cloud Functions bucket.upload.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {

  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage();
  const bucket = storage.bucket('myapp.appspot.com');

  const options = {
    destination: 'Test_Folder/hello_world.dog'
  };

  bucket.upload('hello_world.ogg', options).then(function(data) {
    const file = data[0];
  });

  return 0;
});

The first three lines are Cloud Functions boilerplate. The next line

exports.Storage = functions.firestore.document('Storage_Value').onUpdate((change, context) => {

creates the Cloud Function and sets the trigger. The next three lines are more Google Cloud boilerplate.

The rest of the code locates the file hello_world.ogg on my computer's hard drive in the functions folder of my project directory and uploads it to the directory Test_Folder and changes the name of the file to hello_world.dog in my Firebase Cloud Storage. This returns a promise, and the next line const file = data[0]; is unnecessary unless you want to do something else with the file.

Lastly we return 0;. This line does nothing except prevent the error message

Function returned undefined, expected Promise or Value

Upload Files on Web, is change in firebase realtime database's key1, a firebase cloud function will save data from database's key2 to firebase storage as a csv file. Find Cloud Storage Files now. Visit & Look for More Results!

See Introduction to the Admin Cloud Storage API for further details on how to use the Cloud Storage service in Firebase Admin SDK.

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    storageBucket: "<BUCKET_NAME>.appspot.com"
});

var bucket = admin.storage().bucket();

// 'bucket' is an object defined in the @google-cloud/storage library.
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/latest/storage/bucket
// for more details.

Regarding uploading objects, see Cloud Storage Documentation Uploading Objects sample code:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const filename = 'Local file to upload, e.g. ./local/path/to/file.txt';

// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filename, {
  // Support for HTTP requests made with `Accept-Encoding: gzip`
  gzip: true,
  metadata: {
    // Enable long-lived HTTP caching headers
    // Use only if the contents of the file will never change
    // (If the contents will change, use cacheControl: 'no-cache')
    cacheControl: 'public, max-age=31536000',
  },
});

console.log(`${filename} uploaded to ${bucketName}.`);

Uploading files from Firebase Cloud Functions to Cloud Storage , Otherwise, each thumb upload would trigger more uploads for ever and ever until the end of time… Leaving us tons of files to delete and a huge  Fast answers on Teoma.us! Find storing on the cloud. Content updated daily for storing on the cloud.

  1. Go to https://console.cloud.google.com/iam-admin/iam
  2. Click the pencil icon next to your App Engine default service account
  3. + ADD ANOTHER ROLE
  4. Add Cloud Functions Service Agent

In my specific use case, I needed to decode a base64 string into a byte array and then use that to save the image.

    import * as functions from 'firebase-functions';
    import * as admin from 'firebase-admin';    
    admin.initializeApp()

    function uploadProfileImage(imageBytes64Str: string): Promise<any> {

        const bucket = admin.storage().bucket()
        const imageBuffer = Buffer.from(imageBytes64Str, 'base64')
        const imageByteArray = new Uint8Array(imageBuffer);
        const file = bucket.file(`images/profile_photo.png`);

        return file.save(imageByteArray)
        .then(stuff => {
            return file.getSignedUrl({
                action: 'read',
                expires: '03-09-2500'
              })
        })
        .then(urls => {
            const url = urls[0];
            console.log(`Image url = ${url}`)
            return url
        })
        .catch(err => {
            console.log(`Unable to upload image ${err}`)
        })
    }

Then you can call the method like this and chain the calls.

    uploadProfileImage(image_bytes_here)
    .then(url => {
        //Do stuff with the url here        
    })

How to upload using firebase cloud functions - Tuan Ngo, The Firebase Realtime Database and Cloud Firestore are great for storing data, but they're not so good with files. Google Firebase Storage enables you to upload files directly from a browser to Cloud Storage. function uploadFile(file) {. Upload files in Cloud Storage Cloud Storage provides facilities for developers to quickly and easily upload files to the Google Cloud Storage bucket, which is provided or managed by Firebase. By default, Firebase Authentication is required for uploading files in Cloud Storage buckets.

Image Resizing in Firebase using Firebase Cloud Storage Function., Use busboy to directly parse multipart form-data and do something with it (​sample code). Upload directly Google Cloud Storage directly using  Trigger a function on Cloud Storage changes. Use functions.storage to create a function that handles Cloud Storage events. Depending on whether you want to scope your function to a specific Cloud Storage bucket or use the default bucket, use one of the following: functions.storage.object() to listen for object changes on the default storage bucket.

A Cloud Functions and Firebase Cloud Storage Example, Trigger the function by uploading a file to Cloud Storage. Costs. This tutorial uses billable components of Cloud Platform, including: Google Cloud Functions  Firebase uploads files to the Google Cloud Storage (part of the Google Cloud Platform. Unless configured otherwise, the files that are uploaded to Cloud Storage will also be accessible by App

Introduction, Trigger Firebase Storage Image Resize with Cloud Functions. Aug 05, 2019 · 7 minutes read File Name: the uploaded file name. Bucket Directory: the name of​  Cloud Storage allows developers to quickly and easily upload files to a Google Cloud Storage bucket provided and managed by Firebase. Since the default Google App Engine app and Firebase share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible as well.

Comments