5

我正面临 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 似乎都很好

4

2 回答 2

7

2.5.1 SDK 有一个错误,我在这里解释一下。

基本上,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;
}
于 2017-03-03T20:18:33.380 回答
0

当前的 AWSS3 SDK 确实存在一个错误。如果您使用的是 cocoapods,您可以安装 2.5.0(兼容 Swift 3):

pod 'AWSS3', '2.5.0'
于 2017-03-01T05:46:13.037 回答