|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @remark :上传文件与图片到亚马逊
|
|
|
|
* @name :AmazonS3Service.php
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2024/1/23 9:17
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Aws\S3\S3Client;
|
|
|
|
use Aws\S3\Exception\S3Exception;
|
|
|
|
class AmazonS3Service
|
|
|
|
{
|
|
|
|
// 替换为你自己的 AWS 访问密钥、区域和存储桶名称
|
|
|
|
protected $s3;
|
|
|
|
protected $accessKeyId = '';//key
|
|
|
|
protected $secretAccessKey = '';//密匙
|
|
|
|
protected $region = '';//地址
|
|
|
|
protected $bucket = '';//桶子
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// 创建 S3 客户端
|
|
|
|
$this->s3 = new S3Client([
|
|
|
|
'version' => 'latest',
|
|
|
|
'region' => $this->region,
|
|
|
|
'credentials' => [
|
|
|
|
'key' => $this->accessKeyId,
|
|
|
|
'secret' => $this->secretAccessKey,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :上传图片与文件
|
|
|
|
* @name :uploadImage
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2024/1/23 9:20
|
|
|
|
*/
|
|
|
|
public function uploadFiles($localFilePath, $s3Key)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$result = $this->s3->putObject([
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
'Key' => $s3Key,
|
|
|
|
'SourceFile' => $localFilePath,
|
|
|
|
'ACL' => 'public-read', // 设置图片为公共可读,可根据需求修改
|
|
|
|
]);
|
|
|
|
return $result['ObjectURL'];
|
|
|
|
} catch (S3Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|