php 操作minio

分享一个用php写的操作minio客户端的脚本,提供一些针对minio的基本操作。

列出所有的bucket

新建bucket

获取指定bucket的acl规则

删除bucket

上传文件

获取指定文件的url

删除文件

minio支持亚马逊s3对象存储,所以先安装aws s3客户端sdk

aws s3 sdk 文档:

         https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html

[root@centos7-4 composer]# cat composer.json 
{
    "require": {
        "aws/aws-sdk-php": "^3.184"
    }
}
composer require aws/aws-sdk-php

minio.php

MultipartUploader需要用到xml,所以需要安装php的xml支持

yum install php70w-xml -y
 'latest',
        'region'  => 'us-east-1',
        'endpoint' => "$endpoint",
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => "$key",
                'secret' => "$secret",
        ],
]);

function getBuckets(){
        global $s3Client;
        $result = $s3Client->listBuckets();
        $names = $result->search('Buckets[].Name');
        return $names;
}

function createBucket($bucket){
        global $s3Client;
        $result = $s3Client->createBucket([
            'Bucket' => $bucket,
        ]);
}

function getBucketAcl($bucket){
        global $s3Client;
        $result=$s3Client->getBucketAcl([
                'Bucket' => $bucket
        ]);
        return $result;

}

function upLoadFile($file,$object){
        global $s3Client;
        global $bucket;
        $uploader = new MultipartUploader($s3Client, $file, [
                'bucket' => $bucket,
                'key' => $object,
        ]);

        $result=$uploader->upload();
}
function getURL($object){
        global $s3Client;
        global $bucket;
        $cmd = $s3Client->getCommand('GetObject', [
                'Bucket' => $bucket,
                'Key' => $object
        ]);
        $request=$s3Client->createPresignedRequest($cmd,'+7 days');
        $presignedUrl = (string)$request->getUri();
        return $presignedUrl;
}
function deleteBucket($bucket){
        global $s3Client;
        $result = $s3Client->deleteBucket([
            'Bucket' => $bucket,
        ]);

}
function deleteObject($object){
        global $s3Client;
        global $bucket;
        $result = $s3Client->deleteObject([
            'Bucket' => $bucket,
            'Key' => $object,
        ]);


}
#测试脚本demo
/*
#getBuckets list
$test=getBuckets();
print_r($test);
#createBucket
$test=createBucket("test");
#getBucketAcl
$test=getBucketAcl("test");
print_r($test);
#upLoadFile
$file="./test.php";
$object="test.php";
$test=upLoadFile($file,$object);
#getURL
$test=getURL($object);
print_r($test);
#deleteBucket
$test=deleteBucket("test");
#deleteObject
$test=deleteObject("test.php");
*/

?>

 

你可能感兴趣的:(php,php)