From 94eeeba03063725a7dc5a8f77447d6d4b982cfed Mon Sep 17 00:00:00 2001 From: Ondrej Rafaj Date: Sun, 13 May 2018 00:59:13 +0100 Subject: [PATCH] bugix + custom signer --- Sources/S3/Extensions/S3+Bucket.swift | 6 ------ Sources/S3/Extensions/S3+Delete.swift | 1 - Sources/S3/Extensions/S3+Get.swift | 1 - Sources/S3/Extensions/S3+List.swift | 1 - Sources/S3/Extensions/S3+ObjectInfo.swift | 3 +-- Sources/S3/Extensions/S3+Put.swift | 4 +--- Sources/S3/Extensions/S3+Service.swift | 1 - Sources/S3/S3.swift | 12 +++++++----- 8 files changed, 9 insertions(+), 20 deletions(-) diff --git a/Sources/S3/Extensions/S3+Bucket.swift b/Sources/S3/Extensions/S3+Bucket.swift index a8b8d1a..2a523e0 100644 --- a/Sources/S3/Extensions/S3+Bucket.swift +++ b/Sources/S3/Extensions/S3+Bucket.swift @@ -17,8 +17,6 @@ public extension S3 { // /// Get bucket location // public func location(bucket: String, on container: Container) throws -> Future { -// let signer = try container.makeS3Signer() -// // let region = region ?? signer.config.region // guard let url = URL(string: "https://\(bucket).s3.amazonaws.com/?location"), let host = url.host else { // throw Error.invalidUrl @@ -45,8 +43,6 @@ public extension S3 { /// Delete bucket public func delete(bucket: String, region: Region? = nil, on container: Container) throws -> Future { - let signer = try container.makeS3Signer() - let region = region ?? signer.config.region guard let url = URL(string: "https://\(bucket).s3.\(region.rawValue).amazonaws.com/"), let host = url.host else { throw Error.invalidUrl @@ -64,8 +60,6 @@ public extension S3 { /// Create a bucket public func create(bucket: String, region: Region? = nil, on container: Container) throws -> Future { - let signer = try container.makeS3Signer() - let region = region ?? signer.config.region guard let url = URL(string: "https://\(bucket).s3.\(region.rawValue).amazonaws.com/"), let host = url.host else { throw Error.invalidUrl diff --git a/Sources/S3/Extensions/S3+Delete.swift b/Sources/S3/Extensions/S3+Delete.swift index f5b41e8..12d193b 100644 --- a/Sources/S3/Extensions/S3+Delete.swift +++ b/Sources/S3/Extensions/S3+Delete.swift @@ -16,7 +16,6 @@ public extension S3 { /// Delete file from S3 public func delete(file: LocationConvertible, headers: [String: String] = [:], on container: Container) throws -> Future { - let signer = try container.makeS3Signer() let url = try self.url(file: file, on: container) let headers = try signer.headers(for: .DELETE, urlString: url.absoluteString, headers: headers, payload: .none) return try make(request: url, method: .DELETE, headers: headers, data: "".convertToData(), on: container).map(to: Void.self) { response in diff --git a/Sources/S3/Extensions/S3+Get.swift b/Sources/S3/Extensions/S3+Get.swift index b007473..1acfba3 100644 --- a/Sources/S3/Extensions/S3+Get.swift +++ b/Sources/S3/Extensions/S3+Get.swift @@ -16,7 +16,6 @@ public extension S3 { /// Retrieve file data from S3 public func get(file: LocationConvertible, headers: [String: String] = [:], on container: Container) throws -> Future { - let signer = try container.makeS3Signer() let url = try self.url(file: file, on: container) let headers = try signer.headers(for: .GET, urlString: url.absoluteString, headers: headers, payload: .none) return try make(request: url, method: .GET, headers: headers, on: container).map(to: File.Response.self) { response in diff --git a/Sources/S3/Extensions/S3+List.swift b/Sources/S3/Extensions/S3+List.swift index c6cc4df..ddc6360 100644 --- a/Sources/S3/Extensions/S3+List.swift +++ b/Sources/S3/Extensions/S3+List.swift @@ -13,7 +13,6 @@ extension S3 { /// Get list of objects public func list(bucket: String, region: Region? = nil, headers: [String: String], on container: Container) throws -> Future { - let signer = try container.makeS3Signer() let region = region ?? signer.config.region guard let baseUrl = URL(string: "https://\(bucket).s3.\(region.rawValue).amazonaws.com/"), let host = baseUrl.host, var components = URLComponents(string: baseUrl.absoluteString) else { diff --git a/Sources/S3/Extensions/S3+ObjectInfo.swift b/Sources/S3/Extensions/S3+ObjectInfo.swift index 8413299..7878ef4 100644 --- a/Sources/S3/Extensions/S3+ObjectInfo.swift +++ b/Sources/S3/Extensions/S3+ObjectInfo.swift @@ -30,14 +30,13 @@ public extension S3 { /// Get file information (HEAD) /// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html public func get(fileInfo file: LocationConvertible, headers: [String: String] = [:], on container: Container) throws -> Future { - let signer = try container.makeS3Signer() let url = try self.url(file: file, on: container) let headers = try signer.headers(for: .HEAD, urlString: url.absoluteString, headers: headers, payload: .none) return try make(request: url, method: .HEAD, headers: headers, data: "".convertToData(), on: container).map(to: File.Info.self) { response in try self.check(response) let bucket = file.bucket ?? self.defaultBucket - let region = file.region ?? signer.config.region + let region = file.region ?? self.signer.config.region let mime = response.http.headers.string(File.Info.CodingKeys.mime.rawValue) let size = response.http.headers.int(File.Info.CodingKeys.size.rawValue) let server = response.http.headers.string(File.Info.CodingKeys.server.rawValue) diff --git a/Sources/S3/Extensions/S3+Put.swift b/Sources/S3/Extensions/S3+Put.swift index 5d3c9b1..a0a66e6 100755 --- a/Sources/S3/Extensions/S3+Put.swift +++ b/Sources/S3/Extensions/S3+Put.swift @@ -17,12 +17,10 @@ public extension S3 { /// Upload file to S3 public func put(file: File.Upload, headers: [String: String] = [:], on container: Container) throws -> EventLoopFuture { - let signer = try container.makeS3Signer() - let url = try self.url(file: file, on: container) var awsHeaders: [String: String] = headers - awsHeaders["content-Type"] = file.mime.description + awsHeaders["content-type"] = file.mime.description awsHeaders["x-amz-acl"] = file.access.rawValue let headers = try signer.headers(for: .PUT, urlString: url.absoluteString, headers: awsHeaders, payload: Payload.bytes(file.data)) diff --git a/Sources/S3/Extensions/S3+Service.swift b/Sources/S3/Extensions/S3+Service.swift index 98936f2..f6cc1f1 100644 --- a/Sources/S3/Extensions/S3+Service.swift +++ b/Sources/S3/Extensions/S3+Service.swift @@ -17,7 +17,6 @@ public extension S3 { /// Get list of buckets public func buckets(on container: Container) throws -> Future { - let signer = try container.makeS3Signer() let url = try self.url(on: container) let headers = try signer.headers(for: .GET, urlString: url.absoluteString, payload: .none) return try make(request: url, method: .GET, headers: headers, data: "".convertToData(), on: container).map(to: BucketsInfo.self) { response in diff --git a/Sources/S3/S3.swift b/Sources/S3/S3.swift index d65e997..bf74f01 100755 --- a/Sources/S3/S3.swift +++ b/Sources/S3/S3.swift @@ -29,20 +29,24 @@ public class S3: S3Client { /// If set, this bucket name value will be used globally unless overriden by a specific call public internal(set) var defaultBucket: String + /// Signer instance + let signer: S3Signer // MARK: Initialization /// Basic initialization method, also registers S3Signer and self with services @discardableResult public convenience init(defaultBucket: String, config: S3Signer.Config, services: inout Services) throws { - try self.init(defaultBucket: defaultBucket) + let signer = try S3Signer(config) + try self.init(defaultBucket: defaultBucket, signer: signer) - try services.register(S3Signer(config)) + services.register(signer) services.register(self, as: S3Client.self) } /// Basic initialization method - public init(defaultBucket: String) throws { + public init(defaultBucket: String, signer: S3Signer) throws { self.defaultBucket = defaultBucket + self.signer = signer } } @@ -78,7 +82,6 @@ extension S3 { /// Base URL for S3 region func url(region: Region? = nil, bucket: String? = nil, on container: Container) throws -> URL { - let signer = try container.makeS3Signer() let urlString = (region ?? signer.config.region).hostUrlString + (bucket?.finished(with: "/") ?? "") guard let url = URL(string: urlString) else { throw Error.invalidUrl @@ -88,7 +91,6 @@ extension S3 { /// Base URL for a file in a bucket func url(file: LocationConvertible, on container: Container) throws -> URL { - let signer = try container.makeS3Signer() let bucket = file.bucket ?? defaultBucket guard let url = URL(string: signer.config.region.hostUrlString + bucket.finished(with: "/") + file.path) else { throw Error.invalidUrl