AmazonS3Service.php 3.5 KB
<?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 = 'AKIAU6YKND7SAWIKBXCZ';//key
    protected $secretAccessKey = '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';//密匙
    protected $region = 'us-west-2';//地址
    protected $bucket = 'arn:aws:s3:us-west-2:340934860772:accesspoint/globalso-v6';//桶子

    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(&$files, $s3Key,$filename)
    {
        $key = ltrim($s3Key.'/'.$filename,'/');
        $body = $files->getRealPath();
        try {
            $result = $this->s3->putObject([
                'Bucket' => $this->bucket,
                'Key' => $key,
                'SourceFile' => $body,
//                'ACL' => 'public-read', // 设置图片为公共可读,可根据需求修改
            ]);
            return $result['ObjectURL'];
        } catch (S3Exception $e) {
            return false;
        }
    }

    /**
     * @remark :同步图片文件到亚马逊
     * @name   :uploadImage
     * @author :lyh
     * @method :post
     * @time   :2024/1/23 9:20
     */
    public function syncImageFiles($files)
    {
        $key = str_replace_url($files);
        try {
            $opts = [
                'http' => [
                    'method' => 'GET',
                    'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
                ],
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false
                ]
            ];
            $stream_context = stream_context_create($opts);
            $file_handle = fopen($files, 'rb', null, $stream_context);
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($file_handle, true) . PHP_EOL, FILE_APPEND);
            if ($file_handle === false) {
                @file_put_contents(storage_path('logs/lyh_error.log'), var_export(2222222, true) . PHP_EOL, FILE_APPEND);
                return '无法打开文件';
            }
            $file_stats = fstat($file_handle);
            $content_length = $file_stats['size'];
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($content_length, true) . PHP_EOL, FILE_APPEND);
            $result = $this->s3->putObject([
                'Bucket' => $this->bucket,
                'Key' => $key,
                'Body' => $file_handle,
                'ContentLength' => $content_length
            ]);
            return $result['ObjectURL'];
        } catch (AwsException $e) {
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export('图片:'.$e->getMessage(), true) . PHP_EOL, FILE_APPEND);
            return '上传文件到S3时发生错误:' . $e->getMessage();
        }
    }
}