AWS SDK - PHP

Learn how to use the AWS SDK for PHP with Filebase.

What is AWS SDK - PHP?

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - PHP. Read below to learn how to use the AWS SDK for PHP with Filebase.

Prerequisites:

Setting Your Filebase Credentials as Environment Variables

Linux / Mac OS x:

export FILEBASE_ACCESS_KEY_ID=[Filebase-Access-Key]

export FILEBASE_SECRET_ACCESS_KEY=[Filebase-Secret-Key]

Windows:

SET FILEBASE_ACCESS_KEY_ID=[Filebase-Access-Key]

SET FILEBASE_SECRET_ACCESS_KEY=[Filebase-Secret-Key]

Alternatively, you may use the credentials stored in your .aws/credentials file and reference them using 'profile' => 'filebase' in place of the 'credentials' array, or you may hard code your credentials. Hard coding is not recommended for production environments.

List All Buckets

The following code example lists all buckets within a user account.

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}
 

Upload an Object

The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:

  • bucket: Filebase bucket name

  • key: Object name

  • source: File path to the object

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;
use Aws\S3\ObjectUploader;

$s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);

$bucket = 'your-bucket';
$key = 'my-file.zip';

// Using stream instead of file path
$source = fopen('/path/to/large/file.zip', 'rb');

$uploader = new ObjectUploader(
    $s3Client,
    $bucket,
    $key,
    $source
);

do {
    try {
        $result = $uploader->upload();
        if ($result["@metadata"]["statusCode"] == '200') {
            print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
        }
        print($result);
    } catch (MultipartUploadException $e) {
        rewind($source);
        $uploader = new MultipartUploader($s3Client, $source, [
            'state' => $e->getState(),
        ]);
    }
} while (!isset($result));

fclose($source);

Download an Object

The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:

  • bucket: Filebase bucket name

  • key: Object name

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$bucket = 'bucket';
$key = 'key';

try {
    //Create a S3Client
    $s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
    ]);
    // Save object to a file.
    $result = $s3Client->getObject(array(
        'Bucket' => $bucket,
        'Key' => $key,
        'SaveAs' => $key
    ));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

List All Objects In a Bucket

The following code example lists all objects from the specified bucket. Replace the following values in the code to match your configuration:

  • bucket: Filebase bucket name

  • key: Object name

<?php

require 'vendor/autoload.php';

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

$bucket = 'bucket';

$s3 = new S3Client([
    'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);


try {
    $results = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);

    foreach ($results as $result) {
        foreach ($result['Contents'] as $object) {
            echo $object['Key'] . PHP_EOL;
        }
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

try {
    $objects = $s3->listObjects([
        'Bucket' => $bucket
    ]);
    foreach ($objects['Contents']  as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

Delete an Object

The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:

  • bucket: Filebase bucket name

  • key: Object name

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$bucket = 'bucket';
$key = 'key';

try {
    //Create a S3Client
    $s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
        'region' => 'us-east-1',
        'version' => 'latest',
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => $FILEBASE_ACCESS_KEY_ID,
                'secret' => $FILEBASE_SECRET_ACCESS_KEY,
        ],
    ]);
    $result = $s3Client->deleteObject([
        'Bucket' => $bucket,
        'Key' => $key,
    ]);
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

Generating a Pre-Signed URL

The following code example generates a pre-signed URL for an object in a Filebase bucket. For more information on pre-signed URLs, please see here. This workflow can be used for both public and private buckets. Replace the following values in the code to match your configuration:

  • bucket: Filebase bucket name

  • key: Object name

<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$s3Client = new Aws\S3\S3Client([
    'endpoint' => 'https://s3.filebase.com',
    'region' => 'us-east-1',
    'version' => 'latest',
    'use_path_style_endpoint' => true,
    'credentials' => [
            'key'    => $FILEBASE_ACCESS_KEY_ID,
            'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ]
]);

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$presignedUrl = (string)$request->getUri();

$url = $s3Client->getObjectUrl('my-bucket', 'my-key');

echo $presignedUrl
;

For more information on the AWS SDK for PHP, check out the documentation here, or view additional code examples here.

Last updated