简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM