PHP code example of fedemotta / yii2-aws-sdk

1. Go to this page and download the library: Download fedemotta/yii2-aws-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fedemotta / yii2-aws-sdk example snippets


return [
    //....
    'components' => [
        'awssdk' => [
            'class' => 'fedemotta\awssdk\AwsSdk',
            'credentials' => [ //you can use a different method to grant access
                'key' => 'your-aws-key',
                'secret' => 'your-aws-secret',
            ],
            'region' => 'your-aws-region', //i.e.: 'us-east-1'
            'version' => 'your-aws-version', //i.e.: 'latest'
        ],
    ],
];

$aws = Yii::$app->awssdk->getAwsSdk();
$elb = $aws->createElasticloadbalancing();
$load_balancers = $elb->describeLoadBalancers()->toArray();
if (isset($load_balancers['LoadBalancerDescriptions'])){
    foreach ($load_balancers['LoadBalancerDescriptions'] as $balancer){
        if (isset($balancer['LoadBalancerName'])){ 
            echo $balancer['LoadBalancerName'];
        }
    }
}

//specify the region if it is different than the main configuration region
Yii::$app->awssdk->region = 'sa-east-1';
$aws = Yii::$app->awssdk->getAwsSdk();
//use s3
$s3 = $aws->createS3();
$result = $s3->listObjects(['Bucket' => 'your-bucket-id',"Prefix" => "your-path"])->toArray();
//get the last object from s3
$object = end($result['Contents']);
$key = $object['Key'];
$file = $s3->getObject([
    'Bucket' => 'your-bucket-id',
    'Key' => $key
]);
//download the file
header('Content-Type: ' . $file['ContentType']);
echo $file['Body'];

php composer.phar