【爬坑指南】亚马逊文件中心 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;
        }
    }
}
相关推荐
环境栈笔记1 小时前
指纹浏览器检查代理失败怎么排查?先分清代理、网络和检测服务
前端·人工智能·后端·自动化
做运维的阿瑞1 小时前
Terraform 的 Provider 与 Resource
云原生·云计算·terraform
ShineWinsu1 小时前
对于HTML:HTML常见标签的解析—下
前端·css·面试·html·标签·表单·emmet
恋猫de小郭1 小时前
Kotlin 2.5 的 Companion 改造,对 KMP 的重要升级
android·前端·flutter
前端双越老师2 小时前
程序员 1v1 访谈系列第二期:很焦虑但又不知道干啥
前端·ai编程·全栈
广州华水科技2 小时前
北斗形变监测传感器在大坝安全监测中的应用与优势分析
前端
孔令飞2 小时前
如何学习Go
前端
HUIBUR科技2 小时前
AI 中枢,面向企业场景构建的 AI 智能体
前端·数据库·人工智能
随便写个软件2 小时前
【新版本】EasyExplorer资源管理器 3.12.0——地址栏重要更新
运维·服务器·前端·everything·资源管理器
Jack N2 小时前
【Strapi 系列之二】Collection / Single / Component 怎么选?Strapi 字段大全一篇讲透
前端·javascript·strapi