Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Amplify S3 UploadFile Error when App is in background #1259

Closed
DNCoding opened this issue Jun 3, 2021 · 2 comments
Closed

iOS Amplify S3 UploadFile Error when App is in background #1259

DNCoding opened this issue Jun 3, 2021 · 2 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists follow up Requires follow up from maintainers storage Issues related to the Storage category

Comments

@DNCoding
Copy link

DNCoding commented Jun 3, 2021

My app upload files to S3 for every 15 min. When application is in foreground, the files get uploaded properly. But when app went to background, the upload is too slow and keep throwing error. after so many retry, one file get uploaded successfully.

I keep getting any one of this 3 error

Error 1:
StorageError: Unexpected error occurred with message: Domain: [NSURLErrorDomain
Code: [-1001
LocalizedDescription: [The operation couldn’t be completed. (NSURLErrorDomain error -1001.)
LocalizedFailureReason: [
LocalizedRecoverySuggestion: [
Recovery suggestion: This should not happen. There is a possibility that there is a bug if this error persists. Please take a look at https://github.com/aws-amplify/amplify-ios/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't. Issue encountered at:
file: /.../SourcePackages/checkouts/amplify-ios/Amplify/Categories/Storage/Error/StorageError.swift
function: recoverySuggestion
line: 63

Error 2:
StorageError: Unexpected error occurred with message: Domain: [Amplify.AuthError
Code: [1
LocalizedDescription: [The operation couldn’t be completed. (Amplify.AuthError error 1.)
LocalizedFailureReason: [
LocalizedRecoverySuggestion: [
Recovery suggestion: This should not happen. There is a possibility that there is a bug if this error persists. Please take a look at https://github.com/aws-amplify/amplify-ios/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't. Issue encountered at:
file: /.../SourcePackages/checkouts/amplify-ios/Amplify/Categories/Storage/Error/StorageError.swift
function: recoverySuggestion
line: 63

Error 3:
StorageError: A network error occured while trying to fetch identity id
Recovery suggestion: Try again with exponential backoff
Caused by:
AuthError: A network error occured while trying to fetch identity id
Recovery suggestion: Try again with exponential backoff
Caused by:
network

Code:

In AmplifyMgr:
/** Sign In User using Cognito auth before uploading the file **/

  func signIn (username: String, password: String) {

    Amplify.Auth.signIn(username: username, password: password) { result in
        switch result {
        case .success(let signInResult):
            if case .done = signInResult.nextStep {
                print("\(#function): Sign in succeeded")
                self.getUserToken()
            }
            else {
                self.processSignInNextStep(username: username, nextStep: signInResult.nextStep)
            }
        case .failure(let error):
            let errMsg = "Sign in failed - \(error)"
            print("\(#function): \(errMsg)")
            self.signedInError(errorMsg: errMsg)
        }
    }
}

func uploadFile (_ fileUrl: URL) {
    Amplify.Storage.uploadFile (key: fileUrl.lastPathComponent,
                                local: fileUrl,
                                resultListener: { event in
                                    switch event {
                                    case .success(let data):
                                        print("\(#function): \(fileUrl.lastPathComponent) is uploaded - \(data)")
                                        self.uploadCompletionHandler(fileUrl)
                                    case .failure(let storageError):
                                        print("\(#function): Error uploading file \(fileUrl.lastPathComponent) - \(storageError)")
                                        self.uploadErrorHandler(fileUrl)
                                    }
                                    
                                    DispatchQueue.main.async {
                                        if (nil != self.backgroundURLCompletionHandler) {
                                            print("\(#function): Background URL Session Completion Handler Called")
                                            self.backgroundURLCompletionHandler!()
                                            self.backgroundURLCompletionHandler = nil
                                        }
                                    }
                                }
    )
    
    print("\(#function): Upload called")
}

In AppDelegate:

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    
    print ("handleEventsForBackgroundURLSession called")
    if (UserDefaults.standard.bool(forKey: AWAITING_UPLOAD_RESP)) {
        amplifyMgr.backgroundURLCompletionHandler = completionHandler
    }
    else {
        print("\(#function): Completion Handler Called")
        completionHandler()
    }
}

I have used swift package of Amplify for my project, other settings are done as per the getting started documentation.

@lawmicha lawmicha added bug Something isn't working pending-triage Issue is pending triage storage Issues related to the Storage category labels Jun 3, 2021
@lawmicha lawmicha self-assigned this Jul 9, 2021
@palpatim
Copy link
Member

palpatim commented Jul 23, 2021

Notes for @aws-amplify/ios-team :

  • This seems to be related to token expiry once the download task is handed off to iOS. In that case, this behavior is expected. Possible remedy would be to rework the way we manage URL tasks and cancel/re-submit a task once the token is expired
  • We still don't have a good story for long-running transfers. If a single transfer takes longer than 1 hour to complete, it will fail unless the customer is using static credentials to create the presigned URL. This is covered under an existing feature request.
  • We fix/enhance the error message for the error cases relating to auth token expiry for background tasks

Based on these items, the next steps are:

  • Investigate to validate the above assumptions
  • Add a new issue to improve error messaging
  • Close this issue as a dupe of the open feature request for long-running transfers

@palpatim palpatim added follow up Requires follow up from maintainers and removed pending-triage Issue is pending triage labels Jul 23, 2021
@brennanMKE brennanMKE added the duplicate This issue or pull request already exists label Aug 6, 2021
@brennanMKE
Copy link
Contributor

Closing as a duplicate.

#638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists follow up Requires follow up from maintainers storage Issues related to the Storage category
Projects
None yet
Development

No branches or pull requests

4 participants