9951 explained code solutions for 126 technologies


php-awsHow can I connect to Amazon Web Services (AWS) S3 using PHP?


To connect to Amazon Web Services (AWS) S3 using PHP, the following steps can be followed:

  1. Install the AWS SDK for PHP using Composer:
composer require aws/aws-sdk-php
  1. Create an IAM user with the appropriate access rights to access the S3 buckets.

  2. Create an AWS access key and secret access key for the user.

  3. Configure the AWS SDK for PHP with the access key and secret access key:

$s3 = new Aws\S3\S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => [
        'key'    => 'AKIAIOSFODNN7EXAMPLE',
        'secret' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
    ],
]);
  1. Use the SDK to access the S3 buckets:
$result = $s3->listBuckets();

// Each Bucket is represented by a KeyValuePair object
foreach ($result['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}
  1. Output:
my-bucket
my-other-bucket
  1. For more information, please refer to the AWS SDK for PHP documentation.

Edit this code on GitHub