forked from powerkernel/directadmin-s3-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_upload_s3.php
134 lines (118 loc) · 3.35 KB
/
ftp_upload_s3.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @author Harry Tang <[email protected]>
* @link https://powerkernel.com
* @copyright Copyright (c) 2018 Power Kernel
*/
/* @var $ftp_local_file string $argv[1] */
/* @var $ftp_remote_file string $argv[2] */
use Aws\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
require __DIR__ . '/vendor/autoload.php';
$conf = require __DIR__ . '/config.php';
//$date = date('Ymd');
$bucket = $conf['bucket'];
$ftp_local_file=$argv[1];
$ftp_remote_file=$argv[2];
// S3 Client
$opts = [
//'profile' => 'default',
'version' => 'latest',
'region' => $conf['region'],
'credentials' => $conf['credentials'],
];
if (!empty($conf['endpoint'])) {
$opts['endpoint'] = $conf['endpoint'];
}
$client = new Aws\S3\S3Client($opts);
// check bucket exist
$exist = false;
$buckets = $client->listBuckets();
if ($buckets) {
foreach ($buckets['Buckets'] as $i => $b) {
if ($b['Name'] == $bucket) {
$exist = true;
}
}
}
// Create Bucket
if (!$exist) {
$client->createBucket([
'Bucket' => $bucket,
'LocationConstraint' => $conf['region'],
]);
// Poll the bucket until it is accessible
$client->waitUntil('BucketExists', [
'Bucket' => $bucket
]);
// add Lifecycle
$client->putBucketLifecycleConfiguration(
[
'Bucket' => $bucket, // REQUIRED
'LifecycleConfiguration' => [
'Rules' => [ // REQUIRED
[
'ID' => 'AutoDelete',
'AbortIncompleteMultipartUpload' => [
'DaysAfterInitiation' => 1,
],
'Expiration' => [
'Days' => $conf['expiration'],
],
'Status' => 'Enabled',
'Prefix' => ''
],
],
],
]
);
}
// check and apply lifecycle
$hasLifeCycle = false;
try {
$r = $client->getBucketLifecycle(['Bucket' => $bucket]);
$rules = $r->get('Rules');
foreach ($rules as $rule) {
if ($rule['ID'] == 'AutoDelete') {
$hasLifeCycle = true;
}
}
} catch (\Aws\S3\Exception\S3Exception $e) {
}
if (!$hasLifeCycle) {
// Config auto delete
$client->putBucketLifecycleConfiguration(
[
'Bucket' => $bucket, // REQUIRED
'LifecycleConfiguration' => [
'Rules' => [ // REQUIRED
[
'ID' => 'AutoDelete',
'AbortIncompleteMultipartUpload' => [
'DaysAfterInitiation' => 1,
],
'Expiration' => [
'Days' => $conf['expiration'],
],
'Status' => 'Enabled',
'Prefix' => ''
],
],
],
]
);
echo "adding lifecycle";
} else {
echo "lifecycle already added.";
}
// Upload
$uploader = new MultipartUploader($client, $ftp_local_file, [
'bucket' => $bucket,
'key' => date('Y-m-d') . '/' . $ftp_remote_file,
]);
try {
$result = $uploader->upload();
echo "Upload complete: {$result['ObjectURL']}\n";
} catch (MultipartUploadException $e) {
echo $e->getMessage() . "\n";
}