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
通过composer添加(版本很重要,不同的版本操作不一定一样,我这边都是3.X版本):
"aws/aws-sdk-php": "^3.118"
"aws/aws-sdk-php-laravel": "^3.4"
// 将下面代码追加到 providers 数组中
Aws\Laravel\AwsServiceProvider::class,
// 将下面代码追加到 aliases 数组中
'AwsFacade' => Aws\Laravel\AwsFacade::class,
'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表示区域,有固定标识,如图所示:
$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;
}
$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();
这一块要先把桶设置为public权限(怎么设置这个我也不太清楚,我用的是3.1),然后直接调用如下代码就能获取url
$s3 = AwsFacade::createClient('s3');
$plainUrl = $s3->getObjectUrl('bucket', 'key'); //bucket和key的意思同上
$s3 = AwsFacade::createClient('s3');
$s3->getObject([
'Bucket' =>'home',
'Key' =>$file,
'SaveAs' =>$save_path //这里是你要下载到本地的具体路径(比如:D:\www\1.jpg)
]);
$s3 = AwsFacade::createClient('s3');
$s3->createBucket(['Bucket' => $bucket['Name']]); //创建桶
$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']]); //删除桶
}
}
}