AWS SDK for PHP
Setup AWS Credentials
- If it does not already exist, make an
.aws
directory in the current users home (~/
) directory.
mkdir ~/.aws
- Create the
credentials
file inside the.aws
directory:
nano ~/.aws/credentials
- Put the AWS account login credentials into the file:
[default]
aws_access_key_id = ZKOLJQC7IB5JHEKCWLAV
aws_secret_access_key = D92I6o89ks2F3g2PFn24hGSCmoK9FByeDZyhDAES
S3 Test App
- Create the
aws_test.php
file inside the current users home (~/
) directory.
nano ~/aws_test.php
- Put the AWS test app code into the file:
<?php
// The general Composer autoloader.
require 'vendor/autoload.php';
// Use the S3 and error classes.
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Use the us-east-2 region and latest version of each client.
// NOTE: The AWS login credentials are stored in the `~/.aws/credentials` file.
// See: https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_profiles.html
$sharedConfig = [
'profile' => 'default',
'region' => 'us-east-2',
'version' => 'latest'
];
// Create an SDK class (login to AWS).
$sdk = new Aws\Sdk($sharedConfig);
// Use the AWS\SDK class to create the S3 Client object.
$s3Client = $sdk->createS3();
// List the S3 buckets for the account.
$buckets_result = $s3Client->listBuckets();
foreach ($buckets_result['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}
- Run the test app in Terminal on the command line.
php ~/aws_test.php
View Buckets
// List all the S3 buckets for the current account.
$buckets_result = $s3Client->listBuckets();
foreach ($buckets_result['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}
View Bucket Contents
The following methods return the key
for each object. A key is the full object name including the prefix (file path) and file name for the object. Object keys ending with a slash (/
) are directories (empty objects made to mark storage levels). Objects with no slash have actual file data.
List all bucket objects
// List all objects in a bucket.
$bucket_name = 'my_bucket';
$expression = '[CommonPrefixes[].Prefix, Contents[].Key][]';
$results = $s3Client->getPaginator('ListObjects', [
'Bucket' => $bucket_name
]);
foreach ($results->search($expression) as $item) {
echo $item . "\n";
}
List bucket root objects
// List all prefixes (directories) and objects (files) in the root of a bucket.
$bucket_name = 'my_bucket';
$expression = '[CommonPrefixes[].Prefix, Contents[].Key][]';
$results = $s3Client->getPaginator('ListObjects', [
'Bucket' => $bucket_name,
'Delimiter' => '/'
]);
foreach ($results->search($expression) as $item) {
echo $item . "\n";
}
List deep bucket objects
// List all prefixes (directories) and objects (files) at a specified path in the bucket.
$bucket_name = 'my_bucket';
$bucket_path = 'deep/path/name';
$expression = '[CommonPrefixes[].Prefix, Contents[].Key][]';
$results = $s3Client->getPaginator('ListObjects', [
'Bucket' => $bucket_name,
'Delimiter' => '/',
'Prefix' => $bucket_path
]);
foreach ($results->search($expression) as $item) {
if ($item != $bucket_path) {
echo $item . "\n";
}
}