PHP code example of dnx / php-s3

1. Go to this page and download the library: Download dnx/php-s3 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dnx / php-s3 example snippets



use DNX\S3\CreateItem;
use DNX\S3\ReadItem;
use DNX\S3\DeleteItem;

use Aws\Credentials\CredentialProvider;
use Aws\S3\Exception\S3Exception;
use \Exception;
use Log;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct(){}


    public function listObjects(Request $request)
    {
        try {
            $query  = (object)$request->all();
            // Region sage(), 'code' => $e->getStatusCode() ]; 
        } catch(Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getMessage() ];
        }
    }
    
    public function getObject(Request $request)
    {
        try {
            $query  = (object)$request->all();
            // CredentialProvider is optional, we recommend to use user roles and do not use credentials
            $readS3 = new ReadItem('ap-southeast-2', CredentialProvider::env());
            $object = $readS3->getObject($query->bucket, $query->filePath);
            $headers = [
                'Content-Type'        => $object->file['ContentType'],
                'Content-Disposition' => 'attachment; filename=' . $query->file
            ];
            $stream = function () use ($object) {
                echo $object->file['Body'];
            };
            return new StreamedResponse($stream, 200, $headers);    // If you want to download it
            // return (string)$object->file['Body'];                // If you want to manipulate file body
        } catch(S3Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getAwsErrorMessage(), 'code' => $e->getStatusCode() ];
        } catch(Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getMessage() ];
        }
    }

    public function getCloudFrontURL(Request $request)
    {
        try {
            $query      = (object)$request->all();
            // CredentialProvider is optional, we recommend to use user roles and do not use credentials
            $readS3     = new ReadItem('ap-southeast-2', CredentialProvider::env());
            $url        = $readS3->getCloudFrontURL($query->bucket, $query->filePath);
            return $url;
        } catch(S3Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getAwsErrorMessage(), 'code' => $e->getStatusCode() ];
        } catch(Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getMessage() ];
        }
    }

    public function uploadObjects(Request $request)
    {
        // https://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-commands.html#executing-commands-in-parallel
        // Without ContentType parameter the file will be downloaded instead of served
        try {
            $query      = (object)$request->all();
            $files      = [];
            foreach ($query->files as $key => $file ) {
                $key = (isset($query->path) ? $query->path . "/" : "") . $file->getClientOriginalName();
                array_push($files, [
                    'Key'           => $key,     
                    'Body'          => $file->get(),
                    'ContentType'   => $file->getClientMimeType() 
                ]);
            }
            // CredentialProvider is optional, we recommend to use user roles and do not use credentials
            $createS3   = new CreateItem('ap-southeast-2', CredentialProvider::env());
            $res        = $createS3->uploadObjects($query->bucket, $files);
            foreach($res->results as $index => $result)
            {
                if($result instanceof Exception) {
                    $message   = $result->getMessage();
                    Log::error("Error on creating file: {$message}");
                } else {
                    // Database persist for each file here
                    Log::info("Creating file completed");
                }
            }
            return $res;
        } catch (Exception $e) {
            $message   = $e->getMessage();
            Log::error("S3Client Bulk Load Error: {$message}");
        }
    }

    public function deleteObject(Request $request)
    {
        try {
            $query      = (object)$request->all();
            // CredentialProvider is optional, we recommend to use user roles and do not use credentials
            $deleteS3   = new DeleteItem('ap-southeast-2', CredentialProvider::env());
            $res        = $deleteS3->deleteObject($query->bucket, $query->filePath);
            return $res;
        } catch(S3Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getAwsErrorMessage(), 'code' => $e->getStatusCode() ];
        } catch(Exception $e) {
            return [ 'response' => 'error', 'message' => $e->getMessage() ];
        }
    }
}
bash
composer