【爬坑指南】亚马逊文件中心 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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值