Home > Software engineering >  Laravel How to get size of a file stored in AWS S3
Laravel How to get size of a file stored in AWS S3

Time:09-11

In my Laravel project, before I upload an image file, I resize it to a max width (about 1000 px). Therefore, the size of the file that will be stored in AWS S3 bucket will be smaller that the original file size.

How can I get the new size of the file that I have uploaded to AWS S3? I have the full path of the file in AWS S3 bucket, e.g. client/attachments/130/20220910213218631d0262ccde0-pexels-christina-morillo-1181686.jpg

I use Image/Intervention package to resize the image, e.g.

$image = Image::make($request->file('file'))
    ->resize(
        $width,
        null,
        function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        }
    );

$ok = Storage::disk('s3')->put($filepath, $image->stream()); 

CodePudding user response:

$image = Image::make($request->file('file'))
    ->resize(
        $width,
        null,
        function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        }
    );

$resizedWidth = $image->width();
$resizedHeight = $image->height();
$resizedFilesize = $image->filesize();

Details in their docs: https://image.intervention.io/v2/api/filesize

CodePudding user response:

You can get size of stored file in s3 simply by this

use Illuminate\Support\Facades\Storage;

public function get_size($file_path)
{
    return Storage::disk('s3')->size($file_path);
}
  • Related