Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

get s3 object from aws in laravel

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.");
}
Source by packagist.org #
 
PREVIOUS NEXT
Tagged: #object #aws #laravel
ADD COMMENT
Topic
Name
6+5 =