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

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

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

$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

$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

$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

$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"]

If you have any questions, please join our Discord server, or send us an email at contact@4everland.org.

Last updated