# AWS SDK - PHP

Please read the following to understand how to use the 4EVERLAND Bucket with AWS SDK - PHP. This guide outlines the integration of AWS SDK API for PHP with the 4EVERLAND Bucket to facilitate file uploads to the IPFS or Arweave storage networks.

## **Preparation**

* [Download and install](https://www.php.net/manual/en/install.php) PHP in your development environment.
* [Download and install](https://docs.aws.amazon.com/zh_cn/sdk-for-php/v3/developer-guide/getting-started_installation.html) the AWS SDK for PHP version 3.
* [Register](https://www.4everland.org/) a free 4EVERLAND account.
* If you need to use Arweave storage, you need to create an Arweave bucket. Click to[ ](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible)[learn more](https://docs.4everland.org/storage/bucket/guides).
* To obtain the corresponding API key in the bucket, click to[ learn more](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible).

## Development Examples

### **Set the environment variables**

`4everland-Access-Key`：Bucket-Access Keys-API Key

`4everland-Secret-Key`：Bucket-Access Keys-API Secret

**Linux/Mac OS x:**

`export 4EVERLAND_ACCESS_KEY_ID=[4everland-Access-Key]`

`export 4EVERLAND_SECRET_ACCESS_KEY=[4everland-Secret-Key]`

**Windows:**

`SET 4EVERLAND_ACCESS_KEY_ID=[4everland-Access-Key]`

`SET 4EVERLAND_SECRET_ACCESS_KEY=[4everland-Secret-Key]`

### Configure the parameters within the environment variables

```php
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;

// Use the default credential provider
$provider = CredentialProvider::defaultProvider();

// Pass the provider to the client
$client = new S3Client([
    'endpoint'    => 'https://endpoint.4everland.co',
    'region'      => '4everland',
    'version'     => 'latest',
    'credentials' => $provider
]);
```

### Create a bucket

`Region`: Fill in the default value as "4everland"

`Bucket`: Specify the desired name for the Bucket during creation

```php
$s3client = new Aws\S3\S3Client(['region' => '4everland', 'version' => 'latest']);

try {
    $s3client->createBucket([
        'Bucket' => $bucket_name,
        'CreateBucketConfiguration' => ['LocationConstraint' => $region],
    ]);
    echo "Created bucket named: $bucket_name \n";
} catch (Exception $exception) {
    echo "Failed to create bucket $bucket_name with error: " . $exception->getMessage();
    exit("Please fix error with bucket creation before continuing.");
}


```

### Upload a file

`Bucket`: Your bucket name

`Key`: Provide the filename for the file you want to upload

`SourceFile`: Provide the path to the file you want to upload

```php
$s3client = new Aws\S3\S3Client(['region' => '4everland', 'version' => 'latest']);

$file_name = "local-file-" . uniqid();
try {
    $s3client->putObject([
        'Bucket' => $bucket_name,
        'Key' => $file_name,
        'SourceFile' => '/path/to/4everland/4ever.png'
    ]);
    echo "Uploaded $file_name to $bucket_name.\n";
} catch (Exception $exception) {
    echo "Failed to upload $file_name with error: " . $exception->getMessage();
    exit("Please fix error with file upload before continuing.");
}


```

### Check the files in the bucket

`Bucket`: Your bucket name

```php
$s3client = new Aws\S3\S3Client(['region' => '4everland', 'version' => 'latest']);

try {
    $contents = $s3client->listObjects([
        'Bucket' => $bucket_name,
    ]);
    echo "The contents of your bucket are: \n";
    foreach ($contents['Contents'] as $content) {
        echo $content['Key'] . "\n";
    }
} catch (Exception $exception) {
    echo "Failed to list objects in $bucket_name with error: " . $exception->getMessage();
    exit("Please fix error with listing objects before continuing.");
}


```

### Requesting IPFS CID and Arweave Hash

`Bucket`: Your bucket name

`Key`: Specify the path of the target object

```php
$result = $client->headObject([
    'Bucket' => '4everlandbucket', 
    'Key' => '/path/to/4everland/4ever.png', 
]);

echo "ipfs cid:" . $result["Metadata"]["ipfs-hash"]
//If it is an Arweave type of bucke
echo "arweave hash:" . $result["Metadata"]["arweave-hash"]
```

{% hint style="info" %}
If you have any questions, please join our [Discord server](https://discord.com/invite/Cun2VpsdjF), or send us an email at <contact@4everland.org>.
{% endhint %}


---

# 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.4everland.org/storage/bucket/bucket-api-s3-compatible/coding-examples/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.
