Using Sonic object storage with PHP

PHP logo

You can use s3 libraries to interact with Sonic's S3-compatible API, like the AWS SDK for PHP. To begin, create a storage zone in your account and select the region and storage tier that you prefer. Pushr will create a bucket and will configure CDN routing automatically to cache and accelerate your bucket's content globally.

Create a storage zone

Each storage zone is a bucket + a CDN configuration applied automatically. From the CDN section in your dashboard, create a new storage zone:

Click on Create push zone 

In the popup window that appears, name your zone and select a storage region:

Now with your new storage zone created, click on it to manage it. Navigate to the Storage & Hostnames tab to reveal your storage endpoint, access key, secret key and the bucket name that was generated for you automatically:

Configure Sonic

Require the AWS SDK package with Composer:

php composer.phar require aws/aws-sdk-php

Configure the Sonic S3 endpoint and credentials:


require 'vendor/autoload.php';
use Aws\S3\S3Client;

$client = new Aws\S3\S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1',
    'endpoint' => 'https://s3.de01.sonic.r-cdn.com',
    'use_path_style_endpoint' => false,
    'credentials' => [
        'key'    => getenv('ACCESS_KEY'),
        'secret' => getenv('SECRET_KEY'),
    ],
]);
    

Note: the region variable is set for compatiblity with the AWS SDK, especially bucket creation, but is currently not being used by Sonic. Instead, to create a new bucket, you need to create a new storage zone in your account, and a bucket will be created and attached to it automatically.

Bucket policy

Sonic does not support editing of bucket policies. There is one default mixed access policy that applies to each bucket. It has the following characteristics:

• Unauthenticated requests can download files from the bucket url/CDN hostname via direct links.

• Only authenticated requests can write, list and tag objects towards the S3 endpoint. This behaviour is equivalent to disabling AWS's Block Public Access setting.

Supported APIs

Sonic's S3-compatible APIs

API reference

AWS SDK's reference for PHP