Skip to content

Commit

Permalink
bugix + custom signer
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed May 12, 2018
1 parent e85253f commit 94eeeba
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 20 deletions.
6 changes: 0 additions & 6 deletions Sources/S3/Extensions/S3+Bucket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public extension S3 {

// /// Get bucket location
// public func location(bucket: String, on container: Container) throws -> Future<Bucket.Location> {
// 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
Expand All @@ -45,8 +43,6 @@ public extension S3 {

/// Delete bucket
public func delete(bucket: String, region: Region? = nil, on container: Container) throws -> Future<Void> {
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
Expand All @@ -64,8 +60,6 @@ public extension S3 {

/// Create a bucket
public func create(bucket: String, region: Region? = nil, on container: Container) throws -> Future<Void> {
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
Expand Down
1 change: 0 additions & 1 deletion Sources/S3/Extensions/S3+Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public extension S3 {

/// Delete file from S3
public func delete(file: LocationConvertible, headers: [String: String] = [:], on container: Container) throws -> Future<Void> {
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
Expand Down
1 change: 0 additions & 1 deletion Sources/S3/Extensions/S3+Get.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<File.Response> {
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
Expand Down
1 change: 0 additions & 1 deletion Sources/S3/Extensions/S3+List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<BucketResults> {
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 {
Expand Down
3 changes: 1 addition & 2 deletions Sources/S3/Extensions/S3+ObjectInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<File.Info> {
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)
Expand Down
4 changes: 1 addition & 3 deletions Sources/S3/Extensions/S3+Put.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<File.Response> {
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))

Expand Down
1 change: 0 additions & 1 deletion Sources/S3/Extensions/S3+Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public extension S3 {

/// Get list of buckets
public func buckets(on container: Container) throws -> Future<BucketsInfo> {
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
Expand Down
12 changes: 7 additions & 5 deletions Sources/S3/S3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 94eeeba

Please sign in to comment.