PHP Quickstart
Learn how to use PHP with Rabata.io for managing your object storage using the AWS SDK for PHP.
Installation
To use Rabata.io with PHP, you’ll need to install the AWS SDK for PHP.
Install AWS SDK
$ composer require aws/aws-sdk-php
For a new project, you can set up a basic structure:
$ mkdir my-rabata-project
$ cd my-rabata-project
$ composer init --no-interaction
$ composer require aws/aws-sdk-php
Configuration
There are several ways to configure the AWS SDK for PHP to work with Rabata.io.
Method 1: Using AWS Credentials File
If you’ve already configured the AWS CLI as shown in the AWS CLI Quickstart, the SDK will automatically use those credentials.
Method 2: Explicit Configuration in Code
You can explicitly configure the S3 client in your code:
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Create an S3 client with Rabata.io endpoint
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'eu-west-1',
'endpoint' => 'https://s3.eu-west-1.rabata.io',
'credentials' => [
'key' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY',
],
]);
Method 3: Using Environment Variables
You can set environment variables to configure the SDK:
# Set these environment variables before running your PHP script
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
export AWS_REGION=eu-west-1
Then in your code:
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Create an S3 client with Rabata.io endpoint
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'eu-west-1',
'endpoint' => 'https://s3.eu-west-1.rabata.io',
]);
Basic Operations
Here are some common operations you can perform with the AWS SDK for PHP and Rabata.io.
Bucket Operations
List All Buckets
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'eu-west-1',
'endpoint' => 'https://s3.eu-west-1.rabata.io',
'credentials' => [
'key' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY',
],
]);
$result = $s3Client->listBuckets();
echo "Buckets:\n";
foreach ($result['Buckets'] as $bucket) {
echo " {$bucket['Name']}\n";
}
Create a Bucket
$result = $s3Client->createBucket([
'Bucket' => 'my-bucket-name',
]);
echo "Bucket created: {$result['Location']}\n";
Delete a Bucket
$result = $s3Client->deleteBucket([
'Bucket' => 'my-bucket-name',
]);
echo "Bucket deleted\n";
Note: The bucket must be empty before it can be deleted.
Object Operations
List Objects in a Bucket
$result = $s3Client->listObjectsV2([
'Bucket' => 'my-bucket-name',
]);
echo "Objects in bucket my-bucket-name:\n";
if (isset($result['Contents'])) {
foreach ($result['Contents'] as $object) {
echo " {$object['Key']} ({$object['Size']} bytes)\n";
}
}
Upload a File
$result = $s3Client->putObject([
'Bucket' => 'my-bucket-name',
'Key' => 'remote-file.txt',
'SourceFile' => 'local-file.txt',
]);
echo "File uploaded: {$result['ObjectURL']}\n";
Download a File
$result = $s3Client->getObject([
'Bucket' => 'my-bucket-name',
'Key' => 'remote-file.txt',
'SaveAs' => 'local-file.txt',
]);
echo "File downloaded\n";
Delete a File
$result = $s3Client->deleteObject([
'Bucket' => 'my-bucket-name',
'Key' => 'file-to-delete.txt',
]);
echo "File deleted\n";
Advanced Operations
Here are some more advanced operations you can perform with the AWS SDK for PHP and Rabata.io.
Working with Object Metadata
$result = $s3Client->putObject([
'Bucket' => 'my-bucket-name',
'Key' => 'file-with-metadata.txt',
'Body' => 'Hello, World!',
'Metadata' => [
'custom-key' => 'custom-value',
'content-type' => 'text/plain',
],
]);
echo "File uploaded with metadata\n";
Using Presigned URLs
Generate a presigned URL to allow temporary access to an object:
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => 'my-bucket-name',
'Key' => 'private-file.txt',
]);
$request = $s3Client->createPresignedRequest($cmd, '+1 hour');
$presignedUrl = (string) $request->getUri();
echo "Presigned URL: {$presignedUrl}\n";