Skip to content

Instantly share code, notes, and snippets.

@csexton
Created April 28, 2016 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csexton/f5c6e5f099273b3bd62351e22d86ca46 to your computer and use it in GitHub Desktop.
Save csexton/f5c6e5f099273b3bd62351e22d86ca46 to your computer and use it in GitHub Desktop.
Rough method used for testing if S3 credentials work
// In reply to this comment:
// http://www.codeography.com/2016/03/20/signing-aws-api-requests-in-swift.html#comment-2647137142
func test() -> String {
let bodyDigest = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
let url = NSURL(string: "http://\(bucketName!).s3.amazonaws.com")!
let signer = S3V4Signer(accessKey: accessKey!, secretKey: secretKey!, regionName: regionName!)
let headers = signer.signedHeaders(url, bodyDigest: bodyDigest, httpMethod: "GET")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
do {
var response: NSURLResponse?
try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
print(response?.description)
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
return "Success!"
} else {
return "Failed with HTTP status code \(httpResponse.statusCode)"
}
}
} catch (let e) {
return "Failed with error \(e)"
}
return "Error Connecting to \(bucketName)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment