How can I resize all existing images in firebase storage?
A

1

6

Im using the Resize Images extension in Firebase. Before I had added this extension however I have already uploaded many images, they are in their respective post/ storage bucket and are linked via URL in the realtime database.

Is there any way that I can resize all the existing images inside their bucket? Preferably without having to re-assign their URLs with their posts?

Aliciaalick answered 20/2, 2020 at 22:54 Comment(0)
A
5

If you look at the code of the "Resize Images" extension, you will see that the Cloud Function that underlies the extension is triggered by a onFinalize event, which means:

When a new object (or a new generation of an existing object) is successfully created in the bucket. This includes copying or rewriting an existing object.

So, without rewriting/regenerating the existing images the Extension will not be triggered.


However, you could easily write your own Cloud Function that does the same thing but is triggered, for example, by a call to a specific URL (HTTPS cloud Function) or by creating a new document in a temporary Firestore Collection (background triggered CF).

This Cloud Function would execute the following steps:

  1. Get all the files of your bucket, see the getFiles() method of the Google Cloud Storage Node.js Client API. This method returns a GetFilesResponse object which is an Array of File instances.
  2. By looping over the array, for each file, check if the file has a corresponding resized image in the bucket (depending on the way you configured the Extension, the resized images may be in a specific folder)
  3. If a file does not have a corresponding resized image, execute the same business logic of the Extension Cloud Function for this File.

Depending on the number of images to treat, you may need to increase the timeout value and/or the allocated memory of your Cloud Function, see https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation

Anacardiaceous answered 24/2, 2020 at 11:12 Comment(2)
Or... you can literally just copy and paste the files in again. Download and re-upload to the bucket, or use the GCP transfer service.Astute
@David, doing that you will pay for the upload operations, so if there are a lot of files, it may not be the most appropriate approach.Anacardiaceous

© 2022 - 2024 — McMap. All rights reserved.