Use Z1 Storage with AWS SDK for PHP
Z1 storage is fully compatible with AWS S3 SDK, this allows to integrate our storage solution to your application using PHP programing language.
Prerequisites for hosting
- PHP 7.16.2 or later
- OPCache
- Composer
Installing AWS SDK for PHP
Composer is recommended way to install the AWS SDK for PHP.
It’s a tool that manages and installs the dependencies of your project. For more information follow Composer website
Installing Composer
- For Windows, download and run the Composer-Setup.exe
- For Linux, follow the command-line installation on the Composer download page.
Add AWS SDK for PHP as a dependency via Composer
If Composer is already installed globally on your system, run the following in the base directory of your project to install AWS SDK for PHP as a dependency:
composer require aws/aws-sdk-php
Otherwise type this Composer command to install the latest version of the AWS SDK for PHP as a dependency.
php -d memory_limit=-1 composer.phar require aws/aws-sdk-php
Add autoloader to your php scripts
To use AWS SDK for PHP in your scripts, include the autoloader in your php file.
<?php
require '/path/to/vendor/autoload.php';
?>
Get your Z1 Storage integration Credentials
- Login to Z1 Storage
- Open Integrations page
- On S3 Compatible Keys press Reveal All Keys button
- Copy Endpoint URL, Access Key and Secret Key
- Store this data somewhere safe and don’t share it with anyone
Connect to Z1 Storage with php
We call S3Client library instance after we require autoload.php
<?php
require 'vendor/autoload.php';
use AwsS3S3Client;
In php file we define our access key, secret key and endpoint: https://s3.z1storage.com:443
define('Z1_KEY', 'place access key here');
define('Z1_SECRET_KEY', 'place secret key here');
$ENDPOINT = 'https://s3.z1storage.com:443';
Instantiate S3 Client class, code does not change and creates connection to Z1 Storage with your credentials
// Instantiate the S3 client class
$client = new S3Client([
'region' => 'default',
'version' => '2006-03-01',
'endpoint' => $ENDPOINT,
'credentials' => [
'key' => Z1_KEY,
'secret' => Z1_SECRET_KEY,
],
'use_path_style_endpoint' => true,
]);
We successfully initialized client to Z1 Storage, now we can use it to call various actions.
Our final code should look like this:
<?php
require 'vendor/autoload.php';
use AwsS3S3Client;
define('Z1_KEY', 'place access key here');
define('Z1_SECRET_KEY', 'place secret key here');
$ENDPOINT = 'https://s3.z1storage.com:443';
// Instantiate the S3 client class
$client = new S3Client([
'region' => 'default',
'version' => '2006-03-01',
'endpoint' => $ENDPOINT,
'credentials' => [
'key' => Z1_KEY,
'secret' => Z1_SECRET_KEY,
],
'use_path_style_endpoint' => true,
]);
Listing your own buckets
Listing your buckets is very simple as we already made a connection and have client instance.
We just call listBuckets method.
$listResponse = $client->listBuckets();
This will get AWSResult instance on $listResponse variable as an array.
Now we can list our buckets and display data.
$listResponse = $client->listBuckets();
$buckets = $listResponse['Buckets'];
This will get us output similar to this:
Array
(
[0] => Array
(
[Name] => apiconnection
[CreationDate] => AwsApiDateTimeResult Object
(
[date] => 2021-04-30 14:34:08.546000
[timezone_type] => 2
[timezone] => Z
)
)
[1] => Array
(
[Name] => tester12
[CreationDate] => AwsApiDateTimeResult Object
(
[date] => 2021-04-16 12:45:16.318000
[timezone_type] => 2
[timezone] => Z
)
)
)
Full Code example:
<?php
require 'vendor/autoload.php';
use AwsS3S3Client;
define('Z1_KEY', 'place access key here');
define('Z1_SECRET_KEY', 'place secret key here');
$ENDPOINT = 'https://s3.z1storage.com:443';
// Instantiate the S3 client class
$client = new S3Client([
'region' => 'default',
'version' => '2006-03-01',
'endpoint' => $ENDPOINT,
'credentials' => [
'key' => Z1_KEY,
'secret' => Z1_SECRET_KEY,
],
'use_path_style_endpoint' => true,
]);
$listResponse = $client->listBuckets();
$buckets = $listResponse['Buckets'];
print('<pre>'.print_r($buckets,true).'</pre>');
Creating a bucket
Creating a bucket is same as listing own buckets, we use same client instance and call createBucket method.
Before creating a bucket, there are some rules for bucket naming
- Bucket names must be between 3 and 63 characters long.
- Bucket names can consist only of lowercase letters, numbers, dots (.), and hyphens (-).
- Bucket names must begin and end with a letter or number.
- Bucket names must not be formatted as an IP address (for example, 192.168.5.4).
Now let’s create a bucket. Change “bucketname” to your desired value.
$client->createBucket(['Bucket' => 'bucketname']);
You can check newly created bucket with listBuckets method explained previously.
Deleting own bucket
This is similar to creating a bucket. Change “bucketname” to your desired value.
$client->deleteBucket(['Bucket' => 'bucketname']);
You can check newly created bucket with listBuckets method explained previously.
Creating an object
Again we utilize our client instance.
- Change Bucket parameter to bucket name you want to upload file
- Key is file name which will be added to bucket
- Body is file contents it can be PHP $_FILES['file'] element from POST action.
$client->putObject([
'Bucket' => 'bucketname',
'Key' => 'hello.txt',
'Body' => "Hello World!"
]);
Execute this code to create object called hello.txt with content “Hello World !”
List bucket contents
- Change bucketname in listObjects method to your bucketname
- Execute the code
$objects = $client->listObjects(['Bucket' => 'bucketname']);
print('<pre>'.print_r($objects['Contents'],true).'</pre>');
This will output bucket contents, if you followed previous step you should see object we uploaded previously.
Array
(
[0] => Array
(
[Key] => hello.txt
[LastModified] => AwsApiDateTimeResult Object
(
[date] => 2021-04-30 15:13:57.430000
[timezone_type] => 2
[timezone] => Z
)
[ETag] => "ed076287532e86365e841e92bfc50d8c"
[Size] => 12
[StorageClass] => STANDARD
[Owner] => Array
(
[DisplayName] => YOURDISPLAYNAME
[ID] => YOURID
)
)
)
Download an object to a file
Let’s download object we put previously
- Define bucket name in bucket parameter on getObject method.
- Define object Key in Key parameter on getObject method.
- Execute the code
$object = $client->getObject(
[
'Bucket' => 'bucketname',
'Key' => 'hello.txt',
]
);
file_put_contents('hello.txt', $object['Body']->getContents());
This will download object to your working directory.
Delete an object
To delete an object we utilize deleteObject method
- Define bucket name in bucket parameter on deleteObject method.
- Define object Key in Key parameter on deleteObject method.
- Execute the code
$client->deleteObject(
[
'Bucket' => 'bucketname',
'Key' => 'hello.txt',
]
);
You can check if file was removed by executing listObjects methods defined previously