You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.2 KiB
40 lines
1.2 KiB
<?php
|
|
namespace app\utility;
|
|
|
|
use Aws\S3\S3Client;
|
|
use think\facade\Config;
|
|
|
|
class AwsS3Handler
|
|
{
|
|
protected $s3Client;
|
|
protected $defaultBucket;
|
|
|
|
public function __construct(array $s3Config)
|
|
{
|
|
$this->defaultBucket = $s3Config['aws_bucket'];
|
|
$this->s3Client = new S3Client([
|
|
'version' => 'latest',
|
|
'region' => $s3Config['aws_region'],
|
|
'credentials' => [
|
|
'key' => $s3Config['aws_key'],
|
|
'secret' => $s3Config['aws_secret'],
|
|
],
|
|
]);
|
|
}
|
|
|
|
// 上传单个文件
|
|
public function putObject($keyName, $openFile, $contentType): array
|
|
{
|
|
try {
|
|
$result = $this->s3Client->putObject([
|
|
'Bucket' => $this->defaultBucket,
|
|
'Key' => $keyName, // s3中的存储路径
|
|
'Body' => $openFile,
|
|
'ContentType' => $contentType, // 设置文件的MIME,不然s3会以流式存储
|
|
])->toArray();
|
|
return ['code'=>200, 'message'=>'ok', 'data'=>$result];
|
|
} catch (\Exception $e) {
|
|
return ['code'=>$e->getCode(), 'message'=>$e->getMessage(), 'data'=>$e->getTrace()];
|
|
}
|
|
}
|
|
}
|