aws上传文件及配置
php.in 最后一页 配置cacert.pem证书文件,否则解析不了aws存储桶的路由
curl.cainfo = D:/phpstudy_pro/cacert.pem
引入
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
//aws认证
$credentials = new Aws\Credentials\Credentials(‘key‘, ‘secret‘);
$S3Client= new Aws\S3\S3Client([
‘version‘ =>‘aws.version‘,//存储桶地区
‘region‘ =>‘2006-03-01‘,//日期
‘credentials‘ => $credentials
]);
//存储桶列表
$S3Client->listbuckets();
//获取对象列表
$bucket=‘‘;//存储桶
$S3Client->listObject(‘bucket‘=>‘aws.bucket‘);
//分段上传文件
$source=‘../images/111.png‘;//文件路径
$photo_name=‘test.png‘;//文件上传名字
$S3Client = new Aws\S3\MultipartUploader($S3Client, $source, [
‘bucket‘ =>‘aws.bucket‘,
‘key‘ => $photo_name,
]);
try {
$result = $S3Client->upload();
return $result[‘Key‘];
} catch (MultipartUploadException $e) {
return $e->getMessage() . "\n";
}
//预签名获取文件路由
$cmd = $S3Client->getCommand(‘GetObject‘, [
‘Bucket‘ =>‘aws.bucket‘,
‘Key‘ => $key //aws文件健名
]);
$request = $config->createPresignedRequest($cmd, ‘+20 minutes‘);
$presignedUrl = (string)$request->getUri();