• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php

/**
 * 加载s3客户端
 * @return string
 */
function AWS_S3Client(){
    //证书
    $credentials = new Aws\Credentials\Credentials('你的S3_ACCESS_KEY_ID', '你的S3_SECRET_ACCESS_KEY');
    //s3客户端
    return new Aws\S3\S3Client([
        '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 Aws\S3\MultipartUploader($s3Client, $source, $config);
    if ($type == 1) {
        //在分段上传过程中发生错误,重新开始未完成的上传。
        do {
            try {
                $result = $uploader->upload();
            } catch (Aws\Exception\MultipartUploadException $e) {
                $uploader = new Aws\S3\MultipartUploader($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 (Aws\Exception\MultipartUploadException $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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php的MVC基本理解与原理发布时间:2022-07-10
下一篇:
PHP中使用Imagick实现各种图片效果实例发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap