【AWS-S3】多檔案上傳

env

  • aws sdk php v3
  • php 7.0x

ex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require './vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\CommandPool;

$access_key = "";
$secret_access_key = "";
$bucket = "";

$client = S3Client::factory([
        "region" => "ap-northeast-1", // tokyo
        "version" => "latest", 
        'credentials' => [
            'key'    => $access_key,
            'secret' => $secret_access_key,
        ],
    ]
);

$commands = array();

$binary = file_get_contents('./images/1.jpg');
$commands[] = $client->getCommand('PutObject', [
        'Bucket' => $bucket,
        'Key'    => 'photos/photo01.jpg',
        'Body'   => $binary
    ]
);

$commands[] = $client->getCommand('PutObject', [
        'Bucket' => $bucket,
        'Key'    => 'photos/photo02.jpg',
        'Body'   => $binary
    ]
);

$pool = new CommandPool($client, $commands);
// Initiate the pool transfers
$promise = $pool->promise();

// Force the pool to complete synchronously
try {
    $result = $promise->wait();
} catch (AwsException $e) {
    // handle the error.
}