AmazonS3Service.php 3.4 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)
    {
        $file_link = $this->fetchRemoteImage($files);
        $key = str_replace_url($files);
        try {
            $file_content = $file_link;
            $result = $this->s3->putObject([
                'Bucket' => $this->bucket,
                'Key' => ltrim($key,'/'),
                'Body' => $file_content,
            ]);
//            unlink($location);
            return $result['ObjectURL'];
        } catch (AwsException $e) {
            return '上传文件到S3时发生错误:' . $e->getMessage();
        }
    }

    /**
     * @remark :零时文件
     * @name   :fetchRemoteImage
     * @author :lyh
     * @method :post
     * @time   :2024/1/26 12:48
     */
    public function fetchRemoteImage($url) {
        // 创建 cURL 句柄
        $curl = curl_init();
        // 设置 cURL 选项
        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        // 执行请求并获取内容
        $response = curl_exec($curl);
        // 检查请求是否成功
        if ($response === false) {
            $error = curl_error($curl);
            // 处理错误
            return 'cURL 错误:' . $error;
        } else {
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export($response, true) . PHP_EOL, FILE_APPEND);
            return $response;
        }
        // 关闭 cURL 句柄
        curl_close($curl);
    }
}