sObject - PHP Examples
Publisher: Psychz Networks, March 21,2025- Prerequisites
- Installing PHP SDK
- Creating Connection
- Listing Buckets
- Creating Buckets
- Listing Bucket's Content
- Deleting Bucket
- Creating Object
- Changing Object's ACL
- Deleting Object
- Downloading Object (To a File)
- Generating Object Download URL
- Conclusion
Integrating sObject Storage with PHP
sObject provides an S3-compatible API that lets you interact with your object storage. This article uses the AWS PHP SDK (installed via Composer) to demonstrate various operations such as listing buckets, creating buckets/objects, setting ACLs, downloading files, and generating download URLs.
Prerequisites
- PHP installed on your system
- Composer installed
- sObject endpoint, access key, and secret key
Installing the PHP SDK
Install the AWS PHP SDK using Composer by running the following command in your project directory:
composer require aws/aws-sdk-php
This will download and install the AWS PHP SDK into your project.
Creating a Connection
Create a connection to sObject by configuring the S3 client with sObject endpoint and credentials. Save the following code in a PHP file (for example, psychz_sobject_connection.php):
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// Replace with your sObject endpoint, access key, and secret key.
$endpoint = 'http://sobject-host:port';
$accessKey = 'your-access-key';
$secretKey = 'your-secret-key';
$client = new S3Client([
'version' => 'latest',
'region' => 'us-east-1', // Dummy region; required by the SDK.
'endpoint' => $endpoint,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey,
],
// Use path-style endpoints for sObject.
'use_path_style_endpoint' => true,
]);
echo "Connected to sObject successfully.\n";
?>
Listing Owned Buckets
Use the listBuckets method to retrieve and list all buckets owned by your account:
<?php
try {
$result = $client->listBuckets();
echo "Buckets owned by you:\n";
foreach ($result['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";
}
} catch (AwsException $e) {
echo "Error listing buckets: " . $e->getMessage() . "\n";
}
?>
Creating a Bucket
Create a new bucket by specifying a unique bucket name:
<?php
$bucketName = 'my-new-bucket';
try {
$client->createBucket([
'Bucket' => $bucketName,
]);
echo "Bucket '{$bucketName}' created successfully.\n";
} catch (AwsException $e) {
echo "Error creating bucket: " . $e->getMessage() . "\n";
}
?>
Listing a Bucket's Content
List all objects stored in a bucket:
<?php
try {
$result = $client->listObjects([
'Bucket' => $bucketName,
]);
if (isset($result['Contents'])) {
echo "Objects in bucket '{$bucketName}':\n";
foreach ($result['Contents'] as $object) {
echo $object['Key'] . "\n";
}
} else {
echo "Bucket '{$bucketName}' is empty.\n";
}
} catch (AwsException $e) {
echo "Error listing objects: " . $e->getMessage() . "\n";
}
?>
Deleting a Bucket
Before deleting a bucket, ensure that it is empty. This snippet first deletes all objects inside the bucket and then deletes the bucket itself:
<?php
try {
// List objects in the bucket.
$objects = $client->listObjects(['Bucket' => $bucketName]);
if (isset($objects['Contents'])) {
foreach ($objects['Contents'] as $object) {
$client->deleteObject([
'Bucket' => $bucketName,
'Key' => $object['Key'],
]);
echo "Deleted object: " . $object['Key'] . "\n";
}
}
// Delete the bucket.
$client->deleteBucket(['Bucket' => $bucketName]);
echo "Bucket '{$bucketName}' deleted successfully.\n";
} catch (AwsException $e) {
echo "Error deleting bucket: " . $e->getMessage() . "\n";
}
?>
Creating an Object
Upload content to a new object in your bucket using the putObject method:
<?php
$objectKey = 'sample.txt';
$content = 'This is a sample file content.';
try {
$client->putObject([
'Bucket' => $bucketName,
'Key' => $objectKey,
'Body' => $content,
]);
echo "Object '{$objectKey}' created successfully in bucket '{$bucketName}'.\n";
} catch (AwsException $e) {
echo "Error creating object: " . $e->getMessage() . "\n";
}
?>
Changing an Object's ACL
Modify an object’s access control list (ACL). For instance, to make an object publicly readable:
<?php
try {
$client->putObjectAcl([
'Bucket' => $bucketName,
'Key' => $objectKey,
'ACL' => 'public-read',
]);
echo "ACL for object '{$objectKey}' changed to public-read.\n";
} catch (AwsException $e) {
echo "Error changing ACL: " . $e->getMessage() . "\n";
}
?>
Deleting an Object
Remove an object from your bucket using the deleteObject method:
<?php
try {
$client->deleteObject([
'Bucket' => $bucketName,
'Key' => $objectKey,
]);
echo "Object '{$objectKey}' deleted successfully from bucket '{$bucketName}'.\n";
} catch (AwsException $e) {
echo "Error deleting object: " . $e->getMessage() . "\n";
}
?>
Downloading an Object (To a File)
Download an object and save it locally using the getObject method with the SaveAs option:
<?php
$localFilePath = '/path/to/downloaded/file.txt';
try {
$client->getObject([
'Bucket' => $bucketName,
'Key' => $objectKey,
'SaveAs' => $localFilePath,
]);
echo "Object '{$objectKey}' downloaded successfully to '{$localFilePath}'.\n";
} catch (AwsException $e) {
echo "Error downloading object: " . $e->getMessage() . "\n";
}
?>
Generating an Object Download URL
Signed URL (Temporary Access)
Generate a time-limited signed URL for secure access to a private object:
<?php
try {
$cmd = $client->getCommand('GetObject', [
'Bucket' => $bucketName,
'Key' => $objectKey,
]);
// The URL will expire in 20 minutes.
$request = $client->createPresignedRequest($cmd, '+20 minutes');
$signedUrl = (string) $request->getUri();
echo "Signed URL: " . $signedUrl . "\n";
} catch (AwsException $e) {
echo "Error generating signed URL: " . $e->getMessage() . "\n";
}
?>
Unsigned URL (For Public Objects)
If the object is public, you can manually construct the URL:
<?php
// Ensure there is no trailing slash on the endpoint.
$unsignedUrl = rtrim($client->getEndpoint(), '/') . '/' . $bucketName . '/' . $objectKey;
echo "Unsigned URL: " . $unsignedUrl . "\n";
?>
Conclusion
Using the SDK, you can seamlessly integrate sObject storage into your PHP applications. This guide provided sample code for installing the SDK, establishing a connection, and performing various bucket and object operations. Adjust the code as necessary for your environment and security policies.
Happy coding!