function sample that uses firebase storage ref
See original GitHub issueWe 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:
- Created 6 years ago
- Comments:11 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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’sstorage.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 astorage.ref()
allows you to do.If you use the
@google-cloud/storage
library directly, you’ll need to initialize it with:or you can use the
firebase-admin
API to get an initialized copy of the API with:The Admin SDK uses the
@google-cloud/storage
directly, so you’ll use “files” instead. E.g.