Laravel aws used in operation with respect to the (aws-sdk-php-laravel)

Reference website:

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 premise

    By adding composer (version very important, different versions of operation necessarily the same, here are my 3.X version):

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

1 Basic Configuration item

1.1 app configuration (app / config / app.php)

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

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

1.2 aws configuration (create yourself a app / config / aws.php)

<?php
return [
    'version' =>'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 represents a region, a fixed logo, as shown:

2 upload files

$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 View online documents (two ways)

3.1 Public generate temporary files View online ( pre-Signed the URL of )

$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 Get the next barrel permanent public view documents online ( Plain the URL of )

To put this piece barrel set public authority (how to set this I do not know, I use 3.1), and then directly call the following code will be able to get url

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

4 Download file

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

 

Published 20 original articles · won praise 8 · views 50000 +

Guess you like

Origin blog.csdn.net/a26637896/article/details/103156580