Skip to content

Commit f426b09

Browse files
authored
Php: Cleans up the S3 directory (first pass) and fixes some failing tests in IAM, DynamoDB, and Glue. (#5960)
Cleans up the S3 directory (first pass) and fixes some failing tests in IAM, DynamoDB, and Glue.
1 parent 3015f1a commit f426b09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+105565
-1029
lines changed

.doc_gen/metadata/cross_metadata.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ cross_PAM:
5555
PHP:
5656
versions:
5757
- sdk_version: 3
58+
github: php/applications/photo_asset_manager
5859
block_content: cross_PAM_PHP_block.xml
5960
Rust:
6061
versions:

.doc_gen/metadata/s3_metadata.yaml

+17-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ s3_Hello:
5151
- description:
5252
snippet_tags:
5353
- javascript.v3.s3.hello
54+
PHP:
55+
versions:
56+
- sdk_version: 3
57+
github: php/example_code/s3
58+
sdkguide:
59+
excerpts:
60+
- description:
61+
snippet_tags:
62+
- php.example_code.s3.basics.helloService
5463
services:
5564
s3: {ListBuckets}
5665
s3_CreateBucket:
@@ -116,7 +125,7 @@ s3_CreateBucket:
116125
PHP:
117126
versions:
118127
- sdk_version: 3
119-
github: php/example_code/s3/s3_basics
128+
github: php/example_code/s3
120129
excerpts:
121130
- description: Create a bucket.
122131
snippet_tags:
@@ -248,7 +257,7 @@ s3_CopyObject:
248257
PHP:
249258
versions:
250259
- sdk_version: 3
251-
github: php/example_code/s3/s3_basics
260+
github: php/example_code/s3
252261
excerpts:
253262
- description: Simple copy of an object.
254263
snippet_tags:
@@ -518,7 +527,7 @@ s3_DeleteObjects:
518527
PHP:
519528
versions:
520529
- sdk_version: 3
521-
github: php/example_code/s3/s3_basics
530+
github: php/example_code/s3
522531
excerpts:
523532
- description: Delete a set of objects from a list of keys.
524533
snippet_tags:
@@ -828,7 +837,7 @@ s3_GetObject:
828837
PHP:
829838
versions:
830839
- sdk_version: 3
831-
github: php/example_code/s3/s3_basics
840+
github: php/example_code/s3
832841
excerpts:
833842
- description: Get an object.
834843
snippet_tags:
@@ -1150,7 +1159,7 @@ s3_ListObjects:
11501159
PHP:
11511160
versions:
11521161
- sdk_version: 3
1153-
github: php/example_code/s3/s3_basics
1162+
github: php/example_code/s3
11541163
excerpts:
11551164
- description: List objects in a bucket.
11561165
snippet_tags:
@@ -1318,7 +1327,7 @@ s3_PutObject:
13181327
PHP:
13191328
versions:
13201329
- sdk_version: 3
1321-
github: php/example_code/s3/s3_basics
1330+
github: php/example_code/s3
13221331
excerpts:
13231332
- description: Upload an object to a bucket.
13241333
snippet_tags:
@@ -1466,7 +1475,7 @@ s3_DeleteBucket:
14661475
PHP:
14671476
versions:
14681477
- sdk_version: 3
1469-
github: php/example_code/s3/s3_basics
1478+
github: php/example_code/s3
14701479
excerpts:
14711480
- description: Delete an empty bucket.
14721481
snippet_tags:
@@ -2579,7 +2588,7 @@ s3_Scenario_GettingStarted:
25792588
PHP:
25802589
versions:
25812590
- sdk_version: 3
2582-
github: php/example_code/s3/s3_basics
2591+
github: php/example_code/s3
25832592
sdkguide:
25842593
excerpts:
25852594
- description:

php/example_code/auto-scaling/GettingStartedWithAutoScaling.php

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
# SPDX-License-Identifier: Apache-2.0
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
44

55
/**
66
* Purpose
@@ -16,7 +16,7 @@
1616
* 5. Delete all resources created by the example.
1717
**/
1818

19-
# snippet-start:[php.example_code.auto-scaling.basics.scenario]
19+
// snippet-start:[php.example_code.auto-scaling.basics.scenario]
2020
namespace AutoScaling;
2121

2222
use Aws\AutoScaling\AutoScalingClient;
@@ -63,13 +63,15 @@ public function runExample()
6363
$this->templateName = "example_launch_template_$uniqid";
6464
$instanceType = "t1.micro";
6565
$amiId = "ami-0ca285d4c2cda3300";
66-
$launchTemplate = $this->ec2Client->createLaunchTemplate([
66+
$launchTemplate = $this->ec2Client->createLaunchTemplate(
67+
[
6768
'LaunchTemplateName' => $this->templateName,
6869
'LaunchTemplateData' => [
6970
'InstanceType' => $instanceType,
7071
'ImageId' => $amiId,
7172
]
72-
]);
73+
]
74+
);
7375

7476
/**
7577
* Step 1: CreateAutoScalingGroup: pass it the launch template you created in step 0.
@@ -187,25 +189,29 @@ public function runExample()
187189
'Value' => $autoScalingGroup['AutoScalingGroupName'],
188190
],
189191
];
190-
$metrics = $this->cloudWatchClient->listMetrics([
192+
$metrics = $this->cloudWatchClient->listMetrics(
193+
[
191194
'Dimensions' => $metricsDimensions,
192195
'Namespace' => $metricsNamespace,
193-
]);
196+
]
197+
);
194198
foreach ($metrics['Metrics'] as $metric) {
195199
$timespan = 5;
196200
if ($metric['MetricName'] != 'GroupTotalCapacity' && $metric['MetricName'] != 'GroupMaxSize') {
197201
continue;
198202
}
199203
echo "Over the last $timespan minutes, {$metric['MetricName']} recorded:\n";
200-
$stats = $this->cloudWatchClient->getMetricStatistics([
204+
$stats = $this->cloudWatchClient->getMetricStatistics(
205+
[
201206
'Dimensions' => $metricsDimensions,
202207
'EndTime' => time(),
203208
'StartTime' => time() - (5 * 60),
204209
'MetricName' => $metric['MetricName'],
205210
'Namespace' => $metricsNamespace,
206211
'Period' => 60,
207212
'Statistics' => ['Sum'],
208-
]);
213+
]
214+
);
209215
foreach ($stats['Datapoints'] as $stat) {
210216
echo "{$stat['Timestamp']}: {$stat['Sum']}\n";
211217
}
@@ -236,12 +242,14 @@ public function cleanUp()
236242
/**
237243
* Step 13: Delete launch template.
238244
*/
239-
$this->ec2Client->deleteLaunchTemplate([
245+
$this->ec2Client->deleteLaunchTemplate(
246+
[
240247
'LaunchTemplateName' => $this->templateName,
241-
]);
248+
]
249+
);
242250
}
243251

244-
#snippet-start:[php.example_code.auto-scaling.basics.helloService]
252+
// snippet-start:[php.example_code.auto-scaling.basics.helloService]
245253
public function helloService()
246254
{
247255
$autoScalingClient = new AutoScalingClient([
@@ -253,6 +261,6 @@ public function helloService()
253261
$groups = $autoScalingClient->describeAutoScalingGroups([]);
254262
var_dump($groups);
255263
}
256-
#snippet-end:[php.example_code.auto-scaling.basics.helloService]
264+
// snippet-end:[php.example_code.auto-scaling.basics.helloService]
257265
}
258-
# snippet-end:[php.example_code.auto-scaling.basics.scenario]
266+
// snippet-end:[php.example_code.auto-scaling.basics.scenario]

php/example_code/aws_utilities/AWSServiceClass.php

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function customWaiter($function, $verbose = false)
3131
} catch (AwsException $exception) {
3232
if ($verbose) {
3333
echo "Attempt failed because of: {$exception->getMessage()}.\n";
34+
echo "Aws error type: {$exception->getAwsErrorType()}\n";
35+
echo "Aws error code: {$exception->getAwsErrorCode()}\n";
3436
echo "Waiting " . static::$waitTime . " seconds before trying again.\n";
3537
echo (static::$maxWaitAttempts - $attempts) . " attempts left.\n";
3638
}

php/example_code/aws_utilities/LoadMovieData.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use GuzzleHttp\Client as GuzzleClient;
5+
namespace AwsUtilities;
66

7-
//use ZipArchive;
7+
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\Exception\GuzzleException;
9+
use GuzzleHttp\Psr7\Utils;
10+
use ZipArchive;
811

912
function loadMovieData()
1013
{
@@ -13,8 +16,13 @@ function loadMovieData()
1316

1417
$url = 'https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip';
1518
$guzzle = new GuzzleClient(['verify' => false]);
16-
$file = \GuzzleHttp\Psr7\Utils::tryFopen($movieZipName, 'w');
17-
$guzzle->request("get", $url, ['sink' => $file]);
19+
$file = Utils::tryFopen($movieZipName, 'w');
20+
try {
21+
$guzzle->request("get", $url, ['sink' => $file]);
22+
} catch (GuzzleException $e) {
23+
echo "Could not retrieve remote file. {$e->getCode()}: {$e->getMessage()}\n";
24+
return null;
25+
}
1826

1927
$zip = new ZipArchive();
2028
$extractPath = ".";

php/example_code/aws_utilities/TestableReadline.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
function testable_readline($prompt)
2020
{
2121
global $LINES;
22-
if (count($LINES) > 0) {
22+
if ($LINES && count($LINES) > 0) {
2323
return array_shift($LINES);
2424
}
2525
return readline($prompt);

php/example_code/s3/CommandPool.php renamed to php/example_code/class_examples/CommandPool.php

+48-33
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,58 @@
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
44

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
813
*
14+
* It also assumes it will be run in a *nix environment.
915
*/
10-
// snippet-start:[s3.php.command_pool.complete]
11-
// snippet-start:[s3.php.command_pool.import]
12-
require 'vendor/autoload.php';
1316

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+
1723
use Aws\CommandInterface;
24+
use Aws\CommandPool;
25+
use Aws\Exception\AwsException;
1826
use Aws\ResultInterface;
27+
use Aws\S3\S3Client;
28+
use DirectoryIterator;
1929
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;
3332

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+
]);
3645

3746
// Create an iterator that yields files from a directory
38-
$files = new DirectoryIterator($fromDir);
47+
$files = new DirectoryIterator(__DIR__);
3948

4049
// Create a generator that converts the SplFileInfo objects into
4150
// Aws\CommandInterface objects. This generator accepts the iterator that
4251
// 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 */
4454
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()) {
4757
continue;
4858
}
4959
$filename = $file->getPath() . '/' . $file->getFilename();
@@ -57,9 +67,9 @@
5767
};
5868

5969
// Now create the generator using the files iterator
60-
$commands = $commandGenerator($files, $toBucket);
70+
$commands = $commandGenerator($files, $bucket);
6171

62-
// Create a pool and provide an optional array of configuration
72+
// Create a pool and provide an optional configuration array
6373
$pool = new CommandPool($client, $commands, [
6474
// Only send 5 files at a time (this is set to 25 by default)
6575
'concurrency' => 5,
@@ -93,6 +103,11 @@
93103
$promise->wait();
94104

95105
// 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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"require": {
3+
"aws/aws-sdk-php": "^3.209",
4+
"guzzlehttp/guzzle": "^7.0"
5+
},
6+
"autoload": {
7+
"psr-0": {
8+
"": "*"
9+
},
10+
"psr-4": {
11+
"S3\\": "../s3/",
12+
"AwsUtilities\\": "../aws_utilities/"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)