PHP code example of incursus / laravel-s3-tools

1. Go to this page and download the library: Download incursus/laravel-s3-tools 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/ */

    

incursus / laravel-s3-tools example snippets


// Instantiate an Amazon S3 client.
$s3 = new S3Client([
	'version' => 'latest',
	'region'  => 'us-west-2'
]);

// Fetch the latest version of a file
try {
    $s3->putObject([
        'Bucket' => 'my-bucket',
        'Key'    => 'myfile.png',
				'VersionId' => 'fiWFsPPFwbvlGh37rB9IaZYkO4pzOgWGz'
    ]);
} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error retrieving the file.\n";
}

$file = Storage::disk('s3-tools')->getVersion($versionId)->get('myfile.png');

	...
	'providers' => [
		...
		Incursus\LaravelS3Tools\S3ToolsServiceProvider::class,
		...
	],
	...




return [

    ...

    'disks' => [

				...

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

				// Add this entry
				env('S3_TOOLS_DISK_NAME', 's3-tools') => [
          'driver' => env('S3_TOOLS_DISK_NAME', 's3-tools'),
          'key' => env('AWS_ACCESS_KEY_ID'),
          'secret' => env('AWS_SECRET_ACCESS_KEY'),
          'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
          'bucket' => env('AWS_BUCKET'),
          'url' => env('AWS_URL'),
        ],
      ],
	...

$versions = Storage::disk('s3-tools')->getObjectVersions('myfile.png');

foreach($versions as $v)
{
	echo '<li> Version ID: ' . $v['versionId'];
	echo '<li> File Size: ' . $v['fileSize'] . ' bytes';
	echo '<li> Is Latest Version?: ' . $v['isLatest']; // Will be true or false (boolean)
	echo '<li> Date Modified: ' . $v['dateModified'];
	echo '<li> ----------------------------------------------';
}

// Fetch the latest version of the file from S3
$file = Storage::disk('s3-tools')->get('myfile.png');
	
// Show the image in the browser
return response($file)->header('Content-Type', 'image/png');

// Fetch the image from S3
$versionId = 'fiWFsPPFwbvlGh37rB9IaZYkO4pzOgWGz';
$file = Storage::disk('s3-tools')->getVersion($versionId)->get('myfile.png');
	
// Show the image in the browser
return response($file)->header('Content-Type', 'image/png');

$result = Storage::disk('s3-tools')->delete('some/longer/S3/path/business-plan.pdf');

$versionId = 'fiWFsPPFwbvlGh37rB9IaZYkO4pzOgWGz';
$result = Storage::disk('s3-tools')->getVersion($versionId)->delete('some/longer/S3/path/business-plan.pdf');

$result = Storage::disk('s3-tools')->setOption('VersionId', $versionString)->get('myfile.png');

$options = [
	'VersionId' => 'fiWFsPPFwbvlGh37rB9IaZYkO4pzOgWGz',
	'IfModifiedSince' => '2 days ago'
];

$result = Storage::disk('s3-tools')->setOptions($options)->delete('myfile.png');


// Retrieve a specific version of a file
$versionId = 'fiWFsPPFwbvlGh37rB9IaZYkO4pzOgWGz';
$file = Storage::disk('s3-tools')->setOption('VersionId', $versionId)->get('myfile.png');

// Clear out ll of our options
$file = Storage::disk('s3-tools')->clearOptions();

// or alternatively, just clear the 'VersionId' option
//$file = Storage::disk('s3-tools')->clearOption('VersionId');

// Get the latest version of another file ...
$file = Storage::disk('s3-tools')->get('myfile.png');


$result = Storage::disk('s3-tools')->command('ListObjectVersions', [
	'Prefix' => 'some/longer/S3/path/business-plan.pdf'
]);

- Version ID: WX6q0O9qkcAcqld3DidZo2m5z68uGKnn
- File Size: 132645 bytes
- Is Latest Version?: 1
- Date Modified: 2019-03-21T19:35:29+00:00
----------------------------------------------
- Version ID: nMw5IAmOPdMK0MR3eXtkSPVQTd18Vucd
- File Size: 3631 bytes
- Is Latest Version?:
- Date Modified: 2019-03-21T19:16:26+00:00
----------------------------------------------
...

$result = Storage::disk('s3-tools')->command('ListObjectVersions', [
	'Bucket' => 'MyBucketName',
	'Prefix' => 'some/longer/S3/path/business-plan.pdf'
]);