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

Commit 7661c86

Browse files
committed
Exists API
1 parent 9f7b0a3 commit 7661c86

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/LiquidAwsS3Driver/LiquidAwsS3Storage.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ struct LiquidAwsS3Storage: FileStorage {
6868
func delete(key: String) -> EventLoopFuture<Void> {
6969
s3.deleteObject(S3.DeleteObjectRequest(bucket: bucket, key: key)).map { _ in }
7070
}
71+
72+
func exists(key: String) -> EventLoopFuture<Bool> {
73+
s3.getObject(S3.GetObjectRequest(bucket: bucket, key: key)).map { _ in true }.flatMapError { err -> EventLoopFuture<Bool> in
74+
if let err = err as? SotoS3.S3ErrorType, err == SotoS3.S3ErrorType.noSuchKey {
75+
return s3.eventLoopGroup.next().makeSucceededFuture(false)
76+
}
77+
return s3.eventLoopGroup.next().makeFailedFuture(err)
78+
}
79+
}
80+
7181
}
7282

7383

Tests/LiquidAwsS3DriverTests/LiquidAwsS3DriverTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ final class LiquidAwsS3DriverTests: XCTestCase {
103103
let res = try fs.list(key: "dir02").wait()
104104
XCTAssertEqual(res, ["dir03", "test-01.txt"])
105105
}
106+
107+
func testExists() throws {
108+
let fs = try createTestStorage()
109+
110+
let key1 = "non-existing-thing"
111+
let exists1 = try fs.exists(key: key1).wait()
112+
XCTAssertFalse(exists1)
113+
114+
let key2 = "my/dir"
115+
_ = try fs.createDirectory(key: key2).wait()
116+
let exists2 = try fs.exists(key: key2).wait()
117+
XCTAssertTrue(exists2)
118+
}
106119

107120
/*
108121
func testUploadWithCustomEndpoint() throws {

0 commit comments

Comments
 (0)