sunaoka / laravel-aws-sdk-php
Laravel 的 AWS 服务提供者
1.2.0
2024-04-11 05:08 UTC
Requires
- php: ^7.2 || ^8.0
- aws/aws-sdk-php: ^3.17.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpstan/phpstan: ^1.9
This package is auto-updated.
Last update: 2024-09-16 02:42:25 UTC
README
安装
composer require sunaoka/laravel-aws-sdk-php
配置
php artisan vendor:publish --tag=aws-config
设置可以在生成的 config/aws.php
配置文件中找到。
<?php return [ 'credentials' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'token' => env('AWS_SESSION_TOKEN'), ], 'region' => env('AWS_DEFAULT_REGION'), 'version' => env('AWS_API_VERSION', 'latest'), 'endpoint' => env('AWS_ENDPOINT'), // Override Configuration for specific services // 'S3' => [ // 'use_path_style_endpoint' => false, // ], ];
使用
$s3 = \AWS::createS3(); $result = $s3->getObject([ 'Bucket' => 'Bucket', 'Key' => 'Key', ]); echo $result['Body'];
测试
您可以使用 AWS
门面的 fake
方法应用模拟处理器。
有关模拟处理器的更多信息,请参阅开发者指南。
use Aws\Result; use Aws\MockHandler; use Aws\CommandInterface; use Psr\Http\Message\RequestInterface; use Aws\Exception\AwsException; $mock = new MockHandler(); $mock->append(new Result(['Body' => 'foo'])); $mock->append(function (CommandInterface $cmd, RequestInterface $req) { return new AwsException('Mock exception', $cmd); }); \AWS::fake($mock); $s3 = \AWS::createS3(); $result = $s3->getObject([ 'Bucket' => 'Bucket', 'Key' => 'Key', ]); echo $result['Body']; // foo
也可以为每个客户端设置模拟处理器。
use Aws\DynamoDB\DynamoDbClient; use Aws\MockHandler; use Aws\Result; use Aws\S3\S3Client; $mockHandlers = [ S3Client::class => new MockHandler([ new Result(['Body' => __METHOD__]), ]), DynamoDbClient::class => new MockHandler([ new Result(['TableNames' => ['Table1', 'Table2', 'Table3']]), ]), ]; \AWS::fake($mockHandlers); $s3 = \AWS::createS3(); $result = $s3->getObject([ 'Bucket' => 'Bucket', 'Key' => 'Key', ]); echo $result['Body']; // foo $dynamoDb = \AWS::createDynamoDb(); $result = $dynamoDb->listTables(); echo $result['TableNames'][0]; // Table1