# AWS SDK - PHP

## **What is AWS SDK - PHP?**

AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - PHP. Read below to learn how to use the AWS SDK for PHP with Filebase.

{% hint style="success" %}

### Prerequisites:

* [x] [Download and install](https://www.php.net/manual/en/install.php) PHP in your environment.
* [x] [Download and install](https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html) the AWS SDK for PHP Version 3.
* [x] [Sign up](https://filebase.com/signup) for a free Filebase account.&#x20;
* [x] Have your Filebase Access and Secret Keys. Learn how to view your access keys [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#working-with-access-keys).
  {% endhint %}

### Setting Your Filebase Credentials as Environment Variables

#### Linux / Mac OS x:

`export FILEBASE_ACCESS_KEY_ID=[Filebase-Access-Key]`

`export FILEBASE_SECRET_ACCESS_KEY=[Filebase-Secret-Key]`

#### Windows:

`SET FILEBASE_ACCESS_KEY_ID=[Filebase-Access-Key]`

`SET FILEBASE_SECRET_ACCESS_KEY=[Filebase-Secret-Key]`

Alternatively, you may use the credentials stored in your `.aws/credentials` file and reference them using `'profile' => 'filebase'` in place of the `'credentials'` array, or you may hard code your credentials. Hard coding is not recommended for production environments.&#x20;

### List All Buckets

The following code example lists all buckets within a user account.

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}
 
```

### Upload an Object

The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:

* **bucket:** Filebase bucket name
* **key:** Object name
* **source:** File path to the object

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;
use Aws\S3\ObjectUploader;

$s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);

$bucket = 'your-bucket';
$key = 'my-file.zip';

// Using stream instead of file path
$source = fopen('/path/to/large/file.zip', 'rb');

$uploader = new ObjectUploader(
    $s3Client,
    $bucket,
    $key,
    $source
);

do {
    try {
        $result = $uploader->upload();
        if ($result["@metadata"]["statusCode"] == '200') {
            print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
        }
        print($result);
    } catch (MultipartUploadException $e) {
        rewind($source);
        $uploader = new MultipartUploader($s3Client, $source, [
            'state' => $e->getState(),
        ]);
    }
} while (!isset($result));

fclose($source);
```

### Download an Object

The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:

* **bucket:** Filebase bucket name
* **key:** Object name

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$bucket = 'bucket';
$key = 'key';

try {
    //Create a S3Client
    $s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
    ]);
    // Save object to a file.
    $result = $s3Client->getObject(array(
        'Bucket' => $bucket,
        'Key' => $key,
        'SaveAs' => $key
    ));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

```

### List All Objects In a Bucket

The following code example lists all objects from the specified bucket. Replace the following values in the code to match your configuration:

* **bucket**: Filebase bucket name
* **key:** Object name

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = 'bucket';

$s3 = new S3Client([
    'endpoint' => 'https://s3.filebase.com',
	'region' => 'us-east-1',
	'version' => 'latest',
	'use_path_style_endpoint' => true,
	'credentials' => [
        	'key'    => $FILEBASE_ACCESS_KEY_ID,
        	'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ],
]);


try {
    $results = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);

    foreach ($results as $result) {
        foreach ($result['Contents'] as $object) {
            echo $object['Key'] . PHP_EOL;
        }
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

try {
    $objects = $s3->listObjects([
        'Bucket' => $bucket
    ]);
    foreach ($objects['Contents']  as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

```

### Delete an Object

The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:

* **bucket:** Filebase bucket name
* **key:** Object name

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$bucket = 'bucket';
$key = 'key';

try {
    //Create a S3Client
    $s3Client = new S3Client([
        'endpoint' => 'https://s3.filebase.com',
        'region' => 'us-east-1',
        'version' => 'latest',
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => $FILEBASE_ACCESS_KEY_ID,
                'secret' => $FILEBASE_SECRET_ACCESS_KEY,
        ],
    ]);
    $result = $s3Client->deleteObject([
        'Bucket' => $bucket,
        'Key' => $key,
    ]);
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}
```

### Generating a Pre-Signed URL

The following code example generates a pre-signed URL for an object in a Filebase bucket. For more information on pre-signed URLs, please see [here.](/api-documentation/s3-compatible-api/pre-signed-urls.md) This workflow can be used for both public and private buckets. Replace the following values in the code to match your configuration:

* **bucket:** Filebase bucket name
* **key:** Object name

```php
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;  
use Aws\Exception\AwsException;

$s3Client = new Aws\S3\S3Client([
    'endpoint' => 'https://s3.filebase.com',
    'region' => 'us-east-1',
    'version' => 'latest',
    'use_path_style_endpoint' => true,
    'credentials' => [
            'key'    => $FILEBASE_ACCESS_KEY_ID,
            'secret' => $FILEBASE_SECRET_ACCESS_KEY,
    ]
]);

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$presignedUrl = (string)$request->getUri();

$url = $s3Client->getObjectUrl('my-bucket', 'my-key');

echo $presignedUrl
;
```

For more information on the AWS SDK for PHP, check out the documentation [here](https://github.com/aws/aws-sdk-php), or view additional code examples [here](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code/s3).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.filebase.com/code-development-+-sdks/code-development/aws-sdk-php.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
