简体   繁体   中英

Getting image from REST API and uploading it to Firebase Cloud Storage using Firebase Cloud Functions

What I need to accomplish

I need to fetch images from rest API (base64) and write individual images into Firebase Cloud Storage. After image is successfully written, write log into Firebase Realtime Database.

What's the problem

Well, I initialized Firebase app (Cloud Functions) with AdminSDK, because I need some admin features (for instance bypassing Realtime Database rules). According to Firebase documentation, if I use AdminSDK, then for manipulating Cloud Storage I must use "@google-cloud/storage".

So I looked updocumentation for "@google-cloud/storage" and found out that for uploading files I have to call method ".upload" and specify path to file as argument.

The problem is I don't have path to that file because I have it as base64 string . I can't generate path for it, because nodeJS doesn't have method URL.createObjectURL and polyfiling that method is impossible. Also writing that image to filesystem is not solution, because Cloud Functions is read-only environment.

Things I tried

  • Polyfiling URL.createObjectURL, but it didn't work

  • Use nodeJS module FS to write images from REST API to filesystem, upload them to Cloud Storage and then remove them.

Is there any solution to this problem, or some recommended way to do this kind of functionality?

Solution

My second solution (writing temporary file using fs) was almost right, but I forgot to specify file destination to /tmp . In Firebase Cloud Functions is everything but /tmp read-only storage.

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