关于在laravel中使用aws的操作(aws-sdk-php-laravel)

参考网址:

https://learnku.com/laravel/t/2533/extension-recommended-aws-sdk-php-laravel-aws-official-sdk

https://docs.aws.amazon.com/zh_cn/sdk-for-php/v3/developer-guide/s3-presigned-url.html

https://zhidao.baidu.com/question/715877133399578645.html

0 前提

    通过composer添加(版本很重要,不同的版本操作不一定一样,我这边都是3.X版本):

"aws/aws-sdk-php": "^3.118"
"aws/aws-sdk-php-laravel": "^3.4"

1 配置基本项

1.1 app配置(app/config/app.php)

// 将下面代码追加到 providers 数组中
Aws\Laravel\AwsServiceProvider::class,

// 将下面代码追加到 aliases 数组中
'AwsFacade' => Aws\Laravel\AwsFacade::class,

1.2 aws配置(自己创建一个app/config/aws.php)

'latest',
    'region'  => env('AWS_REGION', 'cn-north-1'),
    'endpoint' => env('AWS_ENDPOINT', 'YOUR_AWS_HOST'),
    'use_path_style_endpoint' =>true,
    'credentials' => [
        'key'    => env('AWS_KEY', 'YOUR_AWS_ACCESS_KEY'),
        'secret' => env('AWS_SECRET', 'YOUR_AWS_SECRET_KEY'),
    ],
    // You can override settings for specific services
    'Ses' => [
        'region' => env('AWS_SES_REGION', 'cn-north-1'),
    ],
];

     region表示区域,有固定标识,如图所示:

关于在laravel中使用aws的操作(aws-sdk-php-laravel)_第1张图片

2 上传文件

$s3 = AwsFacade::createClient('s3');
$s3_return = $s3->putObject([
	'Bucket' =>'home', //存储桶(我的理解就是文件系统中的目录)
	'Key'    =>$file, //文件名(包括后缀名)
	'Body'   =>file_get_contents($file_path) //要上传的文件
]);
if($s3_return['@metadata']['statusCode'] == 200){
    
} else {
	echo '返回值错误 : return fail! ';continue;
}

3 在线查看文件(两种方式)

3.1 生成临时公共文件在线查看(pre-signed URL

$s3 = AwsFacade::createClient('s3');
$s3_command = $s3->getCommand('GetObject', [
	'Bucket' =>'home',
	'Key'    =>$file
]);
$s3_pre_signed_return = $s3->createPresignedRequest($s3_command, '+30 minutes'); //临时存在的时长(分钟数)
$presigned_url = (string)$s3_pre_signed_return->getUri();

3.2 获取永久公共桶下文件在线查看(plain URL

这一块要先把桶设置为public权限(怎么设置这个我也不太清楚,我用的是3.1),然后直接调用如下代码就能获取url

$s3 = AwsFacade::createClient('s3');
$plainUrl = $s3->getObjectUrl('bucket', 'key'); //bucket和key的意思同上

4 下载文件

$s3 = AwsFacade::createClient('s3');
$s3->getObject([
	'Bucket' =>'home',
	'Key'    =>$file,
	'SaveAs' =>$save_path //这里是你要下载到本地的具体路径(比如:D:\www\1.jpg)
]);

5 创建桶

$s3 = AwsFacade::createClient('s3');
$s3->createBucket(['Bucket' => $bucket['Name']]); //创建桶

6 删除桶(涉及调用多个s3相关函数)

$s3 = AwsFacade::createClient('s3');
$list_buckets = $s3->listBuckets()['Buckets']; //列出当前oss用户所有的桶
$not_del = ['mytestingbucket', 'yedan-school-common'];
if(!empty($list_buckets)) { //要做非空判断
    foreach ($list_buckets as $bucket) {
        if (!in_array($bucket['Name'], $not_del)) { //排除掉不想被处理的桶
            //获取桶内对象(文件)列表
            $list_objects = $s3->listObjects(['Bucket' => $bucket['Name']])['Contents'];
            if (!empty($list_objects)) { //要做非空判断
                foreach ($list_objects as $object) {
                    //删除桶内文件,因为有文件的桶是不能删除的
                    $s3->deleteObject([
                        'Bucket'=>$bucket['Name'],
                        'Key'=>$object['Key']
                    ]);
                }
            }
            $s3->deleteBucket(['Bucket' => $bucket['Name']]); //删除桶
        }
    }
}

 

你可能感兴趣的:(php,laravel)