这个世界世界给了你什么,那是你决定不了的。但是怎么看待,怎么利用你手里的东西,确是你可以决定的。--《被讨厌的勇气》

本文我们接着上文继续来探讨一下如何使用使用 PHP 来操作 Ceph S3 接口。

安装 AWS PHP SDK

很简单,直接使用 composer 安装就好了。

composer install aws/aws-sdk-php

你也可以直接新建 composer.json 文件,然后加上如下配置:

{
    "require": {
        "aws/aws-sdk-php": "^3.154"
    }
}

然后再执行:

composer update

建立连接

use Aws\S3\S3Client;

define('AWS_KEY', 'MTAU54XU6ILI6097SNK8');
define('AWS_SECRET_KEY', 'TfYnLGXnOsQ6eXd8j98T3zYlwoewDFwTRwhqygFU');
define('DOMAIN', 'http://s3.rockyang.org');

require __DIR__.'/vendor/autoload.php';

// Instantiate the S3 class and point it at the desired host
$client = new S3Client([
    'region' => '',
    'version' => '2012-10-17',
    'endpoint' => DOMAIN,
    'credentials' => [
        'key' => AWS_KEY,
        'secret' => AWS_SECRET_KEY
    ],
    // Set the S3 class to use objects.dreamhost.com/bucket
    // instead of bucket.objects.dreamhost.com
    'use_path_style_endpoint' => true
]);

获取 Bucket 列表

该 API 将返回一个 ` AWS\Result` 对象。

// list all the buckets
$listResponse = $client->listBuckets();
$buckets = $listResponse['Buckets'];
foreach ($buckets as $bucket) {
    echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
}

输出:

bucket1   2020-10-19T22:05:39.000Z
bucket1   2020-10-19T22:05:48.000Z
bucket1   2020-10-19T22:07:18.000Z

创建一个 Bucket

$client->createBucket(['Bucket' => 'my-new-bucket']);

删除一个 Bucket

$client->deleteBucket(['Bucket' => 'my-old-bucket']);

获取文件列表

$objectsListResponse = $client->listObjects(['Bucket' => $bucketname]);
$objects = $objectsListResponse['Contents'] ?? [];
foreach ($objects as $object) {
    echo $object['Key'] . "\t" . $object['Size'] . "\t" . $object['LastModified'] . "\n";
}

上传文件

上传一个文本文件:

$client->putObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'hello.txt',
    'Body' => "Hello World!" // 文件内容
]);

上传一个二进制文件:

$sourceFile = "hello.jpg";
$r = $client->putObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'hello.jpg',
    'SourceFile' => $sourceFile
]);
$r = $r->toArray();
$res = $r['@metadata'];
if (isset($res["statusCode"]) && $res["statusCode"] == 200) {
    printf("Upload successfully. url: %s\n", $r["ObjectURL"]);
} else {
    echo "File upload failed.\n";
}

删除文件

$client->deleteObject(['Bucket' => 'my-bucket-name', 'Key' => 'hello.txt']);

下载文件

$object = $client->getObject(['Bucket' => 'my-bucket-name', 'Key' => 'poetry.pdf']);
file_put_contents('/home/larry/documents/poetry.pdf', $object['Body']->getContents());

生成文件下载地址

$hello_url = $client->getObjectUrl('my-bucket-name', 'hello.txt');
echo $hello_url."\n";

$secret_plans_cmd = $client->getCommand('GetObject', ['Bucket' => 'my-bucket-name', 'Key' => 'secret_plans.txt']);
$request = $client->createPresignedRequest($secret_plans_cmd, '+1 hour');
echo $request->getUri()."\n";

输出结果:

http://objects.dreamhost.com/my-bucket-name/hello.txt
http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=sandboxAccessKey%2F20190116%2F%2Fs3%2Faws4_request&X-Amz-Date=20190116T125520Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=61921f07c73d7695e47a2192cf55ae030f34c44c512b2160bb5a936b2b48d923

参考文档