Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Rest API without SDK

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)?

like image 243
sparkFinder Avatar asked Apr 08 '16 02:04

sparkFinder


2 Answers

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

like image 100
kevin Avatar answered Nov 09 '22 07:11

kevin


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)")
        }
like image 38
Neo42 Avatar answered Nov 09 '22 06:11

Neo42