我正面临S3的问题。 经过3小时的故障排除(同时我了解了IAM角色并设法创建它们),我试图将fb个人资料图片上传到亚马逊S3。
我的代码:
if let imageData = NSData(contentsOf: NSURL(string: url) as! URL) {
let fileName = ProcessInfo.processInfo.globallyUniqueString + ".jpg"
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
let image = UIImage(data: imageData as Data)
let imageData = UIImageJPEGRepresentation(image!, 1.0)!
do {
try imageData.write(to: fileURL! as URL)
} catch _ {
self.log.error("Could not write to file.")
}
let transferManager = AWSS3TransferManager.default()
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest?.bucket = "app-files"
uploadRequest?.key = "user-data/" + awsId! + "_primary_profile_picture.jpg"
uploadRequest?.body = fileURL!
transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as? NSError {
if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
switch code {
case .cancelled, .paused:
break
default:
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
} else {
print("Error uploading: \(uploadRequest?.key) Error: \(error)")
}
return nil
}
let uploadOutput = task.result
print("Upload complete for: \(uploadRequest?.key)")
return nil
})
}
** **问题
我不断从S3获得The request body terminated unexpectedly
错误,如下所示:
Error uploading: Optional("user-data/eu-west-1:xxxx-xxxx-xxxx-xxxx-xxxxxxxxxx_primary_profile_picture.jpg")
Error: Error Domain=com.amazonaws.AWSS3ErrorDomain Code=0 "(null)"
UserInfo={HostId=XXX,
Message=The request body terminated unexpectedly,
Code=IncompleteBody,
RequestId=1485A0FFBD7819D7}
我不确定出了什么问题,我已经调试了,fileName,fileURL,imageData似乎没问题了
答案 0 :(得分:7)
2.5.1 SDK存在错误,我对其进行了一些解释here。
基本上,AWSSignature为上传创建了错误的签名......
你有两种方法来解决它:
1)通过明确声明你需要的所有pod来恢复使用2.5.0 :(编辑:我注意到你因为SWIFT问题而无法做到这一点。可能尝试选项2)
pod 'AWSCore', '2.5.0'
pod 'AWSCognito', '2.5.0'
pod 'AWSLambda', '2.5.0'
pod 'AWSSNS', '2.5.0'
pod 'AWSS3', '2.5.0'
2)自己更改代码以解决问题,直到亚马逊修复它。您需要做的就是在AWSCore / Authentication / AWSSignature.m文件中注释掉第783-785行 - 如果您尝试,您应该收到一条文件被锁定的消息,只需将其解锁即可。
if (self.endOfStream) {
return NO;
}
答案 1 :(得分:0)
确实当前的AWSS3 SDK存在错误。如果您使用的是cocoapods,您可以安装2.5.0(兼容Swift 3):
pod 'AWSS3', '2.5.0'