Skip to content

Commit

Permalink
Add ability to delete multiple S3 objects
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeskhan47 authored Nov 20, 2024
1 parent 462d922 commit b757623
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/AwsS3V3/AwsS3V3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,31 @@ public function readStream(string $path)
return $resource;
}

public function delete(string $path): void
public function delete(string|array $paths): void
{
$arguments = ['Bucket' => $this->bucket, 'Key' => $this->prefixer->prefixPath($path)];
$command = $this->client->getCommand('DeleteObject', $arguments);
$paths = is_array($paths) ? $paths : [$paths];

$deleteObjects = [];

foreach ($paths as $path) {
$deleteObjects[] = [
'Key' => $this->prefixer->prefixPath($path),
];
}

$arguments = [
'Bucket' => $this->bucket,
'Delete' => [
'Objects' => $deleteObjects,
],
];

$command = $this->client->getCommand('DeleteObjects', $arguments);

try {
$this->client->execute($command);
} catch (Throwable $exception) {
throw UnableToDeleteFile::atLocation($path, '', $exception);
throw UnableToDeleteFile::atLocation(implode(', ', $paths), '', $exception);
}
}

Expand Down

0 comments on commit b757623

Please sign in to comment.