Skip to content

Commit

Permalink
Merge pull request #2 from bignerdranch/master
Browse files Browse the repository at this point in the history
Support vapor 2.0
  • Loading branch information
rafiki270 authored May 26, 2017
2 parents a0845f5 + 76f9a25 commit 2e03fb0
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
.DS_Store
S3.xcodeproj
114 changes: 114 additions & 0 deletions Package.pins
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"autoPin": true,
"pins": [
{
"package": "BCrypt",
"reason": null,
"repositoryURL": "https://github.com/vapor/bcrypt.git",
"version": "1.0.0"
},
{
"package": "Bits",
"reason": null,
"repositoryURL": "https://github.com/vapor/bits.git",
"version": "1.0.0"
},
{
"package": "CTLS",
"reason": null,
"repositoryURL": "https://github.com/vapor/ctls.git",
"version": "1.0.0"
},
{
"package": "Console",
"reason": null,
"repositoryURL": "https://github.com/vapor/console.git",
"version": "2.1.0"
},
{
"package": "Core",
"reason": null,
"repositoryURL": "https://github.com/vapor/core.git",
"version": "2.0.2"
},
{
"package": "Crypto",
"reason": null,
"repositoryURL": "https://github.com/vapor/crypto.git",
"version": "2.0.0"
},
{
"package": "Debugging",
"reason": null,
"repositoryURL": "https://github.com/vapor/debugging.git",
"version": "1.0.0"
},
{
"package": "Engine",
"reason": null,
"repositoryURL": "https://github.com/vapor/engine.git",
"version": "2.0.0"
},
{
"package": "JSON",
"reason": null,
"repositoryURL": "https://github.com/vapor/json.git",
"version": "2.0.2"
},
{
"package": "MimeLib",
"reason": null,
"repositoryURL": "https://github.com/manGoweb/MimeLib.git",
"version": "1.0.0"
},
{
"package": "Multipart",
"reason": null,
"repositoryURL": "https://github.com/vapor/multipart.git",
"version": "2.0.0"
},
{
"package": "Node",
"reason": null,
"repositoryURL": "https://github.com/vapor/node.git",
"version": "2.0.1"
},
{
"package": "Random",
"reason": null,
"repositoryURL": "https://github.com/vapor/random.git",
"version": "1.0.0"
},
{
"package": "Routing",
"reason": null,
"repositoryURL": "https://github.com/vapor/routing.git",
"version": "2.0.0"
},
{
"package": "S3SignerAWS",
"reason": null,
"repositoryURL": "https://github.com/JustinM1/S3SignerAWS.git",
"version": "2.0.1"
},
{
"package": "Sockets",
"reason": null,
"repositoryURL": "https://github.com/vapor/sockets.git",
"version": "2.0.1"
},
{
"package": "TLS",
"reason": null,
"repositoryURL": "https://github.com/vapor/tls.git",
"version": "2.0.0"
},
{
"package": "Vapor",
"reason": null,
"repositoryURL": "https://github.com/vapor/vapor.git",
"version": "2.0.2"
}
],
"version": 1
}
9 changes: 3 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import PackageDescription
let package = Package(
name: "S3",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1),
.Package(url: "https://github.com/JustinM1/S3SignerAWS.git", majorVersion: 1, minor: 1),
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
.Package(url: "https://github.com/JustinM1/S3SignerAWS.git", majorVersion: 2),
.Package(url: "https://github.com/manGoweb/MimeLib.git", majorVersion: 1)
],
exclude: [

]
exclude: []
)

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Basic S3 access library for Vapor written in Swift

1) Add following package to your ```Package.swift```:
``` Swift
.Package(url: "https://github.com/manGoweb/S3.git", majorVersion: 1)
.Package(url: "https://github.com/manGoweb/S3.git", majorVersion: 2)
```

2) Run ```vapor clean```
Expand Down
26 changes: 15 additions & 11 deletions Sources/S3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class S3 {
S3 Signer class (https://github.com/JustinM1/S3SignerAWS)
*/
let signer: S3SignerAWS

let client: ClientFactoryProtocol


// MARK: Initialization
Expand All @@ -81,9 +83,9 @@ public class S3 {
guard let region = Region(rawValue: regionString) else {
throw Error.missingCredentials("region")
}
self.init(accessKey: accessKey, secretKey: secretKey, region: region)
self.init(droplet: drop, accessKey: accessKey, secretKey: secretKey, region: region)
} else {
self.init(accessKey: accessKey, secretKey: secretKey)
self.init(droplet: drop, accessKey: accessKey, secretKey: secretKey)
}

if let bucket: String = drop.config["s3", "bucket"]?.string {
Expand All @@ -103,7 +105,8 @@ public class S3 {
- bucketName: Name of the global bucket to be used for calls where bucket is not specified (optional)
- region: AWS Region, default is .usEast1_Virginia
*/
public init(accessKey: String, secretKey: String, bucketName: String?, region: Region = .usEast1_Virginia) {
public init(droplet drop: Droplet, accessKey: String, secretKey: String, bucketName: String?, region: Region = .usEast1_Virginia) {
self.client = drop.client
self.bucketName = bucketName
self.signer = S3SignerAWS(accessKey: accessKey, secretKey: secretKey, region: region)
}
Expand All @@ -116,8 +119,8 @@ public class S3 {
- secretKey: AWS Secret key
- region: AWS Region, default is .usEast1_Virginia
*/
public convenience init(accessKey: String, secretKey: String, region: Region = .usEast1_Virginia) {
self.init(accessKey: accessKey, secretKey: secretKey, bucketName: nil, region: region)
public convenience init(droplet drop: Droplet, accessKey: String, secretKey: String, region: Region = .usEast1_Virginia) {
self.init(droplet: drop, accessKey: accessKey, secretKey: secretKey, bucketName: nil, region: region)
}

// MARK: Managing objects
Expand All @@ -138,12 +141,13 @@ public class S3 {
throw Error.invalidUrl
}

let bytes: Bytes = try data.makeBytes()
let bytes = data.makeBytes()
var awsHeaders: [String: String] = headers
awsHeaders["x-amz-acl"] = accessControl.rawValue
let signingHeaders: [String: String] = try signer.authHeaderV4(httpMethod: .put, urlString: url.absoluteString, headers: awsHeaders, payload: .bytes(bytes))
let result: Response = try BasicClient.put(fileUrl!.absoluteString, headers: self.vaporHeaders(signingHeaders), query: [:], body: Body(bytes))


let result = try client.put(url.absoluteString, query: [:], vaporHeaders(signingHeaders), Body(bytes), through: [])

guard result.status == .ok else {
throw Error.badResponse(result)
}
Expand Down Expand Up @@ -208,7 +212,7 @@ public class S3 {
}

let headers: [String: String] = try signer.authHeaderV4(httpMethod: .get, urlString: url.absoluteString, headers: [:], payload: .none)
let result: Response = try BasicClient.get(fileUrl!.absoluteString, headers: self.vaporHeaders(headers))
let result: Response = try client.get(url.absoluteString, query: [:], vaporHeaders(headers))

if result.status == .notFound {
throw Error.notFound
Expand All @@ -217,7 +221,7 @@ public class S3 {
guard result.status == .ok else {
throw Error.badResponse(result)
}

guard let bytes: Bytes = result.body.bytes else {
throw Error.missingData
}
Expand All @@ -242,7 +246,7 @@ public class S3 {

let headers: [String: String] = try signer.authHeaderV4(httpMethod: .delete, urlString: url.absoluteString, headers: [:], payload: .none)

let result: Response = try BasicClient.delete(fileUrl!.absoluteString, headers: self.vaporHeaders(headers), query: [:], body: Body(""))
let result = try client.delete(url.absoluteString, query: [:], vaporHeaders(headers), Body(""))

guard result.status == .noContent || result.status == .ok else {
throw Error.badResponse(result)
Expand Down

0 comments on commit 2e03fb0

Please sign in to comment.