定制 matecat/simple-s3 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

matecat/simple-s3

Composer 安装命令:

composer require matecat/simple-s3

包简介

Simple S3 Client

README 文档

README

Tests license Packagist Quality Gate Status Coverage Reliability Rating Maintainability Rating

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 files
  • copyFolder - copy the items from a folder to another one
  • copyItem - copy an item from a bucket to another one
  • copyInBatch - copy in batch items from a bucket to another one
  • createBucketIfItDoesNotExist . create a bucket if it does not exists
  • createFolder . create an empty folder in a bucket if it does not exists
  • deleteBucket - delete a bucket
  • deleteBucketPolicy - delete the bucket policy
  • deleteFolder - delete a folder
  • deleteItem - delete an item
  • downloadItem - download an item
  • enableAcceleration - enable the acceleration mode for a bucket
  • getBucketLifeCycle get the bucket lifecycle configuration
  • getBucketPolicy get the bucket policy
  • getBucketSize get the size (in Bytes) of files in a bucket
  • getItem - get all informations for an item
  • getItemsInABucket get an array of items in a bucket
  • getItemsInAVersionedBucket get an array of items in a versioned bucket
  • getCurrentItemVersion - get the latest version of an item
  • getPublicItemLink - get the public link to download the item
  • hasBucket - check if a bucket exists
  • hasFolder - check if a folder exists
  • hasItem - check if an item exists
  • isBucketVersioned - check if bucket has versioned enabled
  • openItem - get the content of an item
  • restoreItem - try to restore an item from archive
  • setBucketLifecycleConfiguration - set bucket lifecycle configuration
  • setBucketPolicy - set the bucket policy
  • setBucketVersioning - set the bucket versioning
  • transfer - transfer content from/to buckets
  • uploadItem - upload an item to a bucket from a file
  • uploadItemFromBody - 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:

Restore an archived object

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

License

This project is licensed under the MIT License - see the LICENSE.md file for details

matecat/simple-s3 适用场景与选型建议

matecat/simple-s3 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.1k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2019 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「s3」 「sdk」 「aws」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 matecat/simple-s3 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 matecat/simple-s3 我们能提供哪些服务?
定制开发 / 二次开发

基于 matecat/simple-s3 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 9
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-14