diff --git a/Sources/S3/Extensions/S3+List.swift b/Sources/S3/Extensions/S3+List.swift index 51be8e8..5048d74 100644 --- a/Sources/S3/Extensions/S3+List.swift +++ b/Sources/S3/Extensions/S3+List.swift @@ -12,15 +12,20 @@ import Foundation extension S3 { /// Get list of objects - public func list(bucket: String, region: Region? = nil, headers: [String: String], on container: Container) throws -> Future { + public func list(bucket: String, region: Region? = nil, headers: [String: String], prefix: String?, delimiter: String?, on container: Container) throws -> Future { let region = region ?? signer.config.region guard let baseUrl = URL(string: region.hostUrlString(bucket: bucket)), let host = baseUrl.host, var components = URLComponents(string: baseUrl.absoluteString) else { throw S3.Error.invalidUrl } - components.queryItems = [ - URLQueryItem(name: "list-type", value: "2") - ] + var queryItems = [ URLQueryItem(name: "list-type", value: "2") ] + if let prefix = prefix, !prefix.isEmpty { + queryItems.append(URLQueryItem(name: "prefix", value: prefix)) + } + if let delimiter = delimiter, !delimiter.isEmpty { + queryItems.append(URLQueryItem(name: "delimiter", value: delimiter)) + } + components.queryItems = queryItems guard let url = components.url else { throw S3.Error.invalidUrl } @@ -33,9 +38,14 @@ extension S3 { } } + /// Get list of objects + public func list(bucket: String, region: Region? = nil, prefix: String?, delimiter: String?, on container: Container) throws -> Future { + return try list(bucket: bucket, region: region, headers: [:], prefix: prefix, delimiter: delimiter, on: container) + } + /// Get list of objects public func list(bucket: String, region: Region? = nil, on container: Container) throws -> Future { - return try list(bucket: bucket, region: region, headers: [:], on: container) + return try list(bucket: bucket, region: region, headers: [:], prefix: nil, delimiter: nil, on: container) } } diff --git a/Sources/S3/Protocols/S3Client.swift b/Sources/S3/Protocols/S3Client.swift index 0d61637..f962c89 100644 --- a/Sources/S3/Protocols/S3Client.swift +++ b/Sources/S3/Protocols/S3Client.swift @@ -29,9 +29,12 @@ public protocol S3Client: Service { /// Get list of objects func list(bucket: String, region: Region?, on container: Container) throws -> Future - + + /// Get list of objects + func list(bucket: String, region: Region?, prefix: String?, delimiter: String?, on container: Container) throws -> Future + /// Get list of objects - func list(bucket: String, region: Region?, headers: [String: String], on container: Container) throws -> Future + func list(bucket: String, region: Region?, headers: [String: String], prefix: String?, delimiter: String?, on container: Container) throws -> Future /// Upload file to S3 func put(file: File.Upload, on container: Container) throws -> EventLoopFuture diff --git a/Sources/S3DemoApp/S3DemoApp.swift b/Sources/S3DemoApp/S3DemoApp.swift index 0d78d6c..f5a015b 100644 --- a/Sources/S3DemoApp/S3DemoApp.swift +++ b/Sources/S3DemoApp/S3DemoApp.swift @@ -42,7 +42,7 @@ public func routes(_ router: Router) throws { // Delete bucket router.get("files") { req -> Future in let s3 = try req.makeS3Client() - return try s3.list(bucket: "booststore", region: .usEast1, headers: [:], on: req).catchMap({ (error) -> (BucketResults) in + return try s3.list(bucket: "booststore", region: .usEast1, on: req).catchMap({ (error) -> (BucketResults) in if let error = error.s3ErrorMessage() { print(error.message) }