PHP用Aws的SDK实现Minio对象存储链接API
用 thinkphp 框架
通过composer 安装 aws的php版本sdk
cd到thinkphp框架的根目录下,同composer.json目录。
composer require aws/aws-sdk-php
然后新建一个 controller,就可以直接引入Aws的SDK类和函数进行测试
链接本地的 Minio服务器http://192.168.1.112:9005
<?php
namespace app\controller;
use app\BaseController;
use Aws\Credentials\Credentials;
use Aws\Endpoint\EndpointProvider;
use Aws\EndpointDiscovery\EndpointList;
use Aws\s3\S3Client;
class Aws extends BaseController{
public function index(){
$credentials = new Credentials('minioadmin', 'minioadmin');
$s3 = new S3Client([
'version'=> 'latest',
'region' => 'us-east-1',
'endpoint'=> 'http://192.168.1.112:9005',
'credentials'=> $credentials,
'use_path_style_endpoint' => true,
]);
$insert = $s3->putObject([
'Bucket' => 'f202101',
'Key' => 'testkey'. time().".jpg",
'SourceFile' => 'timgbu.jpg',
]);
// Download the contents of the object.
$retrive = $s3->getObject([
'Bucket' => 'f202101',
'Key' => '2020春节放假通知.jpg',
]);
$command = $s3->getCommand('GetObject', [
'Bucket' => 'f202101',
'Key' => '2020春节放假通知.jpg'
]);
// Create a pre-signed URL for a request with duration of 10 miniutes
$presignedRequest = $s3->createPresignedRequest($command, '+10 minutes');
// Get the actual presigned-url
$presignedUrl = (string) $presignedRequest->getUri();
$url = $retrive['@metadata']['effectiveUri'];
// Print the body of the result by indexing into the result object.
// 获取所有的buckets;
echo "当前所有存储桶:<br/>";
$buckets = $s3 ->listBuckets();
foreach($buckets['Buckets'] as $bucket){
echo $bucket['Name']. "<br/>";
}
return "<img src='{$presignedUrl}'/>";
}
}