Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get image object in s3 bucket

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.");
}
 
PREVIOUS NEXT
Tagged: #image #object #bucket
ADD COMMENT
Topic
Name
3+3 =