forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateBucket.php
More file actions
55 lines (46 loc) · 1.6 KB
/
Copy pathCreateBucket.php
File metadata and controls
55 lines (46 loc) · 1.6 KB
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
47
48
49
50
51
52
53
54
55
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[s3.php.create_bucket.complete]
// snippet-start:[s3.php.create_bucket.import]
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\S3\S3Client;
// snippet-end:[s3.php.create_bucket.import]
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Creates a bucket in Amazon S3.
*
* Inputs:
* - $s3Client: An initialized AWS SDK for PHP API client for S3.
* - $bucketName: The name of the bucket to create.
*
* Returns: Information about the bucket; otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
// snippet-start:[s3.php.create_bucket.main]
function createBucket($s3Client, $bucketName)
{
try {
$result = $s3Client->createBucket([
'Bucket' => $bucketName,
]);
return 'The bucket\'s location is: ' .
$result['Location'] . '. ' .
'The bucket\'s effective URI is: ' .
$result['@metadata']['effectiveUri'];
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}
function createTheBucket()
{
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2006-03-01'
]);
echo createBucket($s3Client, 'amzn-s3-demo-bucket');
}
// Uncomment the following line to run this code in an AWS account.
// createTheBucket();
// snippet-end:[s3.php.create_bucket.main]
// snippet-end:[s3.php.create_bucket.complete]