AlexBains
AlexBains

Reputation: 295

Node.js upload images to firebase storage

I am trying to upload files(images) in my firebase cloud function. After reading many posts, I found that the only way that I can do this is to use Google Cloud Platform (GCP) API, and here is my code:

const storage = require('@google-cloud/storage')();
var bucket = storage.bucket('myBucketName');
var buffer = bufferRepresentMyImage;
// what next?

I found the following code from GCP's API:

bucket.upload(filename, (err, file) => {
    // code goes here
});

However, it doesn't seem like will work since it never ask for file's data, and instead, the callback function returns a file. Can anyone please show me how to upload image with bucket?

Upvotes: 1

Views: 3145

Answers (1)

Shai Ben-Tovim
Shai Ben-Tovim

Reputation: 932

The remote destination filename is included in the options param. filename in your example (and this answer) is the local filename path.

let options = {
        destination: fileNameOnDestinationBucket,
        metadata: {                   // (optional)
            metadata: yourCustomMetadata,
            contentType: 'image/jpeg' // (example)
        }
    };

// with Promise:
bucket.upload(filename, options).then((response)=>{
    ...
});

// with callback:
bucket.upload(filename, options, (err, metadata, response)=>{
....
});

Upvotes: 2

Related Questions