For a variety of reasons I can't use AWS SDKs and have to make rest calls to the APIs. I've figured out authentication but need to understand what resources to call. Most of the AWS documentation points to their SDKs. How can I figure out Rest Calls for, say AWS Key Management (KMS)?
See the AWS KMS actions documentation here:
http://docs.aws.amazon.com/kms/latest/APIReference/API_Operations.html
List of AWS endpoints for all services:
http://docs.aws.amazon.com/general/latest/gr/rande.html
For example, KMS in us-east is kms.us-east-1.amazonaws.com
Examples on HTTPS requests to AWS endpoints, and how to sign the request:
http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
So the base URL for KMS ListAliases would be (before signing):
https://kms.us-east-1.amazonaws.com/?Action=ListAliases&Version=2010-05-08
This is an example of doing a PUT object to AWS amazon web services via restful commands in swift 4 for ios. I could not find this anywhere on the internet, so enjoy. I had to cobble it together myself. My bucket is currently set to public read/write. I think that to add username/password (Access key ID and Secret access key) to this will be done through parameters. This restRequest function has a dictionary parameter that is where it might be added. But from experimenting with the same write via Postman, I think the amazon web service actually expects that as a combined header named "Authorization". I'm not sure exactly how that works, but Postman does have AWS as a login type, so go experiment there. I got my restRequest restful function from a restful example somewhere on stack overflow.
func restRequest(url:String, method: String, sBody: String ,
params: [String: String], completion: @escaping ([AnyObject])->() ){
if let nsURL = NSURL(string:url) {
let request = NSMutableURLRequest(url: nsURL as URL)
if method == "PUT" {
request.httpMethod = "PUT"
for thisOne in params {
request.setValue(thisOne.value, forHTTPHeaderField: thisOne.key)
}
request.httpBody = "some text in the file we are putting"
}
// Add other verbs here
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in
do {
// what happens if error is not nil?
// That means something went wrong.
// Make sure there really is some data
if let data = data {
let response = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
completion(response as! [AnyObject])
}
else {
// Data is nil.
}
} catch let error as NSError {
print("json error: \(error.localizedDescription)")
}
}
task.resume()
}
else{
// Could not make url. Is the url bad?
// You could call the completion handler (callback) here with some value indicating an error
}
}
And calling it like this:
let urlString = "https://bucketname.s3.amazonaws.com/test.txt"
restRequest(url: urlString, method: "PUT", sBody: sData, params: [ "Date" : "20180125T214827Z" ]) {
(result) in
// Handle result here.
print("restRequest result : \(result)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With