# Connect to S4 with the AWS SDK for PHP

> How to connect to Bitiful S4 with Aws-for-PHP.

## Add the aws-for-php framework

Prerequisite: install the framework (<https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/welcome.html> )

```php s4-demo.php
require 'vendor/autoload.php';

use Aws\Middleware;
use Aws\S3\S3Client;
use Aws\S3\S3ClientInterface;
use Aws\S3\MultipartUploader;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

### Connect to the S4 endpoint

```php s4-demo.php
    /**
	*
	* Connect to the Bitiful S4 endpoint using the AWS S3 protocol
	*
	* @param - bucket name
	* @return - S3Client object
	*
	*/
	private function connectS4() {		
		$s3 = new Aws\S3\S3Client([
		 	'credentials' => [				
	        	'key'    => 'sub-account accessKey', // Create a sub-account in the console, then create an accessKey for it
	        	'secret' => 'sub-account secretKey', // Important: when you create a sub-account, you must manually grant it specific permissions!
	    	],
                        'use_path_style_endpoint' => false,
                        'use_aws_shared_config_files' => false,
			'endpoint' => "https://s3.bitiful.net", // Use the value shown under the "Endpoint" label at the bottom of the "Bucket settings" page in the console
            'signature_version' => 'v4',
			'version' => 'latest',
			'region'  => 'cn-east-1'
		]);

		return $s3;
	}
```

### List objects in a bucket

```php s4-demo.php
    /**
	*
	* List objects in the bucket
	*
	* @param - bucket name, prefix, delimiter
	* @return - objects list
	*
	*/
	public function listObjects($bucket, $prefix, $StartAfter) {
		$s4 = $this->connectS4();
		$objects = $s4->listObjects([
			'Bucket' => $bucket,
			'Prefix' => $prefix,
			'Delimiter' => '/',
			'StartAfter' => $StartAfter,
			'MaxKeys' => 50
		]);

		return $objects;

	}
```

### Generate a time-limited presigned download URL

```php s4-demo.php
    /**
	*
	* Generate a time-limited presigned download URL
	*
	* @param - Object Key, bucket name
	* @return - time-limited presigned URL for the object
	*
	*/
	public function getFileLink($key, $bucket) {
		$s4 = $this->connectS4($bucket);
		$key = html_entity_decode($key, ENT_COMPAT, 'UTF-8');
		$result = $s4->getCommand('GetObject',[
			'Bucket' => $bucket,
			'Key' => $key
		]);
		# Use the S3 protocol to create a presigned private request for the S4 object (valid for the next hour)
		$presigned_url = $s4->createPresignedRequest($result , '+1 hours');
		# Get the presigned private URL as a string and return it
		$private_url = (string)$presigned_url->getUri();
		return $private_url;
	}
```

### Add CoreIX media processing parameters to a presigned URL

S3 presigning includes all URL query parameters (args) in the signature, so you need to use middleware here:

```php s4-demo.php
    /**
	*
	* Generate a time-limited presigned download URL
	*
	* @param - Object Key to download, bucket name
	* @return - time-limited presigned URL for the object
	*
	*/
	public function getFileLink($key, $bucket) {
		$s4 = $this->connectS4($bucket);
		$key = html_entity_decode($key, ENT_COMPAT, 'UTF-8');
		$result = $s4->getCommand('GetObject',[
			'Bucket' => $bucket,
			'Key' => $key
		]);
		$result->getHandlerList()->appendBuild(
			Middleware::mapRequest(function (RequestInterface $request) {
				// Return a new request with the added header
				return $request->withUri(
					$request->getUri()->withQuery(
						$request->getUri()->getQuery() . "w=500" // Adds the CoreIX processing parameter w=500
					)
				);
			}),
			'add-custom-param'
		);
		# `w=500` is now included in the signature calculation
		$presigned_url = $s4->createPresignedRequest($result , '+1 hours');
		# Get the presigned private URL as a string and return it
		$private_url = (string)$presigned_url->getUri();
		return $private_url;
	}
```

### Add a per-connection rate limit parameter to a presigned URL

S4 extends S3 with the x-amz-limit-rate parameter, which limits the download speed of each connection for the object. The value is in bytes (a positive integer).
As above, you still need middleware to handle the signature:

```php s4-demo.php
    /**
	*
	* Generate a time-limited presigned download URL
	*
	* @param - Object Key to download, bucket name
	* @return - time-limited presigned URL for the object
	*
	*/
	public function getFileLink($key, $bucket) {
		$s4 = $this->connectS4($bucket);
		$key = html_entity_decode($key, ENT_COMPAT, 'UTF-8');
		$result = $s4->getCommand('GetObject',[
			'Bucket' => $bucket,
			'Key' => $key
		]);
		$result->getHandlerList()->appendBuild(
			Middleware::mapRequest(function (RequestInterface $request) {
				// Return a new request with the added header
				return $request->withUri(
					$request->getUri()->withQuery(
						$request->getUri()->getQuery() . "x-amz-limit-rate=102400" // Limit to 102400 bytes (100 KiB) per second
					)
				);
			}),
			'add-custom-param'
		);
		# `x-amz-limit-rate=102400` is now included in the signature calculation
		$presigned_url = $s4->createPresignedRequest($result , '+1 hours');
		# Get the presigned private URL as a string and return it
		$private_url = (string)$presigned_url->getUri();
		return $private_url;
	}
```
