Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for prefix filtering in bucket list #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Sources/S3/Extensions/S3+List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<BucketResults> {
public func list(bucket: String, region: Region? = nil, headers: [String: String], prefix: String?, delimiter: String?, on container: Container) throws -> Future<BucketResults> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please possibly default the values to nil?

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
}
Expand All @@ -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<BucketResults> {
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<BucketResults> {
return try list(bucket: bucket, region: region, headers: [:], on: container)
return try list(bucket: bucket, region: region, headers: [:], prefix: nil, delimiter: nil, on: container)
}

}
7 changes: 5 additions & 2 deletions Sources/S3/Protocols/S3Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public protocol S3Client: Service {

/// Get list of objects
func list(bucket: String, region: Region?, on container: Container) throws -> Future<BucketResults>


/// Get list of objects
func list(bucket: String, region: Region?, prefix: String?, delimiter: String?, on container: Container) throws -> Future<BucketResults>

/// Get list of objects
func list(bucket: String, region: Region?, headers: [String: String], on container: Container) throws -> Future<BucketResults>
func list(bucket: String, region: Region?, headers: [String: String], prefix: String?, delimiter: String?, on container: Container) throws -> Future<BucketResults>

/// Upload file to S3
func put(file: File.Upload, on container: Container) throws -> EventLoopFuture<File.Response>
Expand Down
2 changes: 1 addition & 1 deletion Sources/S3DemoApp/S3DemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public func routes(_ router: Router) throws {
// Delete bucket
router.get("files") { req -> Future<BucketResults> 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)
}
Expand Down