Monday, March 7, 2022

[FIXED] How to retrieve image's name to store in image table after uploading on AWS S3 using Laravel?

Issue

I am uploading images on AWS S3 using laravel8 and package (league/flysystem-aws-s3-v3) with following code.

    $image = $request->file('image');
    $data['image_name'] = time().'.'.$image->getClientOriginalExtension();
    $data['path'] = Storage::disk('s3')->put('images', $request->image);
    $data['ab'] = Storage::disk('s3')->url($data['path']);
    return $data;

After successful upload, Above code is returing

{
 "image_name": "1644919540.jpg",
 "path": "images/SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg",
 "ab": "https://abc-user-images.s3.ap-south-1.amazonaws.com/images/SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg"
}

I need to retrieve only the image's name in the path variable like

"path":"SYrPZTqDQgSuIvqBZzdrSX5JEmRQhwEC3muYDvJO.jpg"

  

              

Solution

Need to use ltrim(string,charlist)

$image = $request->file('image');
$data['image_name'] = time().'.'.$image->getClientOriginalExtension();
$data['path'] = Storage::disk('s3')->put('images', $request->image);
$data['ab'] = Storage::disk('s3')->url($data['path']);
$image = ltrim($data['path'],"images/");
return $image;

The $image have what you need.



Answered By - sujit prasad

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.