PHP链接亚马逊S3/MinIO

<?php

// 使用Composer autoloader引入SDK
date_default_timezone_set('America/Los_Angeles');
require 'vendor/autoload.php';

$s3 = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'endpoint' => 'http://localhost:9000',
        'use_path_style_endpoint' => true,
        'credentials' => [
                'key'    => 'YOUR-ACCESSKEYID',
                'secret' => 'YOUR-SECRETACCESSKEY',
            ],
]);


// 发送PutObject请求并获得result对象
$insert = $s3->putObject([
     'Bucket' => 'testbucket',
     'Key'    => 'testkey',
     'Body'   => 'Hello from MinIO!!'
]);

// 下载文件的内容
$retrive = $s3->getObject([
     'Bucket' => 'testbucket',
     'Key'    => 'testkey',
     'SaveAs' => 'testkey_local'
]);

// 通过索引到结果对象来打印结果的body。
echo $retrive['Body'];
<?php
// 从client中获得一个commad对象
$command = $s3->getCommand('GetObject', [
            'Bucket' => 'testbucket',
            'Key'    => 'testkey'
        ]);

// 获得一个10分钟有效期的pre-signed URL
$presignedRequest = $s3->createPresignedRequest($command, '+10 minutes');

// 获得presigned-url
$presignedUrl =  (string)  $presignedRequest->getUri();

查看SDK Amazon S3

https://packagist.org/packages/aws/aws-sdk-php

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/getting-started_installation.html

发表回复