Neo Object Storage Management Using PHP

Written By Support Team (Administrator)

Updated at February 14th, 2021

NEO Object Storage is the S3 protocol service that can be used to store static data such as pictures, videos, music, and others. It is compatible with Amazon S3, so you can take advantage of NEO Object Storage for your S3 Protocol needs. NEO Object Storage can be managed or managed using the Amazon SDK, of course, to make it easier for you to integrate your PHP applications with NEO Object Storage service.

 

PHP stands for (Hypertext Preprocessor), in short, PHP is a server-side scripting programming language designed for website development or creation.

 

For example, if you have a dynamic or static website using PHP, then you can save assets such as images, videos, music on that website to NEO Object Storage.

 

To follow this guide, make sure you meet the following requirements: 

 

INSTALLATION

 

We use Ubuntu 20.04 LTS operating system for this guide, here are the installation instructions.

 

  1. Checking the operating system used.

 

 

 

  1. For installing PHP 7 on Ubuntu you can refer to this Knowledge Base: How to Install PHP (7.1, 7.2, & 7.3) using PPA on Ubuntu 18.04 LTS
  2. Installing composer on Ubuntu 20.04 LTS

 

ubuntu @ neo-kb: ~ $ curl - sS https://getcomposer.org/installer -o composer-setup.php

ubuntu @ neo-kb: ~ $ HASH = `curl -sS https: // composer.github.io / installer.sig`

ubuntu @ neo-kb : ~ $ php -r "if (hash_file ('SHA384', 'composer-setup.php') === '$ HASH') {echo 'Installer verified';} else {echo 'Installer corrupt'; unlink (' composer-setup.php ');} echo PHP_EOL; "

Installer verified

ubuntu @ neo-kb: ~ $

ubuntu @ neo-kb: ~ $ ls

composer-setup.php

ubuntu @ neo-kb: ~ $ sudo php composer-setup.php --install-dir = / usr / local / bin --filename = composer

 

 

  1. Installing the AWS SDK using composer:

 

ubuntu @ neo-kb: ~ $ sudo mkdir neo-object-storage

ubuntu @ neo-kb: ~ $ cd neo-object-storage /

ubuntu @ neo-kb: ~ / neo -object-storage $

ubuntu @ neo-kb: ~ / neo-object-storage $ sudo composer require aws / aws-sdk-php


 

  1. If the AWS SDK is installed it will look like the following:

 

ubuntu @ neo-kb: ~ / neo-object -storage $ ll

total 40

drwxr-xr-x 3 root root        4096 Dec 1 17:58 ./

drwxr-xr-x 7 ubuntu ubuntu 4096 Dec 1 17:57 ../

-rw-r - r-- 1 root root          63 Dec 1 17:58 composer.json

-rw-r - r-- 1 root root 22154 Dec 1 17:58 composer.lock

drwxr-xr-x 10 root root      4096 Dec 1 17:59 vendor /

ubuntu @ neo -kb: ~ / neo-object-storage $

 

Please note to define composer autoloader with PHP SDK. You can use the following script

require 'vendor / autoload.php';

use Aws \ S3 \ S3Client;

use Aws \ S3 \ Exception \ S3Exception;

 

S3 NEO Object Storage CONNECTION

 

To connect NEO Object Storage with PHP SDK you can use the following PHP script:

 

$ client = S3Client :: factory ([

    'version' => 'latest',

    'region' => 'idn',

    'endpoint' => 'https: // ENDPOINT-S3-YOU',

    'credentials' => [

        'key' => "YOUR-S3-KEY",

        'secret' => "SECRET-KEY-S3-YOU"

    ]

 ]) ;

 

Creating a Bucket

 

To create NEO Object Storage bucket using the PHP SDK, you can use the following script: 

 

try {

            $ result = $ client-> createBucket ([

    'Bucket' => 'NAME-BUCKET-HERE',

            ]);

} catch (S3Exception $ e) {

            echo $ e-> getMessage ();

} The

 

 

Examples:

 

 

If it is finished, please run the PHP file using the PHP command: 

 

ubuntu @ neo-kb: ~ / neo-object-storage $ php create-bucket.php

ubuntu @ neo-kb: ~ / neo-object-storage $

 

For Ensuring that the bucket has been created, make sure it shown through portal.neo.id as follows: 

 

 

 

Bucket Listing

 

In addition to creating buckets, you can also view all bucket lists in your NEO Object Storage using the PHP SDK and the following sample script:

 

try {

    $ buckets = $ client -> listBuckets ();

    foreach ($ buckets ['Buckets'] as $ bucket) {

            echo $ bucket ['Name']. "\ n";

            }

} catch (S3Exception $ e) {

    echo $ e-> getMessage ();

}

 

 

Examples:

 

 

The result as follows:

 

 

Upload File (Object) to NEO Object Storage

 

To upload files (object) to NEO Object Storage can also be done using the PHP SDK, you can use the following script:

 

try {

    $ client-> putObject ([

        ' Bucket '=>' YOUR-BUCKET-NAME ',

        ' Key '=>' OBJECT-NAME-WHICH-WILL-BE-APPLIED ',

        ' ContentType '=>' image / png ', // adjust to the content you want to upload

        ' SourceFile '=>' FILE-LOCATION ', // like /var/www/vhosts/mysite/file.xls

        ' ACL' => 'public-read', // public-read or private

    ]);

} catch (S3Exception $ e) {

    // Catch an S3 specific exception.

    echo $ e-> getMessage ();

} The

 

Examples:

 

 

 

Description: 

 

Bucket: Name of Bucket

Key: Name of file (object) to upload into bucket

ContentType: Type of file (object) to be uploaded to bucket

SourceFile: Directory of file (object) is in

ACL: Permission file (object) )

 

To run it, you can use the PHP command

 

ubuntu @ neo-kb: ~ / neo-object-storage $ php upload-bucket.php

ubuntu @ neo-kb: ~ / neo-object-storage $

 


Make sure the image file is named Biznet-GIO -CLOUD.png is already in the bucket Bucketneo as follows.

 

 

Please access the image file above via the browser

 

 

Delete File (Object) NEO Object Storage

 

To delete files (objects) in the bucket using the PHP SDK can also be done using a script PHP SDK as follows:

 

try {

    $ result = $ client-> deleteObject ([

        'Bucket' => 'YOUR-BUCKET-NAME',

        'Key' => 'YOUR-OBJECT-NAME',

    ]);

} catch (S3Exception $ e) {

    echo $ e-> getMessage ();

}

 

Examples:

 

 

Use the PHP command to run the PHP file

 

ubuntu @ neo-kb: ~ / neo-object-storage $ php delete-file.php

ubuntu @ neo-kb: ~ / neo-object-storage $

 

Make sure the file (object ) file has been deleted

 

 

Simple Program Using PHP

 

If you already know how to upload, delete, list, and create a bucket, then you can apply it to your PHP program. Here we provide an example of a simple uploader program using PHP SDK, of course integrated with S3 NEO Object Storage.

 

Please create an index.php file and enter the PHP program code as follows in the PHP SDK directory: 

 

<? php

require 'vendor / autoload.php';

use Aws \ S3 \ S3Client;

use Aws \ S3 \ Exception \ S3Exception;

 

if (isset ($ _ FILES ['image'])) {

        $ file_name = $ _FILES ['image'] ['name'];

        $ temp_file_location = $ _FILES ['image'] ['tmp_name'];

 

$ client = S3Client :: factory ([

    'version' => 'latest',

    'region' => 'id-jkt-1',

    'endpoint' => 'http: // ENDPOINT-S3-YOU',

    'credentials '=> [

        ' key '=> "KEY-S3-YOU",

        ' secret '=> "SECRET-KEY-S3-YOU"

    ]

 ]);

 

try {

    $ client-> putObject ([

        'Bucket' => 'YOUR-BUCKET-NAME',

        'Key' => $ file_name,

        'ContentType' => 'image / png',

        'SourceFile' => $ temp_file_location, / / like /var/www/vhosts/mysite/file.csv

        'ACL' => 'public-read',

    ]);

} catch (S3Exception $ e) {

    // Catch an S3 specific exception.

    echo $ e-> getMessage ();

}

}

?>

<html>

<form action = "" method = "POST" enctype = "multipart / form-data">

<input type = "file" name = "image" />

<input type = "submit" / >

</form>

</html>

 

 

To run the above program, you can use the example web server here, we use the Apache web server on Ubuntu 20.04 LTS. 

 

 

The following is the virtual host configuration:

 

<VirtualHost *: 80>

            ServerName neo-object-storage.l1staging.my.id

            DocumentRoot / var / www / html / neo-object-storage

            ErrorLog $ {APACHE_LOG_DIR} /error.log

            CustomLog $ {APACHE_LOG_DIR } /access.log combined

</VirtualHost>

 

Enable a2ensite. Then adjust between file and directory permissions for example as follows:

 

ubuntu @ neo-kb: / var / www / html / neo-object-storage $

ubuntu @ neo-kb: / var / www / html / neo-object-storage $ ll

total 60

drwxr-xr-x 3 www-data www-data 4096 Dec 1 19:41 ./

drwxr-xr-x 3 root         root      4096 Dec 1 19:38 ../

-rw-r - r-- 1 www-data www-data        63 Dec 1 17:58 composer.json

-rw-r - r-- 1 www-data www-data 22154 Dec 1 17:58 composer.lock

-rwxr-xr-x 1 www-data www-data 979 Dec 1 19:09 index.php *

-rw-r - r-- 1 www-data www-data 561 Dec 1 18:33 listing-bucket.php

-rw-r - r-- 1 www-data www-data 510 Dec 1 18:21 create-bucket.php

-rw-r - r-- 1 www-data www-data 552 Dec 1 18:56 delete -file.php

-rw-r - r-- 1 www-data www-data 843 Dec 1 18:51 upload-bucket.php

drwxr-xr-x 10 www -data www-data 4096 Dec 1 17:59 vendor /

ubuntu @ neo-kb: / var / www / html / neo-object-storage $

ubuntu @ neo-kb: / var / www / html / neo-object-storage $ a2ensite neo.conf

Site neo already enabled

ubuntu @ neo-kb: / var / www / html / neo-object-storage $

 

If you finished, restarted the apache web server and made sure its service is running.

 

ubuntu @ neo-kb: / var / www / html / neo-object-storage $

ubuntu @ neo-kb: / var / www / html / neo-object-storage $ sudo systemctl restart apache2

ubuntu @ neo-kb: / var / www / html / neo-object- storage $ sudo systemctl apache2 status

● apache2.service - The Apache HTTP Server

            Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)

            Active: active (running) since Tue 2020-12-01 19:49:39 UTC; 4s ago

            Docs: https://httpd.apache.org/docs/2.4/

            Process: 18173 ExecStart = / usr / sbin / apachectl start (code = exited, status = 0 / SUCCESS)

   Main PID: 18192 (apache2)

            Tasks: 6 (limit: 2344)

            Memory: 11.7M

            CGroup: /system.slice/apache2.service

            ├─18192 / usr / sbin / apache2 -k start

            ├─18193 / usr / sbin / apache2 -k start

            ├─18194 / usr / sbin / apache2 -k start

            ├─18195 / usr / sbin / apache2 -k start

            ├─18196 / usr / sbin / apache2 -k start

            └─18197 / usr / sbin / apache2 -k start

 

Dec 01 19:49:39 neo-kb.local.id systemd [1]: Starting The Apache HTTP Server ...

Dec 01 19:49:39 neo-kb. local.id systemd [1]: Started The Apache HTTP Server.

ubuntu @ neo-kb: / var / www / html / neo-object-storage $

 

Please access the domain or subdomain that has been defined on the virtual host side. If successful, it will look like the image below:

 

Upload static files in the form of images, music , and others by clicking on the Browse button.

 

Please click the button Submit Query and make sure you return to portal.neo.id for the image file to upload properly.

 

Was this article helpful?