gpt4 book ai didi

ios - ios swift 中的亚马逊 AWS 服务器端加密

转载 作者:行者123 更新时间:2023-11-28 14:30:11 24 4
gpt4 key购买 nike

在我的应用程序中,我将用户附加的图像上传到使用服务器端加密的 S3 存储桶。

我们在 Android 中使用了以下代码来实现这一点,并且成功了。

try
{
SSECustomerKey sseCustomerKey = new SSECustomerKey(BuildConfig.S3_AES_ENCRYPT_KEY);
CognitoCachingCredentialsProvider sCredProvider = new CognitoCachingCredentialsProvider(mContext, AWSCognitoPoolId, Regions.fromName(Regions.US_EAST_1.getName()));
AmazonS3Client sS3Client = new AmazonS3Client(sCredProvider);
PutObjectRequest putRequest = new PutObjectRequest(BuildConfig.S3_BUCKET_NAME, file.getName(), file).withSSECustomerKey(sseCustomerKey);
sS3Client.putObject(putRequest);
sS3Client.setRegion(Region.getRegion(Regions.fromName(Regions.US_EAST_1.getName())));
}

但在 iOS 中,它不起作用。请找到以下 iOS 代码。

let transferManager = AWSS3TransferManager.default()

let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = bucketName
uploadRequest?.body = fileURL
uploadRequest?.key = imageName[i]
uploadRequest?.serverSideEncryption = .AES256
uploadRequest?.sseCustomerKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
uploadRequest?.contentType = "image/jpeg"

transferManager.upload(uploadRequest!).continueWith(block: { (task) -> AnyObject? in
if let error = task.error as NSError? {
if error.domain == AWSS3TransferManagerErrorDomain as String {
if let errorCode = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch (errorCode) {
case .cancelled, .paused:
DispatchQueue.main.async {
}
break;
default:
print("upload() failed: [\(error)]")
break;
}
} else {
print("upload() failed: [\(error)]")
}
} else {
print("upload() failed: [\(error)]")
}
}
return nil
})

我在 iOS 中收到以下错误

upload() failed: [Error Domain=com.amazonaws.AWSS3ErrorDomain Code=0 "(null)" UserInfo={RequestId=C7302D0F4DD27397, HostId=Dm3itGpwZNcpPq28qfFkKDlB2VFbOzIYn01T270QzzVXJ9lmZWU2bX7oPXyXrG5A86OpfTrXSHw=, Message=Server Side Encryption with Customer provided key is incompatible with the encryption method specified, ArgumentValue=AES256, Code=InvalidArgument, ArgumentName=x-amz-server-side-encryption}]

请给我一些说明

最佳答案

我建议您使用 TransferUtility 而不是 TransferManager。 TransferManager 处于弃用路径,并且不具备 TransferUtility 所具有的所有功能。这是一个代码片段,展示了如何使用服务器端加密上传文件。

 let transferUtility = AWSS3TransferUtility.default()
let uploadExpression = AWSS3TransferUtilityUploadExpression()
uploadExpression.setValue("AES256", forRequestHeader: "x-amz-server-side-encryption")

uploadExpression.progressBlock = {(task, progress) in
print("Upload progress: ", progress.fractionCompleted)
}

let uploadCompletionHandler = { (task: AWSS3TransferUtilityUploadTask, error: Error?) -> Void in
if let error = error {
//Error completing transfer. Handle Error
}
else {
//Successfully uploaded.
......
return nil
}
}

transferUtility.uploadData(
data,
bucket: "bucket",
key: "key",
contentType: "contenttype",
expression: uploadExpression,
completionHandler: uploadCompletionHandler
).continueWith (block: { (task) -> Any? in
if let error = task.error {
//Error initiating transfer. Handle error
}

return nil
})

}

这里是有关如何使用 TransferUtility 的更多信息的链接 - https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-transfer-files-with-transfer-utility.html

关于ios - ios swift 中的亚马逊 AWS 服务器端加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51324499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com