我正面临 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
})
}
**问题** 我不断收到The request body terminated unexpectedly
来自 S3 的错误,如下所示:
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 似乎都很好