【爬坑指南】亚马逊文件中心 AWS S3 预签名URL 前端直传

亚马逊文件中心 AWS S3 预签名URL 接口对接

复制代码
​​​​​​​class AmazonAwsBusiness
{
    private $key = '';
    private $secret = '';
    private $region = '';
    private $version = 'latest';
    private $bucket = 'image';
    public function __construct()
    {
        $this->key = env('AMAZON_AWS.KEY', '');
        $this->secret = env('AMAZON_AWS.SECRET', '');
        $this->region = env('AMAZON_AWS.REGION', '');
        $this->bucket = env('AMAZON_AWS.BUCKET', '');
    }
    public function createBucketBusiness()
    {
        $s3 = new S3Client([
            'version' => 'latest',
            'region' => 'us-2',
//            'endpoint' => 'https://s3.us-west-2.amazonaws.com',
            'credentials' => [
                'key' => 'YOUR_ACCESS_KEY',
                'secret' => 'YOUR_SECRET_KEY',
            ],
        ]);
        $bucket = 'my-new-bucket';
        return $s3->createBucket([
            'Bucket' => $bucket,
        ]);
    }
    public function listBucketsBusiness()
    {
        $s3Client = new S3Client([
            'region' => $this->region,
            'version' => $this->version,
            'credentials' => [
                'key' => $this->key,
                'secret' => $this->secret,
            ]
        ]);
        return $s3Client->listBuckets();
    }
    public function uploadImgBusiness($file, $shop_name = '', $type = 1)
    {
        if ($shop_name == '') {
            $shop_name = time();
        }
        $s3Client = new S3Client([
            'region' => $this->region,
            'version' => $this->version,
            'credentials' => [
                'key' => $this->key,
                'secret' => $this->secret,
            ]
        ]);
        if ($type == 1) {
            $savename = \think\facade\Filesystem::putFile('wsms3_listings', $file);
            $filePath = app()->getRootPath() . '/runtime/storage/' . $savename;
        } else {
            $filePath = $file;
        }
        return $s3Client->putObject([
            'Bucket' => $this->bucket,
            'Key' => $shop_name . '_' . time(),
            'SourceFile' => $filePath,
        ]);
    }
    /**
     * 根据url下载图片 并上传到s3


     */
    public function sendUploadImgBusiness($url)
    {
        $filePath = app()->getRootPath() . '/runtime/storage/wsms3_listings/';
        $imgFile = httpTolocal($url, $filePath);
        try {
            $res = (new AmazonAwsBusiness())->uploadImgBusiness($imgFile, rand(1, 99999), 2);
            return $res['ObjectURL'];
        } catch (Exception $e) {
            return false;
        }
    }
}
相关推荐
知识分享小能手10 分钟前
微信小程序入门学习教程,从入门到精通,微信小程序开发进阶(7)
前端·javascript·学习·程序人生·微信小程序·小程序·vue3
数据与人工智能律师1 小时前
数字人民币钱包抉择:匿名自由与实名安全的法律风险评估
大数据·人工智能·python·云计算·区块链
sophie旭2 小时前
一道面试题,开始性能优化之旅(8)-- 构建工具和性能
前端·面试·性能优化
市民中心的蟋蟀2 小时前
第三章 钩入React 【上】
前端·react.js·架构
Holin_浩霖2 小时前
为什么typeof null 返回 "object" ?
前端
PanZonghui2 小时前
Zustand 实战指南:从基础到高级,构建类型安全的状态管理
前端·react.js
PanZonghui2 小时前
Vite 构建优化实战:从配置到落地的全方位性能提升指南
前端·react.js·vite
_extraordinary_3 小时前
Java Linux --- 基本命令,部署Java web程序到线上访问
java·linux·前端
用户1456775610373 小时前
推荐一个我私藏的电脑神器:小巧、无广、功能强到离谱
前端
用户1456775610373 小时前
终于找到了!一个文件搞定PDF阅读
前端