Use AWS SDK for PHP with MinIO Server
aws-sdk-php is the official AWS SDK for the PHP programming language. In this practicum we will learn how to use aws-sdk-php with the Minio server.
After successfully building Minio Server as a network storage object.
You are expected to be able to use it through the application, referring to https://docs.minio.io that the minio client can use javascript, php, python, etc. Now you will learn how to access minio using PHP scripts
aws-sdk-php
is the official AWS SDK for the PHP programming language. In this recipe we will learn how to use aws-sdk-php
with MinIO server.
Hints:
To be able to do this practicum you must have some basic knowledge, namely:
1. Learn Content Type / MIME Types on http
2. How to make a bucket using php programming language while uploading files with various extensions
3. You upload files with the help of your local web server, XAMPP
4. Make sure XAMPP is active
5. Below me include reference links to help you do the minio practicum using the Minio-SDK with the PHP programming below.
1. Prerequisites
Install MinIO Server from here.
2. Installation
Install aws-sdk-php
from AWS SDK for PHP official docs here.
3. Use GetObject and PutObject
Example below shows putObject and getObject operations on MinIO server using aws-sdk-php. Please replace endpoint
,key
, secret
, Bucket
with your local setup in this example.php
file. Note that we set use_path_style_endpoint
to true
to use MinIO with AWS SDK for PHP. Read more in the AWS SDK for PHP docs here.
Check This Out….
1. File Structure
PHP minio client has been provided by Amazon library.
Follow the steps below:
1. Download AWS-SDK-PHP zip from here
2. Create a root web folder in your Windows htdocs with the name MiniO folder
3. Extract the aws-sdk-php file that you downloaded in the minio folder
4. Open a php file named example.php and place it in the minio folder.
5. Example.php above shows the putObject and getObject operations on the Minio server using aws-sdk-php. Please replace endpoints, keys, secrets, buckets with your local settings in this example.php file. Change use_path_style_endpoint to true to use Minio with the AWS SDK for PHP.
5. Here is the file structure:
2. Create Bucket/directory
Please replace endpoint
,key
, secret
, Bucket
with your local setup in this example.php
file. Note that we set use_path_style_endpoint
to true
to use MinIO with AWS SDK for PHP. In this case, the new bucket that I will create is named “abc”
<?php
require ‘aws-autoloader.php’;
date_default_timezone_set(‘America/Los_Angeles’);
$bucket = ‘abc’;
$s3 = new Aws\S3\S3Client([
‘version’ => ‘latest’,
‘region’ => ‘us-east-1’,
‘endpoint’ => ‘http://192.168.40.141:9000',
‘use_path_style_endpoint’ => true,
‘credentials’ => [
‘key’ => ‘80MFCSW2PFV7I3PRWTG2’,
‘secret’ => ‘0wv0hTGqTOyBZYmdImioUcHy8isnUmT8syZN9ig+’,
],
]);
$result = $s3->createBucket([
‘Bucket’ => $bucket,
]);
echo ‘create bucket berhasil’;
?>
Result
please access your Minio endpoint and make sure your bucket has been added with the name abc. If it appears as shown below, you have successfully created a minio bucket with php-sdk.
After you have successfully created a new bucket using a php script then you can upload objects to the bucket. in this lab I will upload files with different MIME Types.
3. Upload PDF File into Bucket
Please replace endpoint
,key
, secret
, Bucket
with your local setup in this pdf_upload.php
file. Note that we set use_path_style_endpoint
to true
to use MinIO with AWS SDK for PHP. In this case, the file tha we want to upload is Publications_Digest_032019.pdf where located/source file at C:\Users\COMPUTER\Pictures\CSS_Publications_Digest_032019.pdf.
evidence:
Line 17:
$key = ‘file_pdfku’; //filename when it is uploaded on the web minio
Line 23:
SourceFile’ => ‘C:\Users\COMPUTER\Pictures\CSS_Publications_Digest_032019.pdf’ //where is your file that you want to upload be located from.
Line 24:
‘ContentType’ => ‘application/pdf’ //Type of your file that you want to upload using MIME TYPE convension
<?php
require ‘aws-autoloader.php’;
date_default_timezone_set(‘America/Los_Angeles’);
//$bucket = ‘abc’;
$s3 = new Aws\S3\S3Client([
‘version’ => ‘latest’,
‘region’ => ‘us-east-1’,
‘endpoint’ => ‘http://192.168.40.141:9000',
‘use_path_style_endpoint’ => true,
‘credentials’ => [
‘key’ => ‘80MFCSW2PFV7I3PRWTG2’,
‘secret’ => ‘0wv0hTGqTOyBZYmdImioUcHy8isnUmT8syZN9ig+’,
],
]);
$key = ‘file_pdfku’;
$result = $s3->putObject([
‘Bucket’ => ‘abc’,
‘Key’ => $key,
‘Body’ => ‘this is the body!’,
‘SourceFile’ => ‘C:\Users\COMPUTER\Pictures\CSS_Publications_Digest_032019.pdf’,
‘ContentType’ => ‘application/pdf’,
]);
// Print the body of the result by indexing into the result object.
var_dump($result);
Output
This is the var_dump result of the $ result array variable which indicates that the .pdf file was uploaded successfully.
Result Of Code
object(Aws\Result)#170 (2) { [“data”:”Aws\Result”:private]=> array(11) { [“Expiration”]=> string(0) “” [“ETag”]=> string(36) “”f9a348f1758fadc3e4bc9509000f872f-1"” [“ServerSideEncryption”]=> string(0) “” [“VersionId”]=> string(0) “” [“SSECustomerAlgorithm”]=> string(0) “” [“SSECustomerKeyMD5”]=> string(0) “” [“SSEKMSKeyId”]=> string(0) “” [“SSEKMSEncryptionContext”]=> string(0) “” [“RequestCharged”]=> string(0) “” [“@metadata”]=> array(4) { [“statusCode”]=> int(200) [“effectiveUri”]=> string(41) “http://192.168.40.141:9000/abc/file_pdfku" [“headers”]=> array(9) { [“accept-ranges”]=> string(5) “bytes” [“content-length”]=> string(1) “0” [“content-security-policy”]=> string(23) “block-all-mixed-content” [“etag”]=> string(36) “”f9a348f1758fadc3e4bc9509000f872f-1"” [“server”]=> string(34) “MinIO/RELEASE.2019–10–02T21–19–38Z” [“vary”]=> string(6) “Origin” [“x-amz-request-id”]=> string(16) “15CE23ED7BB9FE34” [“x-xss-protection”]=> string(13) “1; mode=block” [“date”]=> string(29) “Wed, 16 Oct 2019 13:33:42 GMT” } [“transferStats”]=> array(1) { [“http”]=> array(1) { [0]=> array(0) { } } } } [“ObjectURL”]=> string(41) “http://192.168.40.141:9000/abc/file_pdfku" } [“monitoringEvents”:”Aws\Result”:private]=> array(0) { } }
Result from Minio Dashboard
please refresh your destination bucket / place where the .pdf file will be placed on the minio dashboard.
4. Upload Foto File into Bucket
Replace endpoint
,key
, secret
, Bucket, Source file, Content Type
with your local setup in this foto_upload.php
file. Note that we set use_path_style_endpoint
to true
to use MinIO with AWS SDK for PHP.
<?php
require ‘aws-autoloader.php’;
date_default_timezone_set(‘America/Los_Angeles’);
$s3 = new Aws\S3\S3Client([
‘version’ => ‘latest’,
‘region’ => ‘us-east-1’,
‘endpoint’ => ‘http://192.168.40.141:9000',
‘use_path_style_endpoint’ => true,
‘credentials’ => [
‘key’ => ‘80MFCSW2PFV7I3PRWTG2’,
‘secret’ => ‘0wv0hTGqTOyBZYmdImioUcHy8isnUmT8syZN9ig+’,
],
]);
$key = ‘jan.jpg’;
$result = $s3->putObject([
‘Bucket’ => ‘abc’,
‘Key’ => $key,
‘Body’ => ‘this is the body!’,
‘SourceFile’ => ‘C:\Users\COMPUTER\Pictures\jansutris.jpg’,
‘ContentType’ => ‘image/jpg’,
]);
// Print the body of the result by indexing into the result object.
var_dump($result);
Result Of Code
object(Aws\Result)#170 (2) { [“data”:”Aws\Result”:private]=> array(11) { [“Expiration”]=> string(0) “” [“ETag”]=> string(36) “”678051e65a7d9701f7f3a00ece5e5a8d-1"” [“ServerSideEncryption”]=> string(0) “” [“VersionId”]=> string(0) “” [“SSECustomerAlgorithm”]=> string(0) “” [“SSECustomerKeyMD5”]=> string(0) “” [“SSEKMSKeyId”]=> string(0) “” [“SSEKMSEncryptionContext”]=> string(0) “” [“RequestCharged”]=> string(0) “” [“@metadata”]=> array(4) { [“statusCode”]=> int(200) [“effectiveUri”]=> string(38) “http://192.168.40.141:9000/abc/jan.jpg" [“headers”]=> array(9) { [“accept-ranges”]=> string(5) “bytes” [“content-length”]=> string(1) “0” [“content-security-policy”]=> string(23) “block-all-mixed-content” [“etag”]=> string(36) “”678051e65a7d9701f7f3a00ece5e5a8d-1"” [“server”]=> string(34) “MinIO/RELEASE.2019–10–02T21–19–38Z” [“vary”]=> string(6) “Origin” [“x-amz-request-id”]=> string(16) “15CE24877A875557” [“x-xss-protection”]=> string(13) “1; mode=block” [“date”]=> string(29) “Wed, 16 Oct 2019 13:44:44 GMT” } [“transferStats”]=> array(1) { [“http”]=> array(1) { [0]=> array(0) { } } } } [“ObjectURL”]=> string(38) “http://192.168.40.141:9000/abc/jan.jpg" } [“monitoringEvents”:”Aws\Result”:private]=> array(0) { } }
Result from Minio Dashboard
please refresh your destination bucket/place where the .jpg file will be placed on the minio dashboard.
Challenge:
To further enhance your understanding, What if you want to upload pictures, Ms. Excel file, database, mp3s or videos? Please explore more about using the minio client using the aws-php library.
— “Stay hungry, stay foolish!” —
References:
https://docs.minio.io/docs/how-to-use-aws-sdk-for-php-with-minio-server.html
https://docs.minio.io/docs/minio-quickstart-guide
https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
https://developer.mozilla.org/enUS/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types