composer require league/flysystem-aws-s3-v3
use AwsS3S3Client;
// first connection with aws
$s3client = S3Client::factory ( [
'credentials' => [
'key' => 'Your aws key',
'secret' => 'Your aws secret key'
],
'region' => 'eu-west-1',
'version' => '2006-03-01'
] );
try {
$image_name_array=[];
$image_object=[];
// Get List Of S3 Object in specified folder name
$contents = $s3client->listObjects([
'Bucket' => 'bucket', // bucket name
'Prefix' => 'public/uploads' // folder name
]);
foreach ($contents['Contents'] as $content) {
// push key of s3 object in array
array_push($image_name_array,$content['Key']);
// print_r($content['Key']);
}
foreach($image_name_array as $image_key){
// get the image file from given key
$result = $s3client->getObject([
'Bucket' => 'chemco',
'Key' => $image_key,
]);
header("Content-Type: {$result['ContentType']}");
// store the image file in array
array_push($image_object,$result['Body']);
}
// You can see the image
foreach($image_object as $key=>$obj){
if($key==1){
echo $obj;
return $obj;
}
}
} catch (Exception $exception) {
echo "Failed to list objects in chemco with error: " . $exception->getMessage();
exit("Please fix error with listing objects before continuing.");
}
use IlluminateSupportFacadesStorage;
$file_upload = Storage::disk('s3')->put('/folder_name', $request->file);
//must have in .env
AWS_ACCESS_KEY_ID=access_key
AWS_SECRET_ACCESS_KEY=secret_key
AWS_DEFAULT_REGION=region
AWS_BUCKET=bucket_name
AWS_USE_PATH_STYLE_ENDPOINT=false