matecat / simple-s3
Simple S3 Client
Installs: 7 819
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 11
Forks: 4
Open Issues: 1
Requires
- php: >=5.6
- ext-curl: ^7.3
- ext-fileinfo: ^7.3
- ext-json: *
- ext-redis: *
- aws/aws-sdk-php: ^3.322
- predis/predis: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3
- monolog/monolog: ^1.24
- phpstan/phpstan: ^0.11.8
- phpunit/phpunit: ~5.7
- symfony/console: ^3.4.17 || ^4.1.6
- dev-master
- v1.5.12
- v1.5.11
- v1.5.10
- v1.5.9
- v1.5.8
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.8
- v1.3.7
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-Ostico-patch-1
- dev-renovate/configure
- dev-metadata
- dev-caching-v1
This package is auto-updated.
Last update: 2024-10-25 08:45:25 UTC
README
Simple S3 Client is a simple wrapper of the official SDK PHP Client.
Basic Usage
To instantiate the Client do the following:
use Matecat\SimpleS3\Client; $s3Client = new Client([ 'version' => 'latest', // REQUIRED 'region' => 'us-west-2', // REQUIRED 'credentials' => [ // OPTIONAL 'key' => 'YOUR-ACCESS-KEY', 'secret' => 'YOUR-SECRET-KEY', 'token' => 'SESSION-TOKEN', ] ];
Please note that if you don't provide the credentials array, the Client will try to get values from the following environments variables on your system as the original S3Client:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
If you instead want to authenticate assuming an IAM Role in another AWS Account do the following:
use Matecat\SimpleS3\Client; $s3Client = new Client([ 'version' => 'latest', 'region' => 'us-west-2', 'iam' => [ 'arn' => 'arn:aws:iam::123456789012:role/xaccounts3acces', 'session' => 's3-access-example', ] ];
For further config details please refer to the official documentation:
Configuration for the AWS SDK for PHP Version 3
Methods
Here is the list of Client's public methods:
clearBucket
- clear a bucket from all filescopyFolder
- copy the items from a folder to another onecopyItem
- copy an item from a bucket to another onecopyInBatch
- copy in batch items from a bucket to another onecreateBucketIfItDoesNotExist
. create a bucket if it does not existscreateFolder
. create an empty folder in a bucket if it does not existsdeleteBucket
- delete a bucketdeleteBucketPolicy
- delete the bucket policydeleteFolder
- delete a folderdeleteItem
- delete an itemdownloadItem
- download an itemenableAcceleration
- enable the acceleration mode for a bucketgetBucketLifeCycle
get the bucket lifecycle configurationgetBucketPolicy
get the bucket policygetBucketSize
get the size (in Bytes) of files in a bucketgetItem
- get all informations for an itemgetItemsInABucket
get an array of items in a bucketgetItemsInAVersionedBucket
get an array of items in a versioned bucketgetCurrentItemVersion
- get the latest version of an itemgetPublicItemLink
- get the public link to download the itemhasBucket
- check if a bucket existshasFolder
- check if a folder existshasItem
- check if an item existsisBucketVersioned
- check if bucket has versioned enabledopenItem
- get the content of an itemrestoreItem
- try to restore an item from archivesetBucketLifecycleConfiguration
- set bucket lifecycle configurationsetBucketPolicy
- set the bucket policysetBucketVersioning
- set the bucket versioningtransfer
- transfer content from/to bucketsuploadItem
- upload an item to a bucket from a fileuploadItemFromBody
- upload an item to a bucket from the body content
Naming validation for buckets and objects
Please refer to the official AWS policy:
The Client comes with two validators:
S3BucketNameValidator
S3ObjectSafeNameValidator
These two classes throws you an InvalidS3NameException
if the name provided is not compliant with the AWS rule conventions.
Validators are invoked in Client's createBucketIfItDoesNotExist
, uploadFileFromBody
and uploadFile
methods.
Objects name escaping
Please read carefully the object safe naming rules.
Escaping object names is entirely up to you.
You can use the provided Matecat\SimpleS3\Components\Encoders\UrlEncoder
class, or inject in Client your own encoder if you prefer, but please note that it MUST implement
Matecat\SimpleS3\Components\Encoders\SafeNameEncoderInterface
interface:
... use Matecat\SimpleS3\Components\Encoders\UrlEncoder; $encoder = new UrlEncoder(); $s3Client->addEncoder($encoder);
Filename trimming
As the Amazon official documentation reports, the filename max size on S3 is 1024 characters.
But you MUST be aware of filename max size of your operating system.
For example, if you run the application on a Linux system, this limit is 255 bytes, so you cannot download a file with a name which exceeds this value.
S3Client comes with a FilenameTrimmer
helper class, which automatically trims the filenames. Default limit value is 255.
To override this limit, use setFilenameMaxSize
method:
... $client->setFilenameMaxSize(512);
Bucket lifecycle
You can set the basic lifecycle for your bucket with setBucketLifecycleConfiguration
method.
... $s3Client->setBucketLifecycleConfiguration(['bucket' => $this->bucket, 'rules' => [...]]);
For further details please refer to the bucket lifecycle configuration official API documentation.
Bucket policy
You can set the bucket policy using setBucketPolicy
method. Consider the following example:
... $s3Client->setBucketPolicy([ 'bucket' => 'mauretto78-bucket-test-policy', 'policy' => '{ "Version": "2012-10-17", "Id": "Versioning", "Statement": [ { "Effect": "Deny", "Principal": "*", "Action": "s3:GetBucketVersioning", "Resource": "arn:aws:s3:::mauretto78-bucket-test-policy" } ] }' ]);
You can grab bucket policy and delete it by using respectively getBucketPolicy
and deleteBucketPolicy
methods.
For further details please refer to the bucket policy official API documentation.
Bucket versioning
You can enable bucket versioning:
... $s3Client->setBucketVersioning(['bucket' => $this->bucket]);
And now, when you use getItemsInABucket
method, a <VERSION_ID> tag will be added to keys:
... // getItemsInABucket() will return something like this: $notHydrated = [ 'key<VERSION_ID=123456789>', 'key<VERSION_ID=234567890>', 'key<VERSION_ID=345678901>', ]; $hydrated = [ 'key<VERSION_ID=123456789>' => 'content', 'key<VERSION_ID=234567890>' => 'content', 'key<VERSION_ID=345678901>' => 'content', ];
For further details please refer to the bucket versioning official API documentation.
Restoring an item
You can use restoreItem
to send a restore an archived object. You can choose between three retrieval options:
Bulk
(lasts 5-12 hours)Expedited
(default, lasts 1-5 minutes)Standard
(lasts 3-5 hours)
For further details please refer to the official documentation:
Caching
In order speed up data retrieval, you can inject a cache handler. Please note that the cache MUST implement Matecat\SimpleS3\Components\Cache\CacheInterface
.
The client comes with a Redis implementation:
... use Matecat\SimpleS3\Components\Cache\RedisCache; $redis = new Predis\Client(); $cacheAdapter = new RedisCache($redis); $s3Client->addCache($cacheAdapter);
Now getItemsInABucket
method will get the elements directly from cache. Please note that caching works ONLY if you provide a prefix to the method:
... // this will get keys from cache $s3Client->getItemsInABucket([ 'bucket' => 'your-bucket', 'prefix' => 'prefix/', 'hydrate' => true // false by default. If true is set the method returns an array of Aws\ResultInterface ]); // this will EVER get keys from S3 $s3Client->getItemsInABucket('your-bucket');
If you need to skip the cache you can add an extra parameter called exclude-cache
:
... // this will get keys from S3 $s3Client->getItemsInABucket([ 'bucket' => 'your-bucket', 'prefix' => 'prefix/', 'exclude-cache' => true ]);
Commands
If you have an application which uses Symfony Console, you have some commands available:
ss3:batch:transfer
Transfer files from/to a bucket.ss3:bucket:clear
Clears a bucket.ss3:bucket:create
Creates a bucket.ss3:bucket:delete
Deletes a bucket.ss3:cache:flush
Flush all data stored in cache.ss3:cache:stats
Get the cache statistics.ss3:folder:copy
Copy the items from a folder to another one.ss3:item:copy
Copy an object from a bucket to another one.ss3:item:delete
Deletes an object from a bucket.ss3:item:download
Download an object from a bucket.ss3:item:upload
Upload an object into a bucket.
You can register the commands in your app, consider this example:
#!/usr/bin/env php <?php set_time_limit(0); require __DIR__.'/../vendor/autoload.php'; $config = parse_ini_file(__DIR__.'/../config/credentials.ini'); $s3Client = new \Matecat\SimpleS3\Client( [ 'version' => $config['VERSION'], 'region' => $config['REGION'], 'credentials' => [ 'key' => $config['ACCESS_KEY_ID'], 'secret' => $config['SECRET_KEY'] ] ] ); $redis = new Predis\Client(); $cacheAdapter = new \Matecat\SimpleS3\Components\Cache\RedisCache($redis); $s3Client->addCache($cacheAdapter); // create symfony console app $app = new \Symfony\Component\Console\Application('Simple S3', 'console tool'); // add commands here $app->add(new \Matecat\SimpleS3\Console\BatchTransferCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\BucketClearCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\BucketCreateCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\BucketDeleteCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\CacheFlushCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\CacheStatsCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\FolderCopyCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\ItemCopyCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\ItemDeleteCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\ItemDownloadCommand($s3Client)); $app->add(new \Matecat\SimpleS3\Console\ItemUploadCommand($s3Client)); $app->run();
Logging
You can inject your logger to log every Client outcome call. Please note that your logger MUST be PSR-3 compliant:
... // $logger MUST implement Psr\Log\LoggerInterface $s3Client->addLogger($logger);
Support
If you found an issue or had an idea please refer to this section.
Authors
- Mauro Cassani - github
License
This project is licensed under the MIT License - see the LICENSE.md file for details