
## Installation

To use Rabata.io with PHP, you'll need to install the AWS SDK for PHP.

### Install AWS SDK

```bash
$ composer require aws/aws-sdk-php
```

For a new project, you can set up a basic structure:

```bash
$ 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](/docs/quickstart/aws-cli/), the SDK will automatically use those credentials.

### Method 2: Explicit Configuration in Code

You can explicitly configure the S3 client in your code:

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

use Aws\S3\S3Client;

// Create an S3 client with Rabata.io endpoint
$s3Client = new S3Client([
    'version' => 'latest',
    'region' => 'us-east-1',
    'endpoint' => 'https://s3.us-east-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:

```bash
# 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=us-east-1
```

Then in your code:

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

use Aws\S3\S3Client;

// Create an S3 client with Rabata.io endpoint
$s3Client = new S3Client([
    'version' => 'latest',
    'region' => 'us-east-1',
    'endpoint' => 'https://s3.us-east-1.rabata.io',
]);
```

### Note on Older PHP Libraries

If you're using older versions of the AWS SDK for PHP (v2) or alternative S3 libraries, you may need to explicitly configure signature version 4 in your client configuration. Rabata.io requires signature v4 for authentication. Modern versions of the AWS SDK for PHP (v3) automatically use signature v4 for custom S3-compatible endpoints.

## 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
<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;

$s3Client = new S3Client([
    'version' => 'latest',
    'region' => 'us-east-1',
    'endpoint' => 'https://s3.us-east-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

```php
$result = $s3Client->createBucket([
    'Bucket' => 'my-bucket-name',
]);

echo "Bucket created: {$result['Location']}\n";
```

#### Delete a Bucket

```php
$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

```php
$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

```php
$result = $s3Client->putObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'remote-file.txt',
    'SourceFile' => 'local-file.txt',
]);

echo "File uploaded: {$result['ObjectURL']}\n";
```

#### Download a File

```php
$result = $s3Client->getObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'remote-file.txt',
    'SaveAs' => 'local-file.txt',
]);

echo "File downloaded\n";
```

#### Delete a File

```php
$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

```php
$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";
```
<!--
### Setting Object ACLs

```php
$result = $s3Client->putObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'public-file.txt',
    'Body' => 'This is a public file',
    'ACL' => 'public-read',
]);

echo "Public file uploaded\n";
``` -->

### Using Presigned URLs

Generate a presigned URL to allow temporary access to an object:

```php
$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";
```

<div class="docs-article__actions">
  <a href="/docs/quickstart/laravel/" class="btn btn--secondary">Laravel Integration</a>
  <a href="/docs/quickstart/symfony/" class="btn btn--secondary">Symfony Integration</a>
</div>
