Firebase Storage clone uploaded file and create new file without downloading/uploading possible? - firebase

let's say I have an image /path/image1.png in the firebase storage. I want to copy this image and create a new image with a different name but the same content as /path/image2.png. I'm using AngularFire. How will I achieve this? Please help

Firebase Storage (nowadays called Cloud Storage for Firebase) is a set of client-side SDKs that allow you to access Cloud Storage from within your application.
There is no Firebase API to create a copy of a file in Cloud Storage. It's a valid use-case though, so I'd recommend you file a feature request for it.
In the meantime, the two options I can think of are:
Read the data to the client, and write it to the new location. This is definitely wasting bandwidth for the client, so I'd only consider it if your files are quite small, and your clients have a decent connection.
Use one of the (non-Firebase) server-side SDKs for Cloud Storage to create a copy of the file. For example, you could wrap this Node.js code in a callable Cloud Function and then call that from your application code.
await storage
.bucket(srcBucketName)
.file(srcFilename)
.copy(storage.bucket(destBucketName).file(destFilename));

Related

Where to store Google Service Account Key while using Google Firebase Functions

Using Google Firebase Functions as a backend of the small application.
Functions are accessing to the Firestore and Realtime database, therefore they need service account credentials file.
On the other hand, I'm trying to automate the deployment of the functions using Github Actions.
Currently I places the credentials file inside the repository. I know that it's not secure.
What is the proper way of storing service account credentials file in this case?
Firebase projects, are, in effect, Google Cloud Platform projects.
More specifically, when you create a Firebase project, an associated Google Cloud Platform project is created for it.
Therefore the process for storing credentials is the same as in Cloud Platform, which is to say in a file, somewhere relatively safe.
This file should be accessible to your Function if it is required, and should either have its path specified as part of an environment variable or explicitly declared in code.
You are already storing it the proper way, because the improper way would be to insert the contents of the JSON file directly into code.
To prevent others from seeing the contents of the JSON file, simply set the respository as private.

How can I upload files to Cloud Storage through Cloud Functions and use Firestore to control access to Cloud Storage?

I'm trying to implement a system that allows react-native clients to upload files to a specific folder in Cloud Storage and allows clients to download from them. I can't do this directly from the client because I first need to query Firestore to validate that the user is 'friends' with the owner of the folder which will allow for read/write permissions.
I decided to use Cloud Functions as a middle layer to encapsulate this business logic and I expected to also be able to use it as a middle layer to upload the files. However, I feel like I may be misunderstanding how to best use these services together to solve this problem.
Current Plan:
Client uploads file to Cloud Function (assuming they are permitted after Cloud Function queries Firestore and validates)
Cloud Function uploads file to Cloud Storage
Client can then request file from Cloud Function, which validates permissions using Firestore and downloads file from CloudStorage
Cloud Function sends file to client
Questions:
Can/Should I use Cloud Functions in this way as a middle layer to upload files after validating permissions store in Firestore?
Is there an alternative solution using firebase that would mitigate the 10MB download limit with Cloud Functions but still allow me to authenticate uploads/downloads to and from Cloud Storage using my custom business logic on relationships in Firestore?
Any help or guidance here is appreciated! I'm very new to firebase, cloud architecture, and architecture in general.
This is definitely a valid and technically feasible approach. Whether you should do it, only you can determine of course.
An alternative is to use Firebase Storage's security rules to enforce the access control on the files. But you won't be able to read anything from Firestore in those rules, so you'll have to ensure everything needed to grant/deny access is in the path of the requested file, in the file's metadata, and/or in the user's token.
Even if you implement the download of files in Cloud Functions, you can still perform uploads through Firebase. You could in that case for example have the user write to a temporary location, which then triggers a Cloud Function that can do whatever additional checks you want on the file.

Flutter: List of files in a folder in Firebase Cloud Storage

I need to load some image files from Cloud Storage. For a given folder I need to check what images (if at all) are available in it. It seems like the firebase client library (https://pub.dev/packages/firebase_storage) doesn't have a simple way of doing that. I am sure ios/android clients have ref().list() command. Is there any way to do that easily in Flutter or I have to write a bridge to native methods?
The methods to list files in Cloud Storage were only recently added to the Firebase SDKs, so it seems likely that they haven't been ported to the FlutterFire library yet.
You'll indeed either have to implement the interop yourself (in which case a PR back to the repo is highly appreciated), or wait for someone else to implement this feature request.
A final alternative is to not depend on this (relatively new) API, and store the list of file paths/download URLs in another data store, such as the Firebase Realtime Database, or Cloud Firestore.

How to use firebase storage along with custom server?

I am planning to use firebase storage as storage bucket for images for my app.
Shall I use client side firebase SDK to upload images directly to firebase or, shall I send image to my server first and use firebase Admin-sdk and let the server upload image to firebase? Also I have other data along with image which client will send, that server needs to handle.
EDIT: I was confused about one thing, if I use firebase admin-sdk, first my image need to upload to the server and then server will send it to firebase storage, won't it double the upload time?
Both are valid options, and neither is pertinently better than the other.
I typically prefer using the Firebase SDK to upload to Cloud Storage, since it saves me from having to come up with my own client-side code and handling things like network detection, retries, etc. I then often write metadata about the file to the Firebase database (either the Realtime Database, or Cloud Firestore) and use that to trigger Cloud Functions to do any backend processing that is needed on the image.
But it's equally valid to write your own server-side endpoint that does the processing of the image, and post to that from within your app.

Need python files stored in Google Database to compile in Google Cloud Engine and return data to an IOS App

My Current Plan:
I'm currently creating an IOS App that will access/change java/python files that are stored in the Google Cloud Storage. Once confirmed the App will talk with App Engine that will have a Compute Engine VM receive files and compile them. Once compiled have the result returned back to the IOS App
Is there any better or easier method to achieve this task? Should I use firebase or Google Cloud Functions? Would it be any help
Currently, I'm lost how to design and have requests sent between many platforms.
It would also depend on what type of data processing you are doing to the files in Cloud Storage. Ideally you would want to avoid as many "hops" between services as possible. You could do everything via Cloud Functions and listen on GCS Triggers. You can think of Cloud Functions as a sudo App Engine Backend to use for quick request handling.
Use Cloud Functions to respond to events from Cloud Storage or Firebase Storage to process files immediately after upload
If you are already using Firebase, it would be better to stay within their ecosystem as much as possible. If you are doing bigger or more intensive data processing you might want to look at different options.
With more information and current pain points, we may be able to offer more insight.

Resources