没有 SDK 的 AWS Rest API
AWS Rest API without SDK
由于各种原因,我无法使用 AWS SDK,不得不对 API 进行剩余调用。我已经弄清楚了身份验证,但需要了解要调用的资源。大多数 AWS 文档都指向他们的 SDK。我如何找出 AWS Key Management (KMS) 等的 Rest Calls?
在此处查看 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 中 ios 中的 restful 命令对 AWS 亚马逊网络服务执行 PUT object 的示例。我在互联网上的任何地方都找不到这个,所以享受吧。我不得不自己拼凑起来。我的存储桶当前设置为 public read/write。我认为向其中添加 username/password(访问密钥 ID 和秘密访问密钥)将通过参数完成。这个 restRequest 函数有一个字典参数,可以在其中添加它。但是通过 Postman 尝试相同的写入,我认为亚马逊网络服务实际上期望它作为一个组合 header 命名为 "Authorization"。我不确定它到底是如何工作的,但 Postman 确实有 AWS 作为登录类型,所以去那里试验一下。我从堆栈溢出某处的 restful 示例中获得了我的 restRequest restful 函数。
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)")
}
由于各种原因,我无法使用 AWS SDK,不得不对 API 进行剩余调用。我已经弄清楚了身份验证,但需要了解要调用的资源。大多数 AWS 文档都指向他们的 SDK。我如何找出 AWS Key Management (KMS) 等的 Rest Calls?
在此处查看 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 中 ios 中的 restful 命令对 AWS 亚马逊网络服务执行 PUT object 的示例。我在互联网上的任何地方都找不到这个,所以享受吧。我不得不自己拼凑起来。我的存储桶当前设置为 public read/write。我认为向其中添加 username/password(访问密钥 ID 和秘密访问密钥)将通过参数完成。这个 restRequest 函数有一个字典参数,可以在其中添加它。但是通过 Postman 尝试相同的写入,我认为亚马逊网络服务实际上期望它作为一个组合 header 命名为 "Authorization"。我不确定它到底是如何工作的,但 Postman 确实有 AWS 作为登录类型,所以去那里试验一下。我从堆栈溢出某处的 restful 示例中获得了我的 restRequest restful 函数。
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)")
}