How do I use AWS SDK for PHP with Wasabi?

AWS SDK for PHP v2 is validated for use with Wasabi. 

 

To use the PHP SDK execute the following steps:

1) Install the AWS SDK for PHP

2) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload and Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket

Other examples can be referred from the AWS documentation.   

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

 

1. How to set the credentials

<?php
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);

// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

 

2.Connect to IAM and S3 endpoints

IAM:

<?php

use Aws\Iam\IamClient;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);


// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$iam = IamClient::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$iam = IamClient::factory($raw_credentials);


S3:

<?php
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);


// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$s3 = S3Client::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$s3 = S3Client::factory($raw_credentials);

 

3. Create a user using IAM

<?php
use Aws\Iam\Exception\IamException;
use Aws\Iam\IamClient;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://iam.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);


//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://iam.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$iam = IamClient::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$iam = IamClient::factory($raw_credentials);

try {
    // Create user
    $result = $iam->createUser([
        'UserName' => 'php-created-user'
    ]);
} catch (IamException $e) {
    echo $e->getMessage() . PHP_EOL;
}

 

4. Create a Bucket

<?php

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);

// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$s3 = S3Client::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$s3 = S3Client::factory($raw_credentials);

// Set parameters to be used in CRUD operations
$bucket = "<bucket-name>";
$key = "<key>";
$file_Path = "<file-path-to-upload>";
$save_Path = "<file-to-save>";

try {
    // Create bucket
    $result = $s3->createBucket([
        'Bucket' => $bucket
    ]);
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

 

5. Upload an object to the Bucket

<?php

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);

// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$s3 = S3Client::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$s3 = S3Client::factory($raw_credentials);

// Set parameters to be used in CRUD operations
$bucket = "<bucket-name>";
$key = "<key>";
$file_Path = "<file-path-to-upload>";
$save_Path = "<file-to-save>";

try {
    // put object
    $result = $s3->putObject([
        'Bucket' => $bucket,
        'Key' => $key,
        'SourceFile' => $file_Path]);
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

 

6. Read an object from the Bucket

<?php

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);

// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$s3 = S3Client::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$s3 = S3Client::factory($raw_credentials);

// Set parameters to be used in CRUD operations
$bucket = "<bucket-name>";
$key = "<key>";
$file_Path = "<file-path-to-upload>";
$save_Path = "<file-to-save>";

try {
    // read object
    $result = $s3->getObject([
        'Bucket' => $bucket,
        'Key' => $key]);
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

 

7. Delete the object from the Bucket

<?php

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;

// Load SDK by requiring from your composer vendor directory
require 'vendor/autoload.php';

// use the following code to establish a connection to wasabi via profile in .aws credential files.
$profile_credentials = array(
    'profile' => 'wasabi',
    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
    'version' => 'latest',
    'use_path_style_endpoint' => true
);

// use the following code to use aws credentials.
//$raw_credentials = array(
//    'credentials' => [
//        'key' => '<insert-access-key>',
//        'secret' => '<insert-secret-key>'
//    ],
//    'endpoint' => 'https://s3.wasabisys.com', // please refer to service end points for buckets in different regions
//    'region' => 'us-east-1', // please refer to service end points for buckets in different regions
//    'version' => 'latest',
//    'use_path_style_endpoint' => true
//);

// establish an S3 Client.
$s3 = S3Client::factory($profile_credentials);

//use the following code to establish a connection to wasabi via access and secret keys.
//$s3 = S3Client::factory($raw_credentials);

// Set parameters to be used in CRUD operations
$bucket = "<bucket-name>";
$key = "<key>";
$file_Path = "<file-path-to-upload>";
$save_Path = "<file-to-save>";

try {
    // put object
    $result = $s3->deleteObject([
        'Bucket' => $bucket,
        'Key' => $key]);
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

Have more questions? Submit a request