PHP上传文件到AWS S3&生成下载文件URL

<?php

/**
 * 加载s3客户端
 * @return string
 */
function AWS_S3Client(){
    //证书
    $credentials = new AwsCredentialsCredentials('你的S3_ACCESS_KEY_ID', '你的S3_SECRET_ACCESS_KEY');
    //s3客户端
    return new AwsS3S3Client([
        'version' => 'latest',
        //地区 亚太区域(新加坡)    ap-southeast-1
        'region' => 'eu-central-1',//自行配置
        //加载证书
        'credentials' => $credentials,
        //开启bug调试
        //'debug'   => true
    ]);
}

/**
 * 判断S3中是否有文件
 * @param string $file
 * @return string
 */
function AWS_S3Response($file){
    $s3Client = AWS_S3Client();
    //存储桶 S3_BUCKET;
    return $s3Client->doesObjectExist('你的存储桶名称', $file);//检测s3是否存在,空格转换会无法找到文件
}

/**
 * AWS S3上传文件
 * @param string $file 文件绝对路径
 * @param string $fileName 文件名称
 * @param int $type 1使用断点续传,0不使用
 * @param bool $publicRead 是否开放访问
 * @return array $path
 */
function S3FileUpload($file = '', $fileName = '', $type = 0, $publicRead = false){
    $s3Client = AWS_S3Client();
    $bucket = '你的存储桶名称';
    //需要上传的文件
    $source = FILE_UPLOAD.$file;//绝对路径 根据自己的项目配置
    $fileName = $fileName ? $fileName : $file;
    $config = [
        'bucket' => $bucket,
        'key' => $fileName,//这里如果是相对路径 如 test/img/1.jpg 会自动创建目录

    ];
    if ($publicRead) {
        $config['ACL'] = 'public-read';
    }
    $uploader = new AwsS3MultipartUploader($s3Client, $source, $config);
    if ($type == 1) {
        //在分段上传过程中发生错误,重新开始未完成的上传。
        do {
            try {
                $result = $uploader->upload();
            } catch (AwsExceptionMultipartUploadException $e) {
                $uploader = new AwsS3MultipartUploader($s3Client, $source, [
                    'state' => $e->getState(),
                ]);
            }
        } while (!isset($result));

        //返回上传后的地址
        $data = [
            'type' => '1',
            'message' => urldecode($result['ObjectURL'])
        ];
    } else {
        try {
            $result = $uploader->upload();
            //返回上传后的地址
            $data = [
                'type' => '1',
                'message' => urldecode($result['ObjectURL'])
            ];
        } catch (AwsExceptionMultipartUploadException $e) {
            $data = [
                'type' => '0',
                'message' => $e->getMessage()
            ];
        }
    }

    return $data;
}

/**
 * 生成AWS S3下载/上传文件url地址
 * @param string $file 文件相对地址
 * @param string $fileName 下载的文件名称
 * @param string $expires 授权时间
 * @return string
 */
function S3FileDownload($file, $fileName = '', $expires = '+10 minutes'){
    if(!$fileName){
        $pathinfo = pathinfo($file);
        $fileName = $pathinfo['basename'];
    }
    $s3Client = AWS_S3Client();
    $cmd = $s3Client->getCommand('GetObject', [
        'Bucket' => '你的存储桶名称',
        'Key' => $file, //地址,
     //'ResponseContentType' => 'text/plain',
     //'ResponseContentLanguage' => 'en-US',
     //'ResponseCacheControl' => 'No-cache',
     //'ResponseExpires' => gmdate(DATE_RFC2822, time() + 3600), 
'ResponseContentDisposition' => 'attachment; filename='.$fileName,//访问链接直接下载 ]); $request = $s3Client->createPresignedRequest($cmd, $expires); //创建预签名 URL $presignedUrl = (string)$request->getUri(); return $presignedUrl; } ?>

参考文档1:https://docs.aws.amazon.com/zh_cn/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html

参考文档2:https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/RetrieveObjSingleOpPHP.html

原文地址:https://www.cnblogs.com/guliang/p/14142982.html