【爬坑指南】亚马逊文件中心 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;
        }
    }
}
相关推荐
泷羽Sec-静安29 分钟前
Less-1 GET-Error based-Single quotes-String GET-基于错误-单引号-字符串
前端·css·网络·sql·安全·web安全·less
小时前端40 分钟前
虚拟DOM已死?90%内存节省的Vapor模式正在颠覆前端
前端·html
Keepreal49641 分钟前
Web Components简介及如何使用
前端·javascript·html
jump6801 小时前
TS中 unknown 和 any 的区别
前端
TG_yunshuguoji1 小时前
亚马逊云渠道商:如何通过配置自动替换构建故障自愈的云架构?
运维·服务器·架构·云计算·aws
无羡仙1 小时前
AI终于‘看见’网页了!Stagewise让UI修改从‘盲调’变‘指哪打哪
前端
柯腾啊2 小时前
“Script error.”的产生原因和解决办法
前端·javascript·浏览器
沙漠之皇2 小时前
ts 定义重复对象字段
前端
TG:@yunlaoda360 云老大2 小时前
阿里云国际站GPU:怎么通过通过VNC连接实例?
服务器·阿里云·云计算
HashTang2 小时前
不用再配服务器了!这套 Next.js + Cloudflare 模板,一个人搞定全栈出海
前端·后端·边缘计算