Chapter 20 - How to fetch and delete an image from Firebase Storage using Laravel PHP project

In this chapter, we will learn how to fetch an image and delete an image from the Firebase Storage using the Laravel PHP project with the kreait/laravel-firebase package.

First of all we assume that you have read our previous chapter on how to upload an image to the Firebase Storage from the Laravel PHP project using the kreait/laravel-firebase package. Chapter 19

In below example, we have "Students" folder in the root of the bucket. And inside the folder, we have a png image with document id of the student.


So you have the path now "Students/defT5uT7SDu9K5RFtIdl.png" Now we will fetch the images from the path. And this can be achieved by following code

 $expiresAt = new \DateTime('today');  
 $imageReference = app('firebase.storage')->getBucket()->object("Students/defT5uT7SDu9K5RFtIdl.png");  
 if($imageReference->exists())  
   $image = $imageReference->signedUrl($expiresAt);  

This $image would give you url of the image

 https://storage.googleapis.com/test-cc4d3.appspot.com/Students/defT5uT7SDu9K5RFtIdl.png?GoogleAccessId=XXXXXX&Expires=1596312000&Signature=XXXXXXX  

Hope you are clear and easy how can you fetch an image stored in firebase storage bucket.

Now lets see how can we delete this image

Consider the same image path "Students/defT5uT7SDu9K5RFtIdl.png", the delete code for this image will be as below

 $imageDeleted = app('firebase.storage')->getBucket()->object("Students/defT5uT7SDu9K5RFtIdl.png")->delete();  

This is very easy step to delete an image. Now how you can detect an image is already deleted and you don't make an call to fetch image that is already deleted. So best practice would be to store the image path in the same document. This way you can change path of any image for a document at any time. So consider below example


You create a new image field and store path in that field as string. So whenever you store a image you will update this field

 $Student->update([  
           ['path' => 'image', 'value' => $imagepath])]  
         ]);  

And on delete, simply make this field null

 $Student->update([  
           ['path' => 'image', 'value' => null])]  
         ]);  

So before fetching the image, just check this image is not null.

Hope, all stuff regarding image in Firebase Storage with Laravel PHP is clear. Other than this if you have any questions or doubts, please let us know in the comment section. And also know if you want a specific chapter on any topic, we would love to do that. So stayed tuned with us for more upcoming chapters on "Laravel with Firebase". And please keep supporting us by sharing this blog with all techie friends. Thanks for reading.



Post a Comment

9 Comments

  1. Replies
    1. Many thanks for your helpful snippets for fetching and deleting images. But I'm facing an issue while deleting. For example, I have an image 'abc.jpg' under 'Students' storage. Now I perform the following to delete:

      app('firebase.storage')->getBucket()->object('Students/abc.jpg')->delete();

      But I get the following error message:

      Google\Cloud\Core\Exception\NotFoundException
      { "error": { "code": 404, "message": "No such object: XYZ.appspot.com/Students/abc.jpg", "errors": [ { "message": "No such object: XYZ.appspot.com/Students/abc.jpg", "domain": "global", "reason": "notFound" } ] } }

      Here, I've replaced my default bucket name with something like 'XYZ'. But why it should include the bucket name, instead of just storage/imageName? Any suggestion please? Thanks in advance.

      Delete
    2. Hi Mizanur Islam Laskar, Thanks for reaching us.

      See, with current Free Plan of Firebase, they provide a single default bucket. But once you upgrade your plan, you can add many buckets. So currently if you don't send bucket name, then it will take default one, so bucket name is important when you have multiple buckets otherwise its okay for the single bucket.

      Regarding your code, there is nothing wrong. Only make sure Students/abc.jpg directory and file exists and make sure to remember its case sensitive.

      Hope it will help.

      Thanks for Reading. Keep sharing and supporting us.

      Delete
  2. i'm getting an error " Project Id not configured"
    and how do i get the list of all the files in my firebase storage?
    i'm trying to make carousel of pictures stored in firebase storage

    ReplyDelete
  3. Hi,
    Regarding error - firebase cannot determine project id

    Probable issue - firebase credentials json file is not properly placed.

    Read our Chapter 4: https://laravelwithfirebase.blogspot.com/2020/02/chapter-4-introduction-to-firebase-admin-sdk-for-php-with-its-usage.html

    Hope this helps u and please help us by following us.

    Thanks


    ReplyDelete
  4. unable to get storage download url . It needs access token. How can i generate access token or get download url.

    ReplyDelete
    Replies
    1. Hi MaD,

      Thanks for reaching out us.
      Read the last comment of this chapter https://laravelwithfirebase.blogspot.com/2020/07/chapter-19-how-to-upload-image-to-firebase-storage-from-laravel-php.html?showComment=1610698874483&m=1#c2485636341416697132

      Hope it helps. Follow us to help us.

      Delete
  5. Error
    Code ExpiredToken
    The provided token has expired.
    Request signature expired at: 2021-04-16T00:00:00+00:00

    ReplyDelete
    Replies
    1. Hi

      Yes the url of image will expire at the time mentioned while you make a retrieve image call.

      Read this
      https://laravelwithfirebase.blogspot.com/2020/07/chapter-19-how-to-upload-image-to-firebase-storage-from-laravel-php.html?showComment=1610698874483&m=1#c2485636341416697132

      Delete