0

I need to resize images stored in firebase, but these image are in differents folders like this:

-bucket  
  -user
    -images

i need to go to images of the certains users and i would like to apply this script to resize the images

const storage = await getStorage();
const bucketName = `$socialmediaAPG.appspot.com/userId/images/`;
const bucket = storage.bucket(bucketName);
const files = await bucket.getFiles();
for (const file of files[0]) {
  const isImage = file.name.toLowerCase().includes('jpg') || file.name.toLowerCase().includes('jpeg');
  const isThumb = file.name.toLowerCase().includes('thumbs');
  if (isImage && !isThumb) {
    console.info(`Copying ${file.name}`);
    const metadata = await file.getMetadata();
    await file.copy(file.name);
    await file.setMetadata(metadata[0]);
  }
}

i am getting this error:

Copying userId/images/image1.jpg Error: No such object: socialmediaAPG.appspot.com/userId/images/image1.jpg

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
brunodia
  • 23
  • 7

1 Answers1

0

This seems wrong:

file.copy(file.name)

The argument you pass to file.copy is the destination file name, so you're trying to copy the file onto itself.

You probably want to add your custom thumbs suffix to the destination file name with something like

file.copy(file.name+"thumbs")

For the actual resizing, I recommend checking out other questions on resizing images in Cloud Storage.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • My code that I publish is not suitable to compress the size of the images? Can you help me with it please, I've been trying to do it for many days please! – brunodia Sep 21 '21 at 17:23
  • You're not compressing the image yet. The link in my answer shows many questions about compressing images, so I recommend reading those, and trying it yourself. If you've done that, edit your question to show what you tried on the actual resizing. – Frank van Puffelen Sep 21 '21 at 17:38
  • i wan to use imageMagick to reduce the size, or should i buy the extension to reduce? i read this https://stackoverflow.com/questions/63043201/resize-all-images-stored-in-firebase-storage – brunodia Sep 21 '21 at 18:03
  • 1
    The extension won't resize existing files in the bucket as far as I know. Calling ImageMagick to do the resizing is possible, but you'll have to write code for that. Lots of good articles here: https://www.google.com/search?q=use+imagemagick+to+resize+existing+images+in+cloud+storage. But the code in your question doesn't call any of that, so there's not much more I can do than what I already answered on (my guess) why you're guessting that error,. – Frank van Puffelen Sep 21 '21 at 18:58