Diskdrive
Diskdrive

Reputation: 18865

uploading raw data to a firebase storage bucket in a Google Cloud function?

I have a Google Cloud Function and I'm trying to save some data into Firebase storage. I'm using the firebase-admin package to interact with Firebase.

I'm reading through the documentation (https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-nodejs) and it seems to have clear instructions on how to upload files if the file is on your local computer.

// 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',
  },
});

In my case through, I have a Google Cloud Function which will be fed some data in the postbody and I want to save that data over to the Firebase bucket.

How do I do this? The upload method only seems to specify a filepath and doesn't have a data parameter.

Upvotes: 4

Views: 3203

Answers (2)

Raine Revere
Raine Revere

Reputation: 33775

Use the save method:

storage
  .bucket('gs://myapp.appspot.com')
  .file('dest/path/in/bucket')
  .save('This will get stored in my storage bucket.', {
    gzip: true,
    contentType: 'text/plain'
  })

Upvotes: 5

Doug Stevenson
Doug Stevenson

Reputation: 317828

The API docs for Bucket.upload() state that it's just a wrapper around File.createWriteStream(). This method will create a WritableStream that you can use upload data that's already in memory. You will deal with this stream just like you would any other stream in Node. There is sample code in the API docs.

Upvotes: 3

Related Questions