Accessing the Ceph Object Gateway using PHP

Use PHP scripts for S3 access. This procedure provides some example PHP scripts to do various tasks, such as deleting a bucket or an object.

Important: The examples are tested against php v5.4.16 and aws-sdk v2.8.24.

Prerequisites

  • Root-level access to a development workstation.

  • Internet access.

  1. Install the php package:

    [root@dev ~]# yum install php
  2. Download the zip archive of aws-sdk for PHP and extract it.

  3. Create a project directory:

    [user@dev ~]$ mkdir php_s3
    [user@dev ~]$ cd php_s3
  4. Copy the extracted aws directory to the project directory. For example:

    [user@dev ~]$ cp -r ~/Downloads/aws/ ~/php_s3/
  5. Create the connection file:

    [user@dev ~]$ vim conn.php
  6. Paste the following contents in the conn.php file:

    Syntax

    <?php
    define(AWS_KEY, MY_ACCESS_KEY);
    define(AWS_SECRET_KEY, MY_SECRET_KEY);
    define(HOST, FQDN_OF_GATEWAY_NODE);
    define(PORT, 8080);
    
    // require the AWS SDK for php library
    require /PATH_TO_AWS/aws-autoloader.php;
    
    use Aws\S3\S3Client;
    
    // Establish connection with host using S3 Client
    client = S3Client::factory(array(
        base_url => HOST,
        port => PORT,
        key      => AWS_KEY,
        secret   => AWS_SECRET_KEY
    ));
    ?>

    Replace FQDN_OF_GATEWAY_NODE with the FQDN of the gateway node. Replace PATH_TO_AWS with the absolute path to the extracted aws directory that you copied to the php project directory.

    Save the file and exit the editor.

  7. Run the file:

    [user@dev ~]$ php -f conn.php | echo $?

    If you have provided the values correctly in the file, the output of the command will be 0.

  8. Create a new file for creating a bucket:

    [user@dev ~]$ vim create_bucket.php

    Paste the following contents into the new file:

    Syntax

    <?php
    
    include 'conn.php';
    
    client->createBucket(array('Bucket' => 'my-new-bucket3'));
    
    ?>

    Save the file and exit the editor.

  9. Run the file:

    [user@dev ~]$ php -f create_bucket.php
  10. Create a new file for listing owned buckets:

    [user@dev ~]$ vim list_owned_buckets.php

    Paste the following content into the file:

    Syntax

    <?php
    
    include 'conn.php';
    
    blist = client->listBuckets();
    echo "Buckets belonging to " . blist['Owner']['ID'] . ":\n";
    foreach (blist['Buckets'] as b) {
        echo "{b['Name']}\t{b['CreationDate']}\n";
    }
    
    ?>

    Save the file and exit the editor.

  11. Run the file:

    [user@dev ~]$ php -f list_owned_buckets.php

    The output should look similar to this:

    my-new-bucket3 2020-01-21 10:33:19 UTC
  12. Create an object by first creating a source file named hello.txt:

    [user@dev ~]$ echo "Hello World!" > hello.txt
  13. Create a new php file:

    [user@dev ~]$ vim create_object.php

    Paste the following contents into the file:

    Syntax

    <?php
    
    include 'conn.php';
    
    key         = 'hello.txt';
    source_file = './hello.txt';
    acl         = 'private';
    bucket      = 'my-new-bucket3';
    client->upload(bucket, key, fopen(source_file, 'r'), acl);
    
    ?>

    Save the file and exit the editor.

  14. Run the file:

    [user@dev ~]$ php -f create_object.php

    This will create the object hello.txt in bucket my-new-bucket3.

  15. Create a new file for listing a bucket’s content:

    [user@dev ~]$ vim list_bucket_content.php

    Paste the following content into the file:

    Syntax

    <?php
    
    include 'conn.php';
    
    o_iter = client->getIterator('ListObjects', array(
        'Bucket' => 'my-new-bucket3'
    ));
    foreach (o_iter as o) {
        echo "{o['Key']}\t{o['Size']}\t{o['LastModified']}\n";
    }
    ?>

    Save the file and exit the editor.

  16. Run the file:

    [user@dev ~]$ php -f list_bucket_content.php

    The output will look similar to this:

    hello.txt    12    Fri, 22 Jan 2020 15:54:52 GMT
  17. Create a new file for deleting an empty bucket:

    [user@dev ~]$ vim del_empty_bucket.php

    Paste the following contents into the file:

    Syntax

    <?php
    
    include 'conn.php';
    
    client->deleteBucket(array('Bucket' => 'my-new-bucket3'));
    ?>

    Save the file and exit the editor.

  18. Run the file:

    [user@dev ~]$ php -f del_empty_bucket.php | echo $?
    If the bucket is successfully deleted, the command will return 0 as output.
    Important: Deleting a non-empty bucket is currently not supported in PHP 2 and newer versions of aws-sdk.
    Note: Edit the create_bucket.php file to create empty buckets, for example, my-new-bucket4, my-new-bucket5. Next, edit the del_empty_bucket.php file accordingly prior to deleting the empty buckets.
  19. Create a new file for deleting an object:

    [user@dev ~]$ vim delete_object.php

    Paste the following contents into the file:

    Syntax

    <?php
    
    include 'conn.php';
    
    client->deleteObject(array(
        'Bucket' => 'my-new-bucket3',
        'Key'    => 'hello.txt',
    ));
    ?>

    Save the file and exit the editor.

  20. Run the file:

    [user@dev ~]$ php -f delete_object.php

    This will delete the object hello.txt.