question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

function sample that uses firebase storage ref

See original GitHub issue

We need a function sample that simply creates a file in firebase storage. Current samples make a file on the server using mkdirp and use temp file path for uploading that file to storage.

I would assume doing something like this would be valid and straightforward but it does not work since admin.storage().bucket().ref(..) is not valid along with functions.storage.bucket().ref(...)

var fileRef = admin.storage().bucket().ref('myFolder/myfile.txt');
// Uint8Array
var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
fileRef .put(bytes).then(function(snapshot) {
  console.log('Uploaded an array!');
});

Here is a scenario and starting function :

export const newAccountCreated = functions.auth.user().onCreate(event => {
    const user = event.data;

    return admin.database().ref('users/' + user.uid).set({
        uid: user.uid,
        email: user.email
    }).then(() => {
       //Create a folder with user id and write data to info.txt file
        //this does not work
        var fileRef = admin.storage().bucket().ref(user.uid + '/info.txt');
        var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
        fileRef.put(bytes).then(() => {
           console.log('file created');
        })
    });
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
00hellocommented, Feb 15, 2018

I really feel that these types of things should be added to the documentation. It was only after going through the functions documentation and the storage documentation and looking at the different things that can be done with each did I ascertain that the storage function’s event.data could not have the same properties as the storage’s storage.ref().

It seems to me that this should be clearer in the function documentation. Because it’s easy to assume they should have the same properties. And it’s not easy to find out how to actually do those things with event.data that a storage.ref() allows you to do.

7reactions
inlinedcommented, Dec 5, 2017

If you use the @google-cloud/storage library directly, you’ll need to initialize it with:

import * as storageAPI from `@google-cloud/storage`
const storage = storageAPI(); // Auto initialize in Google environments

or you can use the firebase-admin API to get an initialized copy of the API with:

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

The Admin SDK uses the @google-cloud/storage directly, so you’ll use “files” instead. E.g.

const stream = bucket.file('myFolder/myfile.txt').createWriteStream();
stream.end(bytes)
// The stream finish event will let you know the upload has completed
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a Cloud Storage reference on Web - Firebase
To create a reference, get an instance of the Storage service using getStorage() then call ref() with the service as an argument. This...
Read more >
How to use the firebase.storage function in firebase - Snyk
To help you get started, we've selected a few firebase.storage examples, based on popular ways it is used in public projects.
Read more >
Firebase for Web: Firebase Storage | by Vrijraj Singh - Medium
Create a reference for Firebase Storage, In order to upload or download files, delete files, or get or update metadata, you must create...
Read more >
How to get a list of all files in Cloud Storage in a Firebase app?
With Cloud Functions you can use the Google Cloud Node package to do epic operations on Cloud Storage. Below is an example that...
Read more >
5 tips for Firebase Storage
1. References point to files ... StorageReference storageRef = FirebaseStorage.getInstance().reference().child("folderName/file.jpg");. References ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found