-
Notifications
You must be signed in to change notification settings - Fork 1
/
purgeBucket.php
77 lines (63 loc) · 1.79 KB
/
purgeBucket.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
<?php
require __DIR__ . '/vendor/autoload.php';
use Aws\CommandPool;
use Aws\S3\S3Client;
use Dotenv\Dotenv;
/**
* @param array $objects
* @return int
*/
function checkBucketIsEmpty($objects)
{
return count($objects);
}
/**
* @param Aws\S3\S3Client $clientS3 initialized before
* @return \AWS\Result
*/
function getObjects($clientS3)
{
return $clientS3->listObjects(
[
'Bucket' => $_ENV['S3_BUCKET_NAME'],
'MaxKeys' => 6000,
]
);
}
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required('S3_REGION')->notEmpty();
$dotenv->required('S3_KEY')->notEmpty();
$dotenv->required('S3_SECRET')->notEmpty();
$dotenv->required('S3_ENDPOINT')->notEmpty();
$dotenv->required('S3_BUCKET_NAME')->notEmpty();
$s3 = new S3Client([
'version' => '2006-03-01',
'region' => $_ENV['S3_REGION'],
'credentials' => [
'key' => $_ENV['S3_KEY'],
'secret' => $_ENV['S3_SECRET'],
],
'endpoint' => $_ENV['S3_ENDPOINT'],
]);
$objects = getObjects($s3);
$generatorDeleteObject = function ($objects) use ($s3) {
foreach ($objects['Contents'] as $object) {
yield $s3->getCommand('deleteObject', [
'Bucket' => $_ENV['S3_BUCKET_NAME'],
'Key' => $object['Key']
]);
}
};
while (CheckBucketIsEmpty($objects['Contents'])) {
$generator = $generatorDeleteObject($objects);
$pool = new CommandPool($s3, $generator);
$pool->promise();
$objects = getObjects($s3);
if (empty($objects['Contents'])) {
print("\nNothing objects in your bucket ! 🪣\n");
break;
}
}
print("\n\nIt's done ! 🎉\n\n");
print("🚨 Please, you must rollback your database, in particular the oc_storage database table, before rerun the migration program.\n\n");