由于多种原因,我无法使用 AWS 开发工具包,不得不对 API 进行休息调用。我已经弄清楚了身份验证,但需要了解要调用哪些资源。大多数 AWS 文档都指向他们的开发工具包。我怎样才能确定休息调用,比如 AWS 密钥管理 (KMS)?
2 回答
请参阅此处的 AWS KMS 操作文档:http:
//docs.aws.amazon.com/kms/latest/APIReference/API_Operations.html
所有服务的 AWS 端点列表:http:
//docs.aws.amazon.com/general/latest/gr/rande.html
例如,us-east 中的 KMS 为 kms.us-east-1.amazonaws.com
有关对 AWS 端点的 HTTPS 请求以及如何签署请求的示例:http:
//docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
因此,KMS ListAliases 的基本 URL 将是(在签名之前):
https ://kms.us-east-1.amazonaws.com/?Action=ListAliases&Version=2010-05-08
这是一个通过 swift 4 for ios 中的 restful 命令对 AWS amazon Web 服务执行 PUT 对象的示例。我在互联网上的任何地方都找不到这个,所以请尽情享受。我不得不自己拼凑起来。我的存储桶当前设置为公共读/写。我认为添加用户名/密码(访问密钥 ID 和秘密访问密钥)将通过参数完成。这个 restRequest 函数有一个字典参数,可以在其中添加它。但是通过 Postman 尝试相同的写入,我认为亚马逊网络服务实际上期望它作为一个名为“授权”的组合标头。我不确定它到底是如何工作的,但 Postman 确实将 AWS 作为登录类型,所以去那里进行实验。我从堆栈溢出某处的宁静示例中获得了我的 restRequest 宁静函数。
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)")
}