I need some users to be able to upload files to firebase storage, but they're non tech-savvy, so I will let them upload to drive first, and mirror drive from storage. The thing is, I can't figure out how without having to host a server; google apps scripts can't access firebase storage easily (it can access both firebase databases though) and I need a server to use the google drive API, which could be done using firebase cloud functions, but I wonder if there is an easier alternative.
相关问题
- How can I force all files in a folder to be owned
- How can I force all files in a folder to be owned
- adding sha1 in firebase app fails with error
- Google Apps Script: testing doPost() with cURL
- firebase storage cors strange Behaviour
相关文章
- How can make folder with Firebase Cloud Functions
- Firestore Update a document field Using Rest API
- How to convert a FCM token to APNS token?
- How to allow access for importrange function via a
- Google app script trigger not working
- App not showing Notification receiving FCM when th
- Android Studio - Get Firebase token from GetIdToke
- Set Date/Time to 00:00:00
An image file can be uploaded to firebase storage using Apps Script.
There are 4 critical things that need to be done:
You will need to get an OAuth token, but you don't need an OAuth library.
Enable the "Google Cloud Storage JSON API"
This needs to be done for the Google account that will be uploading the file. The solution described here is for uploading a file where the Apps Script project, and the Firebase Storage are owned by the same Google Account.
Get the "bucket" name of your Firebase Storage
Go to your Firebase Storage settings. Look for "gs://your-bucket-name.appsspot.com" That is your bucket name. Don't include the "gs://" The bucket name needs to have the "appspot.com" part on the end.
Add the "https://www.googleapis.com/auth/devstorage.read_write" scope to the appsscript.json manifest file
From the script editor, choose "File" and "Project Properties" and click the "Scopes" tab. Copy out all the existing scopes, and paste them somewhere so that you can get them back.
From the script editor, choose "View" and "Show Manifest file." Click on the appsscript.json file to open it. Add all existing scopes, plus the "https://www.googleapis.com/auth/devstorage.read_write" scope to the manifest file.
Manifest file to look like this, except with your time zone and scopes.
Enable "Write" access in your Firebase storage rules
Get the OAuth token:
You can get the OAuth token with:
So, you don't need an OAuth library, you don't need any SDK, you don't need to do anything with client side code, you don't need special information from a firebase service account or the legacy Database Secret.
The Code:
This code uploads an image file from Google Drive to firebase Storage
Note! Any file over 5MB may need something a little different.
When the code is run for the first time, if the user has not enabled the API, there is a link provided in the extended error message in the response. So, you could modify the code to get the Cloud Console link from the error response. That link goes directly to the correct Cloud Console API, so the user doesn't need to know how to navigate their Cloud Console in order to find the correct API.
How to get the download url after the image has been uploaded