简体   繁体   中英

PHP Google App Engine permanently delete Image from Cloud Storage

I'm using GAE version 1.9.0 and I want to delete an image from the data storage and upload another image to its location. This is how I'm doing it right now.

unlink("gs://my_storage/images/test.jpg");
move_uploaded_file($_FILES['image']['tmp_name'],'gs://my_storage/images/test.jpg');

And then I want to get the Image serving URL of the latest uploaded image, and I do it like this.

$image_link = CloudStorageTools::getImageServingUrl("gs://my_storage/images/test.jpg");

The issue is, when the name of the deleted image("test.jpg") and the uploaded image("test.jpg") is the same, the old file is served when I call for the newly uploaded file(I think it is cached.)

Is there anyway I can permanently delete this file without caching it?

You should probably delete the original serving URL before creating another with the same name.

There's a deleteImageServingUrl() method in CloudStorageTools that you can use to do this.

Here it is how to do in php laravel.

 $object = $post_media->media_cloud;
 $objectname = substr($object,48,100); 
 $bucket = Storage::disk('gcs')->delete($objectname);
  1. Here in $object i get google cloud image url from db
  2. Then we take only object name from that url, by substr.
  3. Since you have given in your config Storage class as Storage::disk('gcs')

so this will call the function delete by taking the objectname .

Hope it helps anyone.

Note : For multiple images either pass an array of objects, or repeat it foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM