/usr/share/php/Aws/S3/BatchDelete.php is in php-aws-sdk 3.15.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | <?php
namespace Aws\S3;
use Aws\AwsClientInterface;
use Aws\S3\Exception\DeleteMultipleObjectsException;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromisorInterface;
use GuzzleHttp\Promise\PromiseInterface;
/**
* Efficiently deletes many objects from a single Amazon S3 bucket using an
* iterator that yields keys. Deletes are made using the DeleteObjects API
* operation.
*
* $s3 = new Aws\S3\Client([
* 'region' => 'us-west-2',
* 'version' => 'latest'
* ]);
*
* $listObjectsParams = ['Bucket' => 'foo', 'Prefix' => 'starts/with/'];
* $delete = Aws\S3\BatchDelete::fromListObjects($s3, $listObjectsParams);
* // Asynchronously delete
* $promise = $delete->promise();
* // Force synchronous completion
* $delete->delete();
*
* When using one of the batch delete creational static methods, you can supply
* an associative array of options:
*
* - before: Function invoked before executing a command. The function is
* passed the command that is about to be executed. This can be useful
* for logging, adding custom request headers, etc.
* - batch_size: The size of each delete batch. Defaults to 1000.
*
* @link http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
*/
class BatchDelete implements PromisorInterface
{
private $bucket;
/** @var AwsClientInterface */
private $client;
/** @var callable */
private $before;
/** @var PromiseInterface */
private $cachedPromise;
/** @var callable */
private $promiseCreator;
private $batchSize = 1000;
private $queue = [];
/**
* Creates a BatchDelete object from all of the paginated results of a
* ListObjects operation. Each result that is returned by the ListObjects
* operation will be deleted.
*
* @param AwsClientInterface $client AWS Client to use.
* @param array $listObjectsParams ListObjects API parameters
* @param array $options BatchDelete options.
*
* @return BatchDelete
*/
public static function fromListObjects(
AwsClientInterface $client,
array $listObjectsParams,
array $options = []
) {
$iter = $client->getPaginator('ListObjects', $listObjectsParams);
$bucket = $listObjectsParams['Bucket'];
$fn = function (BatchDelete $that) use ($iter) {
return $iter->each(function ($result) use ($that) {
$promises = [];
if (is_array($result['Contents'])) {
foreach ($result['Contents'] as $object) {
if ($promise = $that->enqueue($object)) {
$promises[] = $promise;
}
}
}
return $promises ? Promise\all($promises) : null;
});
};
return new self($client, $bucket, $fn, $options);
}
/**
* Creates a BatchDelete object from an iterator that yields results.
*
* @param AwsClientInterface $client AWS Client to use to execute commands
* @param string $bucket Bucket where the objects are stored
* @param \Iterator $iter Iterator that yields assoc arrays
* @param array $options BatchDelete options
*
* @return BatchDelete
*/
public static function fromIterator(
AwsClientInterface $client,
$bucket,
\Iterator $iter,
array $options = []
) {
$fn = function (BatchDelete $that) use ($iter) {
return \GuzzleHttp\Promise\coroutine(function () use ($that, $iter) {
foreach ($iter as $obj) {
if ($promise = $that->enqueue($obj)) {
yield $promise;
}
}
});
};
return new self($client, $bucket, $fn, $options);
}
public function promise()
{
if (!$this->cachedPromise) {
$this->cachedPromise = $this->createPromise();
}
return $this->cachedPromise;
}
/**
* Synchronously deletes all of the objects.
*
* @throws DeleteMultipleObjectsException on error.
*/
public function delete()
{
$this->promise()->wait();
}
/**
* @param AwsClientInterface $client Client used to transfer the requests
* @param string $bucket Bucket to delete from.
* @param callable $promiseFn Creates a promise.
* @param array $options Hash of options used with the batch
*
* @throws \InvalidArgumentException if the provided batch_size is <= 0
*/
private function __construct(
AwsClientInterface $client,
$bucket,
callable $promiseFn,
array $options = []
) {
$this->client = $client;
$this->bucket = $bucket;
$this->promiseCreator = $promiseFn;
if (isset($options['before'])) {
if (!is_callable($options['before'])) {
throw new \InvalidArgumentException('before must be callable');
}
$this->before = $options['before'];
}
if (isset($options['batch_size'])) {
if ($options['batch_size'] <= 0) {
throw new \InvalidArgumentException('batch_size is not > 0');
}
$this->batchSize = min($options['batch_size'], 1000);
}
}
private function enqueue(array $obj)
{
$this->queue[] = $obj;
return count($this->queue) >= $this->batchSize
? $this->flushQueue()
: null;
}
private function flushQueue()
{
static $validKeys = ['Key' => true, 'VersionId' => true];
if (count($this->queue) === 0) {
return null;
}
$batch = [];
while ($obj = array_shift($this->queue)) {
$batch[] = array_intersect_key($obj, $validKeys);
}
$command = $this->client->getCommand('DeleteObjects', [
'Bucket' => $this->bucket,
'Delete' => ['Objects' => $batch]
]);
if ($this->before) {
call_user_func($this->before, $command);
}
return $this->client->executeAsync($command)
->then(function ($result) {
if (!empty($result['Errors'])) {
throw new DeleteMultipleObjectsException(
$result['Deleted'] ?: [],
$result['Errors']
);
}
return $result;
});
}
/**
* Returns a promise that will clean up any references when it completes.
*
* @return PromiseInterface
*/
private function createPromise()
{
// Create the promise
$promise = call_user_func($this->promiseCreator, $this);
$this->promiseCreator = null;
// Cleans up the promise state and references.
$cleanup = function () {
$this->before = $this->client = $this->queue = null;
};
// When done, ensure cleanup and that any remaining are processed.
return $promise->then(
function () use ($cleanup) {
return Promise\promise_for($this->flushQueue())
->then($cleanup);
},
function ($reason) use ($cleanup) {
$cleanup();
return Promise\rejection_for($reason);
}
);
}
}
|