首先使用 composer在项目中下载aws:

composer require aws/aws-sdk-php
/**
 * AWS S3上传文件
 * @param string $file 文件名称
 * @return array
 */
function fileUpload($file){
    //设置超时
    set_time_limit(0);
    //证书 AWS access KEY ID  和  AWS secret  access KEY 替换成自己的
    $credentials = new Aws\Credentials\Credentials('AWS access KEY ID ', 'AWS secret  access KEY');
    //s3客户端
    $s3 = new Aws\S3\S3Client([
	      'version'     => 'latest',
	      //地区 亚太区域(新加坡)	
		  //AWS区域和终端节点: http://docs.amazonaws.cn/general/latest/gr/rande.html
	      'region'      => 'ap-southeast-1',
	      //加载证书
	      'credentials' => $credentials,
	      //开启bug调试
	      //'debug'   => true
    ]);

    //存储桶 获取AWS存储桶的名称
    $bucket = 'test';//'AWS存储桶名称';
    //需要上传的文件
	//ROOT_PATH项目根目录,文件的本地路径例:D:/www/abc.jpg;
    $source = ROOT_PATH.$file; 
    //多部件上传
    $uploader = new Aws\S3\MultipartUploader($s3, $source, [
        //存储桶
        'bucket' => $bucket,
        //上传后的新地址
        'key'    => $file,
        //设置访问权限  公开,不然访问不了
        'ACL'    => 'public-read',
        //分段上传
        'before_initiate' => function (\Aws\Command $command) {
            // $command is a CreateMultipartUpload operation
            $command['CacheControl'] = 'max-age=3600';
        },
        'before_upload'   => function (\Aws\Command $command) {
            // $command is an UploadPart operation
            $command['RequestPayer'] = 'requester';
        },
        'before_complete' => function (\Aws\Command $command) {
            // $command is a CompleteMultipartUpload operation
           $command['RequestPayer'] = 'requester';
        },
    ]);

    try {
        $result = $uploader->upload();
        //上传成功--返回上传后的地址
        $data = [
           'type' => '1',
           'data' => urldecode($result['ObjectURL'])
        ];
    } catch (Aws\Exception\MultipartUploadException $e) {
       //上传失败--返回错误信息
        $uploader =  new Aws\S3\MultipartUploader($s3, $source, [
            'state' => $e->getState(),
        ]);
        $data = [
          'type' => '0',
          'data' =>  $e->getMessage();
        ];
   }
   return $data;
}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐