AWS SDK for PHP
Learn how to pin files and folders to IPFS using the AWS SDK for 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.
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.



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
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);
git clone https://github.com/web3-storage/ipfs-car
npx ipfs-car
This has been tested with .car archives containing 10,000 or more files. Use any of the following commands, depending on your desired workflow:
ipfs-car --pack path/to/file/or/dir
ipfs-car --pack path/to/files --output path/to/write/ipfs-car.car
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
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
If you have any questions, please join our Discord server, or send us an email at [email protected]
Last modified 1yr ago