PHP code example of unntech / minio-php-s3
1. Go to this page and download the library: Download unntech/minio-php-s3 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
unntech / minio-php-s3 example snippets
use MinioS3\Sts\StsClient;
$stsClient = new StsClient([
'credentials' => [
'key' => 'key',
'secret' => 'secret'
],
'region' => 'cn-default-1',
'version' => 'latest',
'bucket_endpoint' => false,
'use_path_style_endpoint' => true,
'endpoint' => 'http://127.0.0.1:9801'
]);
$assumeRoleResult = $stsClient->assumeRole([
'RoleArn' => 'arn:ssssxxx:xxx:xxx:xxxx',
'RoleSessionName' => 'role-session-name'
]);
$res = $stsClient->createCredentials($assumeRoleResult);
$sts = $res->toArray();
var_dump($sts);
use MinioS3\S3\PostObjectV4;
use MinioS3\S3\S3Client;
$s3 = new S3Client([
'version' => 'latest',
'region' => 'cn-default-1',
'endpoint' => 'http://127.0.0.1:9801',
'bucket_endpoint' => false,
'use_path_style_endpoint' => true,
'credentials' => [
'key' => 'key',
'secret' => 'secret',
//'token' => $sts['token'],
],
]);
// 列出所有对象
$res = $s3->listObjectsV2([
'Bucket' => 'pub'
]);
//var_dump($res);
// 复制对象
$result = $s3->copyObject([
'Bucket' => 'pub',
'CopySource' => '/pub/a.txt',
'Key' => 'b.txt',
]);
var_dump($result);
//删除对象
$res = $s3->deleteObject([
'Bucket' => 'pub',
'Key' => 'b.txt'
]);
var_dump($res);
// 上传对象
$insert = $s3->putObject([
'Bucket' => 'pub',
'Key' => 'c.txt',
// 'SourceFile' => $sourceFileName, // 使用文件上传
'Body' => 'Hello from MinIO!! exm', // 文件内容
'ContentType' => 'text/plain'
]);
var_dump($insert);
var_dump($insert['ObjectURL']);
// 下载文件的内容
$retrive = $s3->getObject([
'Bucket' => 'pub',
'Key' => 'a.txt',
'SaveAs' => './a.txt'
]);
var_dump($retrive);
//获得一个10分钟有效期的pre-signed URL
$command = $s3->getCommand('GetObject', [
'Bucket' => 'pub',
'Key' => 'a.txt',
]);
$signedRequest = $s3->createPresignedRequest($command, "+600 seconds");
var_dump((string)$signedRequest->getUri());
// 获取一个临时上传地址,交前端上传文件
$options = [
['acl' => 'public-read'],
['bucket' => 'pub'],
['key' => 'p.txt'],
];
$formInputs = ['acl' => 'public-read', 'key' => 'p.txt'];
$postObject = new PostObjectV4(
$s3,
'pub',
$formInputs,
$options,
'+2 hours'
);
$data = [
'endpoint'=>$postObject->getFormAttributes(),
'form'=>$postObject->getFormInputs(),
];
var_dump($data);
use MinioS3\Exception\MultipartUploadException;
use MinioS3\S3\MultipartUploader;
// Use multipart upload 分段上传
$source = './a.zip';
$uploader = new MultipartUploader($s3, $source, [
'bucket' => 'pub',
'key' => 'a1.zip',
]);
try {
$result = $uploader->upload();
echo "Upload complete: {$result['ObjectURL']}\n";
} catch (MultipartUploadException $e) {
echo $e->getMessage() . "\n";
}