AWS SDK for PHP
The AWS SDK for PHP
Getting started
- Create a service account.
- Assign the service account the roles required for your project. For more information about roles, see the Identity and Access Management documentation.
- Create a static access key.
Note
A service account is only allowed to view a list of buckets in the folder it was created in.
A service account can perform actions with objects in buckets that are created in folders different from the service account folder. To enable this, assign the service account roles for the appropriate folder or its bucket.
Installation
To install the AWS SDK for PHP, follow the guide
Setup
-
In your home folder, create a file named
.aws/credentials
with this static key:[default] aws_access_key_id = <key_ID> aws_secret_access_key = <secret_key>
-
If using a LAMP image from Cloud Marketplace, add the
HOME
environment variable referring to your home folder to the Apachehttpd.conf
configuration file (apache2.conf
for Debian and Ubuntu):SetEnv HOME <home_folder>
For more information about the location and name of the Apache configuration file for different operating systems, see the Apache HTTP Server Wiki
.
You can use the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables instead of the .aws/credentials
file.
Use the Object Storage address to access storage.yandexcloud.net
.
Code snippets
Make sure to include your project path in the autoload.php
file's connection string:
<?php
// We assume that the AWS SDK is installed via Composer
require "<project_path>/vendor/autoload.php";
use Aws\S3\S3Client;
$s3 = new S3Client([
"version" => "latest",
"endpoint" => "https://storage.yandexcloud.net",
"region" => "ru-central1",
]);
$buckets = $s3->listBuckets();
$bucket_count = 1;
echo "<b>Well, here are your buckets:</b></br></br>";
foreach ($buckets["Buckets"] as $bucket) {
echo $bucket_count . ". " . $bucket["Name"] . "</br>";
$bucket_count++;
}
?>
See also the code samples