Download jpg to S3: "Request body unexpectedly terminated"

I have a problem with S3. After 3 hours of troubleshooting (in the meantime, I found out about IAM roles and was able to create them). I'm stuck trying to upload fb profile picture to Amazon S3.

My code is:

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
                            })
                        }

** Task ** I constantly get an error The request body terminated unexpectedlyfrom S3 that looks like this:

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}

I'm not sure what is going wrong, I was debugging and fileName, fileURL, imageData seem to be good

+4
source share
2 answers

There is a bug with 2.5.1 SDK, I talk a little about it here .

, AWSSignature ...

:

1) 2.5.0, , : (Edit: , - 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) , , Amazon . , , 783-785 AWSCore/Authentication/AWSSignature.m - , , , .

if (self.endOfStream) {
    return NO;
}
+7

, SDK AWSS3. cocoapods, 2.5.0 (Swift 3 ) :

pod 'AWSS3', '2.5.0'
0

Source: https://habr.com/ru/post/1670411/


All Articles