PHP Classes

PHP AWS S3 Client SDK Wrapper: Manage files stored in the AWS S3 cloud storage

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 36 All time: 10,981 This week: 206Up
Version License PHP version Categories
aws-sdk-php-wrapper 1.0.0MIT/X Consortium ...5PHP 5, Web services
Description 

Author

This package can manage files stored in the AWS S3 cloud storage.

It can call the PHP AWS SDK client classes to send HTTP requests to the AWS to perform several types of operations on files stored in the AWS S3 service.

Currently, it can access the account of the AWS service user and:

- Store the contents of a given local file in a directory

- Get the data contents of a given bucket of a directory

- Get the list of buckets

- Get the URL of a given bucket

- Get the contents of a bucket with a given key

- Delete a bucket with a given key

Picture of Ogbemudia Osayawe
  Performance   Level  
Name: Ogbemudia Osayawe <contact>
Classes: 9 packages by
Country: Germany Germany
Age: 41
All time rank: 2885172 in Germany Germany
Week rank: 192 Up13 in Germany Germany Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php
require_once __DIR__ .'/../vendor/autoload.php';
use
Devscreencast\S3Wrapper\Storage;

$env = new \Dotenv\Dotenv(basePath());
$env->load();

try{
   
/**
     * Create an instance of Devscreencast\S3Wrapper\Storage class
     * and pass in config array
     */
   
$storage = new Storage([
      
'region' => 'us-east-1',
      
'version' => 'latest',
      
'bucket' => 'learning-s3-bucket.com'
   
]);
   
    if(isset(
$_POST['submit'])){
       
$name = $_FILES['file']['name'];
        if(!
$name){
           
$msg = 'Please select a file';
        }
       
/**
         * @param $key, the name given to the file upload field in the html form
         * @param $folder (optional) if omitted file in placed inside bucket
         * @param $filename (optional) if omitted a unique filename is generated
         * @param $options (optional) https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject
         */
      
$path = $storage->store('file', 'avatars/');
    }
   
    if(isset(
$_GET['delete'])){
       
$item = $_GET['delete'];
        if(!
$item){
           
$msg = 'Item is required';
        }else{
            if(
$storage->delete($item)){
               
$msg = $item . ' deleted successfully';
            }else{
               
$msg = 'Item not found';
            }
        }
    }
   
   
$bucket_contents = $storage->getBucketContents();
   
}catch (
Exception $ex){
    die(
$ex->getMessage());
}

require_once
__DIR__ .'/app.php';


Details

aws-sdk-php-wrapper

A simple wrapper over aws php sdk to perform basic file management task

Installation

composer require devscreencast/aws-sdk-php-wrapper

Basic Usage

Environment Variables

Create a .env file with the following:

AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_secret

<?php
require_once 'vendor/autoload.php';
use Devscreencast\S3Wrapper\Storage;

#load environment variables
$env = new \Dotenv\Dotenv('path_to/.env');
$env->load();

try{
    /
     * Create an instance of Devscreencast\S3Wrapper\Storage class
     * and pass in config array
     */
    $storage = new Storage([
       'region' => 'us-east-1',
       'version' => 'latest',
       'bucket' => ''
    ]);
    
    if(isset($_POST['submit'])){
        $name = $_FILES['file']['name'];
        if(!$name){
            $msg = 'Please select a file';
        }
        
      /
       * @param $key, the name given to the file upload field in the html form
       * @param $folder (optional) if omitted file in placed inside bucket
       * @param $filename (optional) if omitted a unique filename is generated
       * @param $options (optional) https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putobject
       */
       $path = $storage->store('file', 'avatars/', $name);
    }
    
    # delete a file from bucket
    if(isset($_GET['delete'])){
        $item = $_GET['delete'];
        if(!$item){
            $msg = 'Item is required';
        }else{
            if($storage->delete($item)){
                $msg = $item . ' deleted successfully';
            }else{
                $msg = 'Item not found';
            }
        }
    }
    
    # get all objects from a buckets as iterator instance
    $bucket_contents = $storage->getBucketContents();
    foreach ($bucket_contents as $bucket_content){
            echo $bucket_contents['Key'] . '<br />';
    }
   
}catch (Exception $ex){
    die($ex->getMessage());
}

For complete example / demo refer to example folder


  Files folder image Files (10)  
File Role Description
Files folder imageexample (2 files)
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file .env.example Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (10)  /  example  
File Role Description
  Accessible without login Plain text file app.php Example Example script
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (10)  /  src  
File Role Description
Files folder imagehelpers (2 files)
  Plain text file Storage.php Class Class source

  Files folder image Files (10)  /  src  /  helpers  
File Role Description
  Plain text file FileUploadHelper.php Class Class source
  Accessible without login Plain text file functions.php Aux. Auxiliary script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:36
This week:0
All time:10,981
This week:206Up