Skip to content

Commit

Permalink
Adds the ExpressRunner.php file and fixes some missing data cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
beqqrry-aws committed Oct 29, 2024
1 parent a7f98a0 commit 24233cb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
26 changes: 26 additions & 0 deletions php/example_code/s3/ExpressRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace S3;

use Aws\Exception\AwsException;
use AwsUtilities\RunnableExample;

require_once __DIR__ . "/vendor/autoload.php";

require "S3ExpressBasics.php";

try {
/**
* @var RunnableExample $runner
*/
$runner = new S3ExpressBasics();
$runner->helloService();
$runner->runExample();
} catch (AwsException $error) {
echo "Errored with the following: (" . $error->getCode() . ") - " . $error->getMessage();
} finally {
echo "Cleaning up.\n";
$runner->cleanUp();
}
37 changes: 26 additions & 11 deletions php/example_code/s3/S3ExpressBasics.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* or as a PHPUnit test:
* vendor/bin/phpunit S3ExpressBasicsTests.php
* /**/
**/

namespace S3;
use Aws\CloudFormation\CloudFormationClient;
Expand Down Expand Up @@ -123,7 +123,7 @@ public function runExample()
echo "Error waiting for the CloudFormation stack to create: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
$this->resources['StackName'] = $stackName;
$this->resources['stackName'] = $stackName;
$stackInfo = $this->cloudFormationClient->describeStacks([
'StackName' => $result['StackId'],
]);
Expand All @@ -138,10 +138,14 @@ public function runExample()
$expressUserName = $output['OutputValue'];
}
}
$this->resources['regularUserName'] = $regularUserName;
$this->resources['expressUserName'] = $expressUserName;
$regularKey = $this->iamService->createAccessKey($regularUserName);
$regularCredentials = new Credentials($regularKey['AccessKeyId'], $regularKey['SecretAccessKey']);
$this->resources['regularKey'] = $regularKey['AccessKeyId'];
$expressKey = $this->iamService->createAccessKey($expressUserName);
$expressCredentials = new Credentials($expressKey['AccessKeyId'], $expressKey['SecretAccessKey']);
$this->resources['expressKey'] = $expressKey['AccessKeyId'];

// 3. Create an additional client using the credentials with S3 Express permissions.
echo "\n";
Expand Down Expand Up @@ -317,19 +321,30 @@ public function cleanUp()
}

//delete the stack
if(isset($this->resources['StackName'])){
if(isset($this->resources['stackName'])){
$this->cloudFormationClient->deleteStack([
'StackName' => $this->resources['StackName'],
'StackName' => $this->resources['stackName'],
]);
unset($this->resources['StackName']);
unset($this->resources['stackName']);
}

// $this->iamService->deleteRole($this->resources['roleName']);
// unset($this->resources['roleName']);

// delete User
// $this->iamService->deleteUser($this->resources['userName']);
// unset($this->resources['userName']);
// delete User data
if(isset($this->resources['regularKey'])) {
$this->iamService->deleteAccessKey($this->resources['regularKey'], $this->resources['regularUserName']);
unset($this->resources['regularKey']);
}
if(isset($this->resources['regularUserName'])) {
$this->iamService->deleteUser($this->resources['regularUserName']);
unset($this->resources['regularUserName']);
}
if(isset($this->resources['expressKey'])) {
$this->iamService->deleteAccessKey($this->resources['expressKey'], $this->resources['expressUserName']);
unset($this->resources['expressKey']);
}
if(isset($this->resources['expressUserName'])) {
$this->iamService->deleteUser($this->resources['expressUserName']);
unset($this->resources['expressUserName']);
}

// delete all the objects in both buckets
if(isset($this->resources['objectKey'])){
Expand Down

0 comments on commit 24233cb

Please sign in to comment.