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
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.