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.
php v5.4.16 and
aws-sdk v2.8.24.Prerequisites
-
Root-level access to a development workstation.
-
Internet access.
-
Install the
phppackage:[root@dev ~]# yum install php -
Download the zip archive of
aws-sdkfor PHP and extract it. -
Create a project directory:
[user@dev ~]$ mkdir php_s3 [user@dev ~]$ cd php_s3 -
Copy the extracted
awsdirectory to the project directory. For example:[user@dev ~]$ cp -r ~/Downloads/aws/ ~/php_s3/ -
Create the connection file:
[user@dev ~]$ vim conn.php -
Paste the following contents in the
conn.phpfile: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_NODEwith the FQDN of the gateway node. ReplacePATH_TO_AWSwith the absolute path to the extractedawsdirectory that you copied to thephpproject directory.Save the file and exit the editor.
-
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. -
Create a new file for creating a bucket:
[user@dev ~]$ vim create_bucket.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f create_bucket.php -
Create a new file for listing owned buckets:
[user@dev ~]$ vim list_owned_buckets.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f list_owned_buckets.phpThe output should look similar to this:
my-new-bucket3 2020-01-21 10:33:19 UTC -
Create an object by first creating a source file named
hello.txt:[user@dev ~]$ echo "Hello World!" > hello.txt -
Create a new php file:
[user@dev ~]$ vim create_object.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f create_object.phpThis will create the object
hello.txtin bucketmy-new-bucket3. -
Create a new file for listing a bucket’s content:
[user@dev ~]$ vim list_bucket_content.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f list_bucket_content.phpThe output will look similar to this:
hello.txt 12 Fri, 22 Jan 2020 15:54:52 GMT -
Create a new file for deleting an empty bucket:
[user@dev ~]$ vim del_empty_bucket.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f del_empty_bucket.php | echo $?If the bucket is successfully deleted, the command will return0as output.Important: Deleting a non-empty bucket is currently not supported in PHP 2 and newer versions ofaws-sdk.Note: Edit thecreate_bucket.phpfile to create empty buckets, for example,my-new-bucket4,my-new-bucket5. Next, edit thedel_empty_bucket.phpfile accordingly prior to deleting the empty buckets. -
Create a new file for deleting an object:
[user@dev ~]$ vim delete_object.phpPaste 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.
-
Run the file:
[user@dev ~]$ php -f delete_object.phpThis will delete the object
hello.txt.