# AWS SDK for 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.

## What is a .car file?

A .car file is a type of compressed archive file that contains multiple files, similar to a ZIP file. .car files are used by the FileCoin and IPFS networks, which utilize the metadata fields to include the IPFS CIDs of each file within the archive.

Read below to learn how to pin files and folders to IPFS using the AWS SDK for PHP.

{% hint style="success" %}

### Prerequisites:

* [x] [Download and install](https://aws.amazon.com/cli/) AWS CLI.
* [x] [Configure AWS CLI](https://docs.filebase.com/configurations/cli-tools-and-resources/cli-tools/aws-cli) for use with your Filebase account.
* [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.
* [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 %}

## Pinning a Single File to IPFS

### 1. Click on the ‘Buckets’ option from the menu to open the Buckets dashboard.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FwfbSYivu9OY8APAjWqoP%2Fimage.png?alt=media&#x26;token=50b882f2-29ee-4e8d-8a12-8dc8a6a267a9" alt=""><figcaption></figcaption></figure>

### 2. Once at the Buckets dashboard, create a new bucket by clicking the ‘Create Bucket’ option in the top right corner.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FO0HVqgFf54Sgm6RQFp3r%2Fimage.png?alt=media&#x26;token=d8a1fd66-9763-4cd3-be9b-663d715b22ec" alt=""><figcaption></figcaption></figure>

### 3. Enter a bucket name and choose your storage network.

<figure><img src="https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FnRCYTkpYJ1W93ovsw7b5%2Fimage.png?alt=media&#x26;token=52b62ed7-1b95-4a76-a863-b788b4ae97c9" alt=""><figcaption></figcaption></figure>

### 4. Create a new file with the following AWS SDK for PHP code:

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);
```

## Pinning a Folder to IPFS using the AWS SDK for PHP

### **1. Download the ipfs-car package with the following command:**

`git clone https://github.com/web3-storage/ipfs-car`

### **2. Install the ipfs-car package:**

`npx ipfs-car`

### **3. Next, use ipfs-car to package a folder of files into a .car file.**

This has been tested with .car archives containing 10,000 or more files. Use any of the following commands, depending on your desired workflow:

#### **Write a .car file to the current working directory**

`ipfs-car --pack path/to/file/or/dir`

#### **Write a .car file to a specific directory with a specified name**

`ipfs-car --pack path/to/files --output path/to/write/ipfs-car.car`

### 4. Create a new file with the following AWS SDK for PHP code:

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:** .car file 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 = 'ipfs-car.car';

// Using stream instead of file path
$source = fopen('/path/to/ipfs-car.car', '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);p
```
