|
2 | 2 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 |
| -/* |
6 |
| - * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at |
7 |
| - * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html |
| 5 | +/** |
| 6 | + * This file shows how to use the CommandPool class provided with the AWS SDK for PHP. |
| 7 | + * It uses the S3Client for this example, but the CommandPool class can be used with |
| 8 | + * many SDK clients. See https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_commands.html |
| 9 | + * for more information. |
| 10 | + * |
| 11 | + * This code expects that you have AWS credentials set up per: |
| 12 | + * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html |
8 | 13 | *
|
| 14 | + * It also assumes it will be run in a *nix environment. |
9 | 15 | */
|
10 |
| -// snippet-start:[s3.php.command_pool.complete] |
11 |
| -// snippet-start:[s3.php.command_pool.import] |
12 |
| -require 'vendor/autoload.php'; |
13 | 16 |
|
14 |
| -use Aws\Exception\AwsException; |
15 |
| -use Aws\S3\S3Client; |
16 |
| -use Aws\CommandPool; |
| 17 | +namespace ClassExamples; |
| 18 | + |
| 19 | +# snippet-start:[php.class_examples.command_pool.complete] |
| 20 | +# snippet-start:[php.class_examples.command_pool.import] |
| 21 | +include __DIR__ . "/vendor/autoload.php"; |
| 22 | + |
17 | 23 | use Aws\CommandInterface;
|
| 24 | +use Aws\CommandPool; |
| 25 | +use Aws\Exception\AwsException; |
18 | 26 | use Aws\ResultInterface;
|
| 27 | +use Aws\S3\S3Client; |
| 28 | +use DirectoryIterator; |
19 | 29 | use GuzzleHttp\Promise\PromiseInterface;
|
20 |
| -// snippet-end:[s3.php.command_pool.import] |
21 |
| -/** |
22 |
| - * Use Command Pool to upload a file to an Amazon S3 bucket. |
23 |
| - * |
24 |
| - * This code expects that you have AWS credentials set up per: |
25 |
| - * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html |
26 |
| - */ |
27 |
| -// Create the client |
28 |
| -// snippet-start:[s3.php.command_pool.main] |
29 |
| -$client = new S3Client([ |
30 |
| - 'region' => 'us-standard', |
31 |
| - 'version' => '2006-03-01' |
32 |
| -]); |
| 30 | +use Iterator; |
| 31 | +use S3\S3Service; |
33 | 32 |
|
34 |
| -$fromDir = '/path/to/dir'; |
35 |
| -$toBucket = 'my-bucket'; |
| 33 | +# snippet-end:[php.class_examples.command_pool.import] |
| 34 | + |
| 35 | +# snippet-start:[php.class_examples.command_pool.main] |
| 36 | +$client = new S3Client([]); |
| 37 | + |
| 38 | +$s3Service = new S3Service($client, true); |
| 39 | + |
| 40 | +$bucket = 'my-bucket-' . uniqid(); // This bucket will be deleted at the end of this example. |
| 41 | + |
| 42 | +$client->createBucket([ |
| 43 | + "Bucket" => $bucket, |
| 44 | +]); |
36 | 45 |
|
37 | 46 | // Create an iterator that yields files from a directory
|
38 |
| -$files = new DirectoryIterator($fromDir); |
| 47 | +$files = new DirectoryIterator(__DIR__); |
39 | 48 |
|
40 | 49 | // Create a generator that converts the SplFileInfo objects into
|
41 | 50 | // Aws\CommandInterface objects. This generator accepts the iterator that
|
42 | 51 | // yields files and the name of the bucket to upload the files to.
|
43 |
| -$commandGenerator = function (\Iterator $files, $bucket) use ($client) { |
| 52 | +$commandGenerator = function (Iterator $files, $bucket) use ($client) { |
| 53 | + /** @var DirectoryIterator $file */ |
44 | 54 | foreach ($files as $file) {
|
45 |
| - // Skip "." and ".." files |
46 |
| - if ($file->isDot()) { |
| 55 | + // Skip "." and ".." files as well as directories |
| 56 | + if ($file->isDot() || $file->isDir()) { |
47 | 57 | continue;
|
48 | 58 | }
|
49 | 59 | $filename = $file->getPath() . '/' . $file->getFilename();
|
|
57 | 67 | };
|
58 | 68 |
|
59 | 69 | // Now create the generator using the files iterator
|
60 |
| -$commands = $commandGenerator($files, $toBucket); |
| 70 | +$commands = $commandGenerator($files, $bucket); |
61 | 71 |
|
62 |
| -// Create a pool and provide an optional array of configuration |
| 72 | +// Create a pool and provide an optional configuration array |
63 | 73 | $pool = new CommandPool($client, $commands, [
|
64 | 74 | // Only send 5 files at a time (this is set to 25 by default)
|
65 | 75 | 'concurrency' => 5,
|
|
93 | 103 | $promise->wait();
|
94 | 104 |
|
95 | 105 | // Or you can chain the calls off of the pool
|
96 |
| -$promise->then(function() { echo "Done\n"; }); |
97 |
| -// snippet-end:[s3.php.command_pool.main] |
98 |
| -// snippet-end:[s3.php.command_pool.complete] |
| 106 | +$promise->then(function () { |
| 107 | + echo "Done\n"; |
| 108 | +}); |
| 109 | + |
| 110 | +//Clean up the created bucket |
| 111 | +$s3Service->emptyAndDeleteBucket($bucket); |
| 112 | +# snippet-end:[php.class_examples.command_pool.main] |
| 113 | +# snippet-end:[php.class_examples.command_pool.complete] |
0 commit comments