Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 2a1cdb8

Browse files
authored
[Flysystem 1] Better check if file exists. (#502)
* [Flysystem 1] Better check if file exists. * Updated change log
1 parent fdf1bec commit 2a1cdb8

File tree

3 files changed

+13
-39
lines changed

3 files changed

+13
-39
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 0.3.2
4+
5+
### Fixed
6+
7+
- Better check if file exists.
8+
39
## 0.3.1
410

511
### Added

src/S3FilesystemV1.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,12 @@ public function has($path)
173173
{
174174
$location = $this->applyPathPrefix($path);
175175

176-
$result = $this->client->getObject(array_merge($this->options, [
176+
$result = $this->client->objectExists(array_merge($this->options, [
177177
'Bucket' => $this->bucket,
178178
'Key' => $location,
179179
]));
180180

181-
try {
182-
$result->resolve();
183-
184-
return true;
185-
} catch (ClientException $e) {
186-
if (404 !== $e->getResponse()->getStatusCode()) {
187-
throw $e;
188-
}
189-
}
190-
191-
return $this->doesDirectoryExist($location);
181+
return $result->isSuccess();
192182
}
193183

194184
/**
@@ -547,30 +537,6 @@ protected function normalizeResponse(object $output, ?string $path = null): arra
547537
return array_merge($result, $this->resultMap($output), ['type' => 'file']);
548538
}
549539

550-
protected function doesDirectoryExist(string $location): bool
551-
{
552-
// Maybe this isn't an actual key, but a prefix.
553-
// Do a prefix listing of objects to determine.
554-
$result = $this->client->listObjectsV2([
555-
'Bucket' => $this->bucket,
556-
'Prefix' => rtrim($location, '/') . '/',
557-
'MaxKeys' => 1,
558-
]
559-
);
560-
561-
try {
562-
$result->resolve();
563-
564-
return !empty($result->getContents()) || !empty($result->getCommonPrefixes());
565-
} catch (ClientException $e) {
566-
if (403 === $e->getResponse()->getStatusCode()) {
567-
return false;
568-
}
569-
570-
throw $e;
571-
}
572-
}
573-
574540
private function retrievePaginatedListing(array $options): array
575541
{
576542
$result = $this->client->listObjectsV2($options);

tests/Unit/S3FilesystemV1Test.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use AsyncAws\Core\Test\ResultMockFactory;
88
use AsyncAws\Core\Test\SimpleResultStream;
9+
use AsyncAws\Core\Waiter;
910
use AsyncAws\Flysystem\S3\S3FilesystemV1;
1011
use AsyncAws\S3\Enum\StorageClass;
1112
use AsyncAws\S3\Result\CopyObjectOutput;
@@ -14,6 +15,7 @@
1415
use AsyncAws\S3\Result\GetObjectOutput;
1516
use AsyncAws\S3\Result\HeadObjectOutput;
1617
use AsyncAws\S3\Result\ListObjectsV2Output;
18+
use AsyncAws\S3\Result\ObjectExistsWaiter;
1719
use AsyncAws\S3\Result\PutObjectAclOutput;
1820
use AsyncAws\S3\Result\PutObjectOutput;
1921
use AsyncAws\S3\S3Client;
@@ -244,11 +246,11 @@ public function testHasFile()
244246

245247
$s3Client = $this->getMockBuilder(S3Client::class)
246248
->disableOriginalConstructor()
247-
->onlyMethods(['getObject'])
249+
->onlyMethods(['objectExists'])
248250
->getMock();
249251

250252
$s3Client->expects(self::once())
251-
->method('getObject')
253+
->method('objectExists')
252254
->with(self::callback(function (array $input) use ($path) {
253255
if ($input['Key'] !== self::PREFIX . '/' . $path) {
254256
return false;
@@ -259,7 +261,7 @@ public function testHasFile()
259261
}
260262

261263
return true;
262-
}))->willReturn(ResultMockFactory::create(GetObjectOutput::class));
264+
}))->willReturn(ResultMockFactory::waiter(ObjectExistsWaiter::class, Waiter::STATE_SUCCESS));
263265

264266
$filesystem = new S3FilesystemV1($s3Client, self::BUCKCET, self::PREFIX);
265267

0 commit comments

Comments
 (0)