kabilesh's avatar

GET IMAGE ETAG VALUE IN S3 BUCKET USING LARAVEL

I want to get Image etag value from the uploaded image using s3.

I can get lastModified value of the Uploaded Image file using the following code

$time =Storage::disk('s3')->lastModified($filenametostore)

My question is how to get the etag value?

0 likes
5 replies
rodrigo.pedra's avatar

I don think it is possible using the built-in Storage mechanism. As it internally uses the Flysytem library (https://flysystem.thephpleague.com/docs/adapter/aws-s3/) and its documentation has nothing about retrieving a file etag.

One thing you could try is getting the file URL and making a request to it with Guzzle and checking the response headers.

Might be an overhead but I can't think of any other method to do it.

kabilesh's avatar

Thank you can u please send me the link for requesting with Guuzzle. And can u please explain what is the use of Etag and when it is needed in S3 Bucket ?

rodrigo.pedra's avatar
Level 56

Guzzle is a HTTP client implemented in PHP. So you can make requests to other servers from your PHP code:

http://docs.guzzlephp.org/en/stable/

If you never used it and find it a bit cumbersome you can try using Zttp which is a simple wrapper around Guzzle:

https://github.com/kitetail/zttp/

I actually don't manage the etag myself. I know it is generally used to improve browser cache for static assets (images, css, etc.), but I use the defaults from S3 or CloudFront.

Just mentioned because you asked how to "Get image etag value in S3 bucket using Laravel".

kabilesh's avatar

$path = Storage::disk('s3')->getAdapter();

    $client = $path->getClient();

    $object = $client->headObject([
    'Bucket' => 'assets.hhh.com',
    'Key' => $empfilenametostore,
      
     
    ]);

    $s = array();

    $s[] = $object;

    foreach($s as $s1)

{

        $etag= $s1['ETag'];
        
    }

 echo $etag;

This is the way I got the ETag I hope it will help someone

3 likes

Please or to participate in this conversation.