AWSS3TransferUtilityErrorDomain Code=2 on iOS Swift While trying to upload Image/pdf

Viewed 1066

I am trying to upload pdf file or image file to AWSS3 bucket but I am getting AWSS3TransferUtilityErrorDomain Code=2 error. Please note I already have checked region and it is correct. Also I have verified that I am using correct accessKey and secretKey I also have visited below mentioned links with no luck:

My code to upload file is below:

let credentials = AWSStaticCredentialsProvider(accessKey: “accessKey” , secretKey: “secretKey”)
let configuration = AWSServiceConfiguration(region: AWSRegionType.APSouth1 , credentialsProvider: credentials)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let expression = AWSS3TransferUtilityUploadExpression()
    expression.progressBlock = { (task, progress) in
       DispatchQueue.main.async(execute: {
            // Update a progress bar
             print("Task: \(task)")
             print("Progress: \(progress)")
        })
   }
 var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
 completionHandler = { (task, error) -> Void in
    DispatchQueue.main.async(execute: {
       if let error = error {
          CommonLoader.hide()
          SCLAlertView().showError("Error", subTitle: error.localizedDescription)
          return
       }
                // Do stuff after success
    })
 }
let transferUtility = AWSS3TransferUtility.default()
// contentType —-> “image/jpeg” for images && “application/pdf” for pdf files
transferUtility.uploadData(data, bucket: s3BucketName, key: remoteName, contentType: contentType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> Any? in
            if let error = task.error {
                // error case
            }
            else {
                if !task.isFaulted && task.result != nil {
                    // success case
                }
            }
            
            return nil
            
}
1 Answers

After a lot of search and reading documentation I am able to solve this issue. In my case there were two strange things, one with same credentials and bucket on Android it was working. But on iOS exactly same code was working in Dubai but not in Pakistan. I solved the issue by just adding the region to project info.plist file as mentioned below:

  • Please note in my case region was ap-south-1 but you need to put it here yours, you can check region from Amazon S3 Endpoints and then find corresponding region value to use in your info.plist. Hope this will help someone and save time. Happy coding. cheers!
<key>S3TransferUtility</key>
<dict>
   <key>Default</key>
   <dict>
        <key>Region</key>
        <string>"ap-south-1"</string>
    </dict>
</dict>
Related