Skip to main content

PHP

Use Tigris with PHP through the AWS SDK for PHP. The SDK works with Tigris by changing the endpoint configuration.

For the full SDK reference, see the AWS PHP SDK guide.

Prerequisites

Install

composer require aws/aws-sdk-php

Configure credentials

Set your Tigris credentials as environment variables:

export AWS_ACCESS_KEY_ID="tid_your_access_key"
export AWS_SECRET_ACCESS_KEY="tsec_your_secret_key"

Create a client

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;

$s3 = new S3Client([
'region' => 'auto',
'endpoint' => 'https://t3.storage.dev',
'version' => 'latest',
]);

The client reads credentials from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables automatically.

Basic operations

List buckets

$result = $s3->listBuckets();

foreach ($result['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}

Upload an object

$s3->putObject([
'Bucket' => 'my-bucket',
'Key' => 'hello.txt',
'Body' => 'Hello, World!',
]);

Download an object

$result = $s3->getObject([
'Bucket' => 'my-bucket',
'Key' => 'hello.txt',
]);

echo $result['Body'] . "\n";

List objects

$result = $s3->listObjectsV2([
'Bucket' => 'my-bucket',
]);

foreach ($result['Contents'] ?? [] as $object) {
echo " {$object['Key']} ({$object['Size']} bytes)\n";
}

Generate a presigned URL

$command = $s3->getCommand('GetObject', [
'Bucket' => 'my-bucket',
'Key' => 'hello.txt',
]);

$request = $s3->createPresignedRequest($command, '+60 minutes');
echo (string) $request->getUri() . "\n";

Next steps