- 26 Sep 2024
- 7 Minutes to read
- Print
- PDF
AWS SDK for PHP v3 With Wasabi
- Updated on 26 Sep 2024
- 7 Minutes to read
- Print
- PDF
How do I use AWS SDK for PHP with Wasabi?
AWS SDK for PHP v2 is validated for use with Wasabi.
Using the PHP SDK
Execute the following steps:
Install the AWS SDK for PHP.
(optional) Configure an additional AWS CLI profile for Wasabi account using the Wasabi keys.
In this example, we set the profile name as "wasabi" in the "~/.aws/credentials" file. To help you use this SDK with Wasabi, we provided examples for both IAM and S3:
Setting the Credentials
Connecting to IAM and S3 Endpoints
Creating a User Using IAM
Creating a Bucket
Uploading an Object to the Bucket
Reading an Object From the Bucket
Deleting an Object From the Bucket
Additional examples are available from the AWS documentation.
The examples use Wasabi's us-east-1 storage region. For other Wasabi storage regions, use the appropriate Wasabi service URL.
Send all IAM requests to iam.wasabisys.com
Setting the Credentials
<?php
use Aws\S3\S3Client;
use Aws\Credentials\CredentialProvider;
use Aws\Exception\AwsException;
// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';
// Set up the profile credentials provider
$provider = CredentialProvider::ini('default', '~/.aws/credentials');
$provider = CredentialProvider::memoize($provider);
// Create an S3Client object
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => $provider,
'endpoint' => 'https://s3.wasabisys.com', // Change this to your specific endpoint
'use_path_style_endpoint' => true
]);
?>
Or:
<?php
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';
// Use the following code to use AWS credentials directly
$raw_credentials = [
'credentials' => [
'key' => '<access-key>',
'secret' => '<secret-key>'
],
'endpoint' => 'https://s3.wasabisys.com', // Change this to your specific endpoint
'region' => 'us-east-1', // Change this to your desired region
'version' => 'latest',
'use_path_style_endpoint' => true
];
?>
Connecting to IAM and S3 Endpoints
IAM
<?php
require 'vendor/autoload.php'; // Include the AWS SDK autoload.php file
use Aws\Iam\IamClient;
use Aws\Exception\AwsException;
$endpoint = 'https://iam.wasabisys.com';
$bucketName = 'gowtham-php2';
$region = 'us-east-2';
$iamclient = new IamClient([
'region' => $region,
'version' => 'latest',
'endpoint' => $endpoint,
'credentials' => [
'key' => '<Access-Key>',
'secret' => '<Secret-Key>',
],
]);
S3
<?php
require 'vendor/autoload.php'; // Include the AWS SDK autoload.php file
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$endpoint = 'https://s3.us-east-2.wasabisys.com';
$bucketName = 'gowtham-php2';
$region = 'us-east-2';
$s3client = new S3Client([
'region' => $region,
'version' => 'latest',
'endpoint' => $endpoint,
'credentials' => [
'key' => '<Access-Key>',
'secret' => '<Secret-Key>',
],
]);
<?php
Creating a User Using IAM
<?php
// Include the AWS SDK autoload.php file
require 'vendor/autoload.php';
// Import necessary classes from the AWS SDK
use Aws\Iam\IamClient;
use Aws\Exception\AwsException;
// AWS region, access key, and secret key
$region = 'your-aws-region'; // Replace with your AWS region
$accessKey = 'your-access-key'; // Replace with your IAM user's access key
$secretKey = 'your-secret-key'; // Replace with your IAM user's secret key
// Create an IAM client with specified configuration
$iamClient = new IamClient([ 'region' => $region, 'version' => 'latest', 'credentials' => [
'key' => $accessKey, 'secret' => $secretKey,
],
]);
// Desired IAM username to be created
$iamUserName = 'your-iam-username'; // Replace with the desired IAM username
try {
// Attempt to create an IAM user with the specified username
$result = $iamClient->createUser([ 'UserName' => $iamUserName,
]);
// If successful, display a message
echo "IAM user created: $iamUserName\n";
} catch (AwsException $exception) {
// If an error occurs, display an error message and exit
echo "Failed to create IAM user $iamUserName with error: " . $exception->getMessage() . "\n";
exit("Please fix error with IAM user creation before continuing.\n");
}
?>
Creating a Bucket
<?php
require 'vendor/autoload.php'; // Include the AWS SDK autoload.php file
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$endpoint = 'https://s3.your-region.wasabisys.com'; // Replace with the endpoint for your Wasabi S3
$bucketName = 'your-php-bucket'; // Replace with the desired bucket name
$region = 'your-region'; // Replace with your desired AWS region
// Replace with your actual AWS access key
$accessKey = 'your-access-key';
// Replace with your actual AWS secret key
$secretKey = 'your-secret-key';
// Create an S3 client with specified configuration
$s3client = new S3Client([ 'region' => $region, 'version' => 'latest', 'endpoint' => $endpoint, 'credentials' => [
'key' => $accessKey, 'secret' => $secretKey,
],
]);
try {
// Attempt to create a bucket with the specified name
$result = $s3client->createBucket([ 'Bucket' => $bucketName,
]);
// If successful, display a message
echo "Created bucket named: $bucketName\n";
} catch (AwsException $exception) {
// If an error occurs, display an error message and exit
echo "Failed to create bucket $bucketName with error: " . $exception->getMessage() . "\n";
// Print out the full exception for debugging
// echo "Exception: " . $exception->getAwsRequestId() . "\n";
// echo "Exception: " . $exception->getAwsErrorType() . "\n"; exit("Please fix error with bucket creation before continuing.\n");
}
?>
Upload an Object to the Bucket
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Replace with your Wasabi S3 endpoint
$endpoint = 'https://s3.your-region.wasabisys.com';
// Replace with the desired bucket name
$bucketName = 'your-php-bucket';
// Replace with your desired AWS region
$region = 'your-region';
// Replace with your actual AWS access key
$accessKey = 'your-access-key';
// Replace with your actual AWS secret key
$secretKey = 'your-secret-key';
// Create an S3 client with specified configuration
$s3Client = new S3Client([ 'region' => $region, 'version' => 'latest', 'endpoint' => $endpoint, 'credentials' => [
'key' => $accessKey, 'secret' => $secretKey,
],
]);
// Replace with the local file path you want to upload
$localFilePath = DIR . '/your-local-file.txt';
// Replace with the desired object key in the S3 bucket
$s3Key = 'your-object-key.txt';
try {
// Attempt to upload the local file to the S3 bucket
$s3Client->putObject([ 'Bucket' => $bucketName, 'Key' => $s3Key,
'SourceFile' => $localFilePath,
]);
// If successful, display a message
echo "Uploaded $localFilePath to $bucketName/$s3Key.\n";
} catch (AwsException $exception) {
// If an error occurs, display an error message and exit
echo "Failed to upload $localFilePath with error: " . $exception->getMessage() . "\n"; exit("Please fix the error with file upload before continuing.\n");
}
?>
Reading an Object From the Bucket
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Replace with your Wasabi S3 endpoint
$endpoint = 'https://s3.your-region.wasabisys.com';
// Replace with the desired bucket name
$bucketName = 'your-php-bucket';
// Replace with your desired AWS region
$region = 'your-region';
// Replace with your actual AWS access key
$accessKey = 'your-access-key';
// Replace with your actual AWS secret key
$secretKey = 'your-secret-key';
// Create an S3 client with specified configuration
$s3Client = new S3Client([ 'region' => $region, 'version' => 'latest', 'endpoint' => $endpoint, 'credentials' => [
'key' => $accessKey, 'secret' => $secretKey,
],
]);
// Replace with the actual key of the object you want to read
$objectKey = 'your-object-key';
try {
// Attempt to get the content of the specified object in the S3 bucket
$file = $s3Client->getObject([ 'Bucket' => $bucketName, 'Key' => $objectKey,
]);
// Get the 'Body' of the file
$body = $file['Body'];
// Output the content of the object to the console echo $body->getContents();
// Optionally, you can read a specific number of bytes
// echo $body->read(26);
} catch (AwsException $exception) {
// If an error occurs, display an error message and exit
echo "Failed to read $objectKey from $bucketName with error: " . $exception->getMessage() . "\n"; exit("Please fix error with object reading before continuing.\n");
}
?>
Deleting an Object From the Bucket
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Replace with your Wasabi S3 endpoint
$endpoint = 'https://s3.your-region.wasabisys.com';
// Replace with the desired bucket name
$bucketName = 'your-php-bucket';
// Replace with your desired AWS region
$region = 'your-region';
// Replace with your actual AWS access key
$accessKey = 'your-access-key';
// Replace with your actual AWS secret key
$secretKey = 'your-secret-key';
// Create an S3 client with specified configuration
$s3Client = new S3Client([ 'region' => $region, 'version' => 'latest', 'endpoint' => $endpoint, 'credentials' => [
'key' => $accessKey, 'secret' => $secretKey,
],
]);
// Replace with the actual file name you want to download and delete
$fileName = 'your-file-name.txt';
try {
// Attempt to get the content of the specified file in the S3 bucket
$file = $s3Client->getObject([ 'Bucket' => $bucketName, 'Key' => $fileName,
]);
// Get the 'Body' of the file
$body = $file->get('Body');
$body->rewind();
// Output the content of the file to the console
echo "Downloaded the file and it begins with: {$body->read(26)}.\n";
// Delete the file from S3
$s3Client->deleteObject([ 'Bucket' => $bucketName, 'Key' => $fileName,
]);
echo "Deleted $fileName from $bucketName.\n";
} catch (AwsException $exception) {
// If an error occurs, display an error message and exit
echo "Failed to download or delete $fileName from $bucketName with error: " . $exception->getMessage() . "\n"; exit("Please fix the error before continuing.\n");
}
?>