亚马逊S3存储服务文件上传与获取工具(基于AWS sdk for PHP 3.x)

亚马逊S3存储服务文件上传与获取工具(基于AWS sdk for PHP 3.x)


直接上代码


/**
 * Desciption : 亚马逊S3文件管理工具类
 *
 * @author    : [email protected]
 * @date      : 2018-09-19
 *
 *
 */

namespace Chunbo\Util;

vendor("Aws.aws-autoloader");

use Think\Log\LogHelper;
use Aws\S3\S3Client;
use Aws\S3\ObjectUploader;

class AwsFile
{
    // 桶名称
    const AWS_BUCKET = YOUR_BUCKEY_NAME;

    private static $region = YOUR_REGION;//'cn-north-1'
    private static $s3version = '2006-03-01';
    private static $profile = 'default';
    private static $_s3clients;

    private static function uploadDemo()
    {
        //上传到服务器的临时文件
        $file_name = $_FILES['aws_test']['tmp_name'];
        // 获取数据流
        $stream = file_get_contents($file_name);
        // 调用工具类上传
//        $name = AwsFile::upload(AwsFile::AWS_BUCKET_TEST, 'demo.jpg', $stream);
        $name = AwsFile::getObject(AwsFile::AWS_BUCKET, 'text.txt');
        // 返回完整外网地址
        echo $name;
        exit;
    }


    /*
     * Desciption 使用该方法将文件上传到亚马逊S3,如果成功返回文件在S3中的地址;
     * 
     * @param string $bucket AWS存储桶
     * @param string $filename 存储名称
     * @param string||stream $body  文件内容,写入到filename中的数据
     * 
     * @return string File Full URL;
     * 
     */

    public static function upload(AwsFile $bucket, $filename, $body)
    {
        try {
            $s3Client = self::getS3Client($bucket);

            $key = md5(time()) . '_' . $filename;
            $uploader = new ObjectUploader(
                $s3Client, $bucket, $key, $body
            );
            $uploader->upload();
            return $key;
        } catch (\Exception $e) {
            LogHelper::info(
                 '文件上传失败 | '. $e->getMessage()
            );
        }
        return '';
    }

    public static function getObject(AwsFile $bucket, $filename)
    {
        $s3Client = self::getS3Client($bucket);
        $result = $s3Client->getObject(
            [
                'Bucket'                     => $bucket,
                'Key'                        => $filename,
                'ResponseContentDisposition' => 'attachment; filename='
                    . $filename,
                'ResponseContentType'        => 'text/plain',
                'ResponseContentLanguage'    => 'en-US',
                'ResponseCacheControl'       => 'No-cache',
            ]
        );
        header("Content-Type: {$result['ContentType']}");
        echo $result['Body'];
        exit;
    }

    private static function getS3Client($bucket)
    {
        if (!self::$_s3clients[$bucket] instanceof S3Client) {
            $credentials = self::getCredentials($bucket);
            self::$_s3clients[$bucket] = new S3Client(
                [
                    'region'      => self::$region,
                    'version'     => self::$s3version,
                    'credentials' => $credentials
                ]
            );
        }
        return self::$_s3clients[$bucket];
    }

    private static function getCredentials($bucket)
    {
        switch ($bucket) {
            case self::AWS_BUCKET:
                $credentials = [
                    'key'    => YOUR_KEY,
                    'secret' => YOUR_SECRET
                ];
                break;

            default:
                $credentials = [];
        }
        return $credentials;
    }

    private function __construct()
    {
    }

    private function __clone()
    {
    }
}

http://www.hello-php.com/index.php/article/30
希望对你有帮助!

你可能感兴趣的:(亚马逊S3存储服务文件上传与获取工具(基于AWS sdk for PHP 3.x))