Search code examples
laravelamazon-web-servicesamazon-s3bucket

upload to s3 bucket folder with laravel


I am currently using this packages for amazon file uploads and it works, the only problem is i don't know how to specify a folder in my chosen bucket.

Package Used - "aws/aws-sdk-php": "~3.0",

This is how i currently upload to the bucket

$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image = $request->file('image');
$t = Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');
$imageName = Storage::disk('s3')->url($imageName);

Solution

  • You need to create a path and use in put method instead of $imageName to store in that particular path of bucket. It'll create the folder itself according to path.

    For e.g. if you set the path as $path = "folder_1/folder_2/file.pdf", s3 driver will store file.pdf in folder_2 which is inside folder_1.

    $imageName = time().'.'.$request->image->getClientOriginalExtension();
    $image = $request->file('image');
    
    //image stored in folder name image_folder
    $path = "image_folder/".$imageName;
    
    $t = Storage::disk('s3')->put($path, file_get_contents($image));