php

AWS SDK for PHP

The client under Laravel, Symfony and WordPress

Install

shell
composer require aws/aws-sdk-php

Configure

Configure the endpoint, region, credential and bucket in this tool’s vocabulary. Path-style addressing is recommended for dotted bucket names and clients without nested wildcard TLS, but virtual-hosted addressing is also supported.

In AWS SDK for PHPSet it to
endpointhttps://s3.canada.popcloud.ca
regioncanada
credentials.keyPCAK00EXAMPLEKEYID00
credentials.secret<your secret access key>
use_path_style_endpointtrue
Environment
example values
export AWS_ACCESS_KEY_ID=PCAK00EXAMPLEKEYID00
export AWS_SECRET_ACCESS_KEY=<your secret access key>
export AWS_ENDPOINT_URL=https://s3.canada.popcloud.ca
export AWS_REGION=canada
export POPCLOUD_BUCKET=pc-your-org-media

These are example values. Sign in and every snippet on this site fills in with your own endpoint, key and bucket.

PHP
use Aws\S3\S3Client;

$s3 = new S3Client([
    'version'  => 'latest',
    'region'   => getenv('AWS_REGION'),            // canada
    'endpoint' => getenv('AWS_ENDPOINT_URL'),      // https://s3.<region>.popcloud.ca
    'credentials' => [
        'key'    => getenv('AWS_ACCESS_KEY_ID'),
        'secret' => getenv('AWS_SECRET_ACCESS_KEY'),
    ],
    // Recommended for predictable custom-endpoint behavior. Virtual-hosted
    // requests are supported too for ordinary single-label bucket names.
    'use_path_style_endpoint' => true,
]);

Use it

Upload an object

PHP
$s3->putObject([
    'Bucket'      => $bucket,
    'Key'         => $key,
    'Body'        => $body,
    'ContentType' => 'text/plain',
]);

Upload a large object

PHP
// The multipart uploader handles parts, retries and concurrency for you.
$uploader = new \Aws\S3\MultipartUploader($s3, $path, [
    'bucket'    => $bucket,
    'key'       => $key,
    'part_size' => 8 * 1024 * 1024,
]);
$uploader->upload();

Download an object

PHP
$result = $s3->getObject(['Bucket' => $bucket, 'Key' => $key]);
$body = (string) $result['Body'];

List a prefix

PHP
$pages = $s3->getPaginator('ListObjectsV2', [
    'Bucket' => $bucket,
    'Prefix' => $prefix,
]);
foreach ($pages as $page) {
    foreach ($page['Contents'] ?? [] as $object) {
        $keys[] = $object['Key'];
    }
}

Presign a URL

PHP
$command = $s3->getCommand('GetObject', ['Bucket' => $bucket, 'Key' => $key]);
$request = $s3->createPresignedRequest($command, '+1 hour'); // 7 days is the maximum
$url = (string) $request->getUri();
// Anyone holding this URL can read the object until it expires, with no
// credentials at all.

Delete an object

PHP
$s3->deleteObject(['Bucket' => $bucket, 'Key' => $key]);

What to watch for

These apply to AWS SDK for PHP specifically. The full list covers the platform.

You’ll see
405 MethodNotAllowed from CreateBucket or DeleteBucket — `aws s3 mb`, `mc mb`, or a tool provisioning its own bucket on first run.
Why
Bucket lifecycle belongs to the control plane, which also allocates the storage account, the data-plane key and the CORS rules that come with it. Letting the edge create buckets would create half of one.
Instead
Create buckets in the dashboard, or with `POST /v1/buckets` on the control-plane API. Everything object-level then works normally against that bucket.

How this page is kept true

Every snippet above was extracted from examples/php/aws-sdk-php, a program that uploads, downloads, compares bytes, lists, presigns and cleans up after itself. It runs in CI against a live sandbox organisation via make docs-verify-php. If it stops passing, this page is wrong and we treat that as a bug in the product.

A few snippets on this page are marked not run in CI — they need a browser, a second provider, or a configuration change the sandbox cannot make. Those are reviewed by hand.

Not yet run against the live sandbox — the runner is wired, the first verified run stamps this line.