Thomas David Kehoe
Thomas David Kehoe

Reputation: 10960

Cloud Functions methods for writing files to Storage?

A crucial difference between Firestore and Storage is that Firestore stores data (objects, arrays, strings, etc.) when Storage stores files. How we handle files in Cloud Functions is very different from how we handle data.

I use Node to call APIs, get files, and write the files to Storage. Node is hard. Are there easy methods to write files to Storage?

I found an easy way to write files to Storage from Cloud Functions. [Sounds of thin ice creaking.]

index.ts

import { getStorage, ref, uploadBytes, uploadString, connectStorageEmulator } from "firebase/storage";

let file = await got('https://audio.oxforddictionaries.com/en/mp3/winter__us_2.mp3');
await uploadBytes(storageRef, file['rawBody'], metadata);

The ['rawBody'] parameter overrides the requirement that uploadBytes only uploads files.

That's cringy, right? Is there a better way?

Upvotes: 1

Views: 888

Answers (2)

Ronnie Smith
Ronnie Smith

Reputation: 18595

Google says

Cloud Client Libraries are the recommended option for accessing Cloud APIs programmatically, where available.

In terms of Cloud Storage (and every other GCP product), they provide a whole bunch of working examples that you can practically copy/paste into your code.

Node.js Cloud Client Libraries

Specifically, see Google Cloud Storage: Node.js Client

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

The methods for accessing Cloud Storage in Cloud Functions for Firebase comes from the Firebase Admin SDK for Node.js and are a thin wrapper around the Cloud Storage SDK for Node.js. There is no friendly Firebase wrapper for these, so my usual approach is to search for (non Cloud Functions specific, non Firebase specific) Node.js example.

Upvotes: 2

Related Questions