1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Script used to put a file in AWS S3 |
5
|
|
|
**/ |
6
|
|
|
|
7
|
|
|
require __DIR__ . "/../../vendor/autoload.php"; |
8
|
|
|
|
9
|
|
View Code Duplication |
function usage() |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
echo("Usage: php ". basename(__FILE__) . " [-h] [--rrs::] [--encrypt::] --bucket <s3 bucket> --file <filename> --from <filepath>\n"); |
12
|
|
|
echo("--help, -h: Print this help\n"); |
13
|
|
|
echo("--bucket <s3 bucket>: Name of the S3 bucket\n"); |
14
|
|
|
echo("--file <filename>: Name of the file to create in bucket. You can override local filename.\n"); |
15
|
|
|
echo("--from <filepath>: Full path to file to send to S3\n"); |
16
|
|
|
echo("--rrs: Activate type of storage in S3: REDUCED_REDUNDANCY\n"); |
17
|
|
|
echo("--encrypt: Activate Server encryption: AES256\n\n"); |
18
|
|
|
exit(0); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function check_input_parameters(&$options) |
22
|
|
|
{ |
23
|
|
View Code Duplication |
if (!count($options) || isset($options['h']) || |
24
|
|
|
isset($options['help'])) |
25
|
|
|
usage(); |
26
|
|
|
|
27
|
|
View Code Duplication |
if (!isset($options['bucket']) || !isset($options['file']) || |
28
|
|
|
!isset($options['from'])) |
29
|
|
|
{ |
30
|
|
|
print "Error: Missing mandatory parameter !\n"; |
31
|
|
|
usage(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$options['bucket'] = rtrim( $options['bucket'], "/"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$options = getopt("h", [ |
38
|
|
|
"bucket:", |
39
|
|
|
"file:", |
40
|
|
|
"from:", |
41
|
|
|
"force::", |
42
|
|
|
"help::", |
43
|
|
|
"rrs::", |
44
|
|
|
"encrypt::"]); |
45
|
|
|
check_input_parameters($options); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
# Check if preper env vars are setup |
49
|
|
|
if (!($region = getenv("AWS_DEFAULT_REGION"))) |
50
|
|
|
throw new CpeSdk\CpeException("Set 'AWS_DEFAULT_REGION' environment variable!"); |
51
|
|
|
|
52
|
|
|
// Get S3 client |
53
|
|
|
$s3 = new \Aws\S3\S3Client([ |
54
|
|
|
'version' => 'latest', |
55
|
|
|
'region' => $region |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$params = array( |
59
|
|
|
'Bucket' => $options['bucket'], |
60
|
|
|
'Key' => ltrim($options['file'], '/'), |
61
|
|
|
'SourceFile' => $options['from'], |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
// StorageClass and Encryption ? |
65
|
|
|
if (isset($options['rrs'])) |
66
|
|
|
$params['StorageClass'] = 'REDUCED_REDUNDANCY'; |
67
|
|
|
if (isset($options['encrypt'])) |
68
|
|
|
$params['ServerSideEncryption'] = 'AES256'; |
69
|
|
|
|
70
|
|
|
// Upload and Save file to S3 |
71
|
|
|
$s3->putObject($params); |
72
|
|
|
|
73
|
|
|
// Print JSON error output |
74
|
|
|
print json_encode([ "status" => "SUCCESS", |
75
|
|
|
"msg" => "[".__FILE__."] Upload '" . $options['from'] . "' to '" . $options['bucket'] . "/" . $options['file'] . "' successful !" ]); |
76
|
|
|
} |
77
|
|
|
catch (Exception $e) { |
78
|
|
|
$err = "Unable to put file '" . $options['from'] . "' into S3: '" . $options['bucket'] . "/" . $options['file'] . "'! " . $e->getMessage(); |
79
|
|
|
|
80
|
|
|
// Print JSON error output |
81
|
|
|
print json_encode([ "status" => "ERROR", |
82
|
|
|
"msg" => "[".__FILE__."] $err" ]); |
83
|
|
|
|
84
|
|
|
die("[".__FILE__."] $err"); |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.