【爬坑指南】亚马逊文件中心 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;
        }
    }
}
相关推荐
TG_yunshuguoji2 小时前
亚马逊云代理商:AWS亚马逊云的独特优势与实用价值
服务器·云计算·aws
阿雄不会写代码2 小时前
AWS strands agents 当智能体作为独立服务/容器部署时,它们无法共享进程内状态
云计算·aws
华仔啊2 小时前
前端登录token到底应该存在哪?LocalStorage、SessionStorage还是Cookie?一篇说透!
前端·javascript
BeefyBytes2 小时前
动态组件库建设
前端
懒大王95272 小时前
uni-app + Vue3 开发展示 echarts 图表
前端·uni-app·echarts
yinuo2 小时前
Uni-App跨端实战:微信小程序WebView与H5通信全流程解析(01)
前端
xkroy3 小时前
ajax
前端·javascript·ajax
Yvonne爱编码3 小时前
AJAX入门-URL、参数查询、案例查询
前端·javascript·ajax
闲人编程3 小时前
前端形态与样式风格:从古典到现代的视觉语言演进
前端·css·状态模式·组件·js·风格·响应式