由于各种原因,我不能使用AWS SDK并且需要通过REST调用API。我已经弄清楚了认证的方法,但是需要了解需要调用哪些资源。大部分AWS文档都指向他们的SDK。例如如何找到AWS密钥管理(KMS)的REST调用?
由于各种原因,我不能使用AWS SDK并且需要通过REST调用API。我已经弄清楚了认证的方法,但是需要了解需要调用哪些资源。大部分AWS文档都指向他们的SDK。例如如何找到AWS密钥管理(KMS)的REST调用?
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
}
}
并且像这样调用:
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)")
}
2010-05-08
? - falsePockets