带有KMS的AWS iOS SDK

时间:2016-04-04 07:55:30

标签: amazon-web-services encryption aws-kms

我正在尝试查找有关如何将AWS iOS SDK与KMS结合使用的示例或模式。

这里的iOS SDK文档:

https://aws.amazon.com/developers/getting-started/ios/
https://github.com/aws/aws-sdk-ios

似乎暗示了S3和EC2示例,但没有一个孤立任何KMS示例。

建议?

2 个答案:

答案 0 :(得分:1)

如果您正在使用AWS KMS CMK在AWS S3中查找服务器端加密,那么您可以指定,您需要在上传请求中为我的数据执行服务器端加密。

此处的代码用于将图像上传到AWS S3使用AWS KMS CMK进行服务器端加密。(使用swift 3编写的代码)

@IBAction func uploadButtonPressed(_ sender: AnyObject) {
    if documentImageView.image == nil {
       // Do something here
    } else {
        let image = documentImageView.image! // I picked image from my imageView named as "documentImageView". You can choose from wherever you want.
        let fileManager = FileManager.default
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("\(imageName!).jpeg")
        let imageData = UIImageJPEGRepresentation(image, 0.99)
        fileManager.createFile(atPath: path as String, contents: imageData, attributes: nil)

        let fileUrl = NSURL(fileURLWithPath: path)
        uploadRequest?.bucket = "S3BucketName"
        uploadRequest?.key = "yourImageName.jpeg"
        uploadRequest?.contentType = "image/jpeg"
        uploadRequest?.body = fileUrl as URL!
        uploadRequest?.serverSideEncryption = AWSS3ServerSideEncryption.awsKms
        uploadRequest?.ssekmsKeyId = "Your AWS KMS CMK id" 
        uploadRequest?.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
            DispatchQueue.main.async(execute: {
                self.amountUploaded = totalBytesSent. // To show the amount of data uploaded 
                self.fileSize = totalBytesExpectedToSend
            })
        }

        let transferManager = AWSS3TransferManager.default()
        transferManager?.upload(uploadRequest).continue(with: AWSExecutor.mainThread(), withSuccessBlock: { (taskk: AWSTask) -> Any? in

            if taskk.error != nil {
                // Error
            } else {
               // Handle success response
            }
            return nil
        })
    }
}

注意:如果您未在上传请求中将 AWS KMS CMK ID 提供给 ssekmsKeyId 属性,那么AWS S3将会创建一个默认的CMK ID,它对您的IAM是唯一的(如果您使用您的IAM凭据访问AWS S3)或根凭证(如果您使用您的根凭据访问AWS S3)。只有在上传请求中的 ssekmsKeyId属性中指定您的CMK ID ,才能使用此默认 CMK ID 进行进一步的加密/解密。 / p>

答案 1 :(得分:0)

也在寻找这个。他们在Javascript(浏览器,节点),Android(Java)甚至C ++上都有KMS api(可能在iOS上运行..但请)。

但不是在iOS SDK中。叹息。