Aws-sdk-ios: Multiple file upload to s3 'IncompleteBody Error'

Created on 11 Jul 2016  路  9Comments  路  Source: aws-amplify/aws-sdk-ios

I use by swift 2.0.

When I tried to upload multiple image by dispatch async, process always fail with below error message.(correctly only first image uploaded)

 <Error><Code>IncompleteBody</Code><Message>You did not provide the number of bytes specified by the Content-Length HTTP header</Message><NumberBytesExpected>414435</NumberBytesExpected><NumberBytesProvided>407817</NumberBytesProvided></Error>

In this case, 414435 is content-length of first request, and 407817 is content-length of second request.

question

All 9 comments

Hi Yukitoto,
Sorry that you are having trouble using the SDK. Do you have a code snipped or any steps by which we can reproduce this? Have you use AWSS3TransferUtility?

Thanks,
Rohan

@rohandubal Thanks for your reply.
This is my code snippet. Is there any problem?

        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {
            guard let currentUser = User.sharedInstance() else {
                return
            }
            let imagePath         = NSProcessInfo.processInfo().globallyUniqueString.stringByAppendingString(".png")
            let imageData: NSData = UIImageJPEGRepresentation(image, 0.3)!
            let isSuccess         = imageData.writeToFile(imagePath, atomically: true)
            if isSuccess {
                let fileUrl: NSURL      = NSURL(fileURLWithPath: imagePath)
                let date                = NSDate()
                let unixTimeStamp       = date.timeIntervalSince1970
                let unixTimeStampString = String(format:"%f", unixTimeStamp)
                let uploadedFileName    = "\(currentUser.id)_\(unixTimeStampString).jpg"

                let myTransferManagerRequest = AWSS3TransferManagerUploadRequest()
                myTransferManagerRequest.bucket = "sample"
                myTransferManagerRequest.key = uploadedFileName
                myTransferManagerRequest.body = fileUrl
                myTransferManagerRequest.ACL = AWSS3ObjectCannedACL.PublicRead

                let myTransferManager: AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager()
                myTransferManager.upload(myTransferManagerRequest).continueWithBlock({ (task) -> AnyObject! in
                    if task.error == nil && task.exception == nil {
                        let s3Path = "https://s3.amazonaws.com/sample/" + uploadedFileName
                        guard let imageURL = NSURL(string: s3Path) else {
                            return nil
                        }
                        print("#### Success!!")
                        callback(imageURL)
                    } else {
                        print("\n#### upload didn't seem to go through..\n")
                        let myError = task.error
                        print("error: \(myError)")
                        callback(nil)
                    }
                    return nil
                })
            }

Why you don't use directly uploadData(data, bucket, key, contentType, expression, completionHandler) to upload your file?

Then, you can maybe move AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager() outside the block and store the variable.

@plougy Thanks for reply!
I understand that the point is to use same AWSS3TransferManager.
However, because I am new to AWS, I counld not understand below part correct.

Why you don't use directly uploadData(data, bucket, key, contentType, expression, completionHandler) to upload your file?

Can you explain this in detail? Thanks.

First, after reading your code again, I note than you use always the same path.

let imagePath = NSHomeDirectory().stringByAppendingPathComponent("Documents.jpeg")

Then you use writeToFile to save each file. I think that is better if you send directly your NSData and after in the callback, save the files.

@plougy Nice!! It works well.
The problem is imagePath. I used same imagePath.

Thanx for your grateful support.

get the error

Message=You did not provide the number of bytes specified by the Content-Length HTTP header, in swift 3.1 when i upload images on s3 using loop

@Pinturaj Please check this post
https://stackoverflow.com/a/50856785/800313
Today I fixed also on my side!

Nothing in the post.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bradgmueller picture bradgmueller  路  5Comments

mohab2014 picture mohab2014  路  4Comments

aTylerRice picture aTylerRice  路  3Comments

ChrisInspect picture ChrisInspect  路  4Comments

aymericio picture aymericio  路  5Comments