•  Home
  •  Dashboard
  •  Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
  •  Hosting Services
    • Infrastructure
      • iColocation
    • Compute
      • cMetal
      • cVirtual
    • Storage
      • sObject
      • sBlock
    • Networking
      • nIP Transit
      • nWavelength
    • Protection
      • pBackup
      • pDDoS
  •  Solutions
    • Ecommerce
    • Finance
    • Gaming
    • Hosting
    • Management
    • Security
    • System Integrator
  •  Support
    • Community
    • Knowledge Base
    • Open A Ticket
    • Status
  •  USA & Canada: 800-933-1517
  •  International: 626-549-2801
  •  Email: sales@psychz.net
  • Services
    • new-colocation-header-img
      Infrastructure
      • iColocation
    • new-compute-header-img
      Compute
      • cMetal
      • cVirtual
    • new-storage-header-img
      Storage
      • sObject
      • sBlock
    • new-networking-header-img
      Networking
      • nIP Transit
      • nWavelength
    • new-protection-header-img
      Protection
      • pBackup
      • pDDoS
  • Solutions
    • Ecommerce
    • Security
    • Gaming
    • Hosting
    • Management
    • Finance
    • System Integrator
  • Dashboard

sObject - PHP Examples

  • Home
  • Client
  • Knowledgebase
  • Storage
  • sObject
  • sObject - PHP Examples

Table Of Content

    Related Articles

    • sObject – Bucket Policy Examples
    • sObject – Sub-User Permissions
    • s3Express Guide
    • sObject - Javascript Examples
    • sObject - Python Examples
    • sObject - Configuring CORS and Bucket Policies Using S3 Browser
    • sObject - SSE (Server Side Encryption) Guide
    • sObject - CNAME Guide
    • sObject - CrossFTP Guide
    • sObject - WinSCP Guide
    • sObject - S3 Browser Guide
    • sObject - Keys
    • sObject - Buckets
    • sObject - Access Using S3tools
    • sObject - Limits and Best Practices
    • sObject - Delete
    • sObject - Intro
    • sObject - Create

    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!

    Views: (2051) Votes: (0)

    Related Articles

    • sObject – Bucket Policy Examples
    • sObject – Sub-User Permissions
    • s3Express Guide
    • sObject - Javascript Examples
    • sObject - Python Examples
    • sObject - Configuring CORS and Bucket Policies Using S3 Browser
    • sObject - SSE (Server Side Encryption) Guide
    • sObject - CNAME Guide
    • sObject - CrossFTP Guide
    • sObject - WinSCP Guide
    • sObject - S3 Browser Guide
    • sObject - Keys
    • sObject - Buckets
    • sObject - Access Using S3tools
    • sObject - Limits and Best Practices
    • sObject - Delete
    • sObject - Intro
    • sObject - Create
    Copyright © 2026 Psychz Networks,
    A Profuse Solutions Inc Company
    Hosting Services
    • Catalog
    Infrastructure
    • iColocation
    Compute
    • cMetal
    • cVirtual
    Storage
    • sObject
    • sBlock
    Networking
    • nIP Transit
    • nWavelength
    Protection
    • pBackup
    • pDDoS
    Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
    Policies
    • Acceptable Usage Policy
    • Privacy Policy
    • Service Level Agreement
    • Terms and Conditions
    Support
    • Community
    • Knowledge Base
    • Open A Ticket
    • Status
    Get In Touch
    • Psychz Networks,
      A Profuse Solutions Company
      611 Wilshire Blvd #300
      Los Angeles,California 90017
      USA
    • US/Canada: 800-933-1517
    • International: 626-549-2801