简体   繁体   中英

(iOS) AWS S3 Upload Fails with No Error (User Authenticated Using Federated Identities - Apple SSO)

I'm not able to do a S3 Upload despite AWS Cognito indicating that the device is signedIn and the IdentityID being obtained.

The storage error description is "Session expired could not fetch identity id". This is despite the identityID that was returned and passed into the s3 upload file function.

  1. Logs into AWS Cognito using the ASAuthorizationAppleIDCredential.identityToken
  2. Also obtains the IdentityID
    func SignIn() {

       awsmobileclient.federatedSignIn(providerName: IdentityProvider.apple.rawValue,
                                            token: identityToken) { (userState, error) in
                    if let error = error {
                        print("Error in federatedSignIn: \(error)")
                        return
                    }

                    guard let userState = userState else {
                        print("userState unexpectedly nil")
                        return
                    }
                print("federatedSignIn successful: \(userState.rawValue)")
                sleep(5)
                
                // Retrieve your Amazon Cognito ID
                let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .CACentral1, identityPoolId: "ca-central-1:3e8d12d5-9739-4934-8eb0-df6bec232d77")
                let configuration = AWSServiceConfiguration(region: .CACentral1, credentialsProvider: credentialsProvider)
                AWSServiceManager.default().defaultServiceConfiguration = configuration
                
                credentialsProvider.getIdentityId().continueWith(block: { (task) -> AnyObject? in
                    if (task.error != nil) {
                        print("Error: " + task.error!.localizedDescription)
                        
                    }
                    else {
                        // the task result will contain the identity id
                        let cognitoId = task.result!
                        print("Cognito id: \(cognitoId)")
                        UserDefaults.standard.set(cognitoId, forKey: "cognitoId")
                    }
                    return task;
                })

    }

  1. Uploads Data to S3

    func uploadData(key: String, data: Data) {
        
        var progressSink: AnyCancellable?
        var resultSink: AnyCancellable?
        
        

        let options = StorageUploadDataRequest.Options(accessLevel: .private, targetIdentityId: UserDefaults.standard.string(forKey: "cognitoId"), contentType: "image/jpeg")
        let storageOperation = Amplify.Storage.uploadData(key: key, data: data, options: options)
        progressSink = storageOperation.progressPublisher.sink { progress in print("Progress: \(progress)") }
        resultSink = storageOperation.resultPublisher.sink {
            if case let .failure(storageError) = $0 {
                print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
            }
        }
        receiveValue: { data in
            print("Completed: \(data)")
        }
    }

Turns out it was likely due to AWS Cognito settings. AWS Cognito configured as, "enable access to unauthenticated users" unchecked, Allow Basic (Classic) Flow checked, Apple Services ID should be Bundle ID, Role Selection Default, Attributes Disabled.

This was done using the AWS Amplify Escape Hatch to AWS Mobile Client SDK with the AWSMobileClient.federatedSignIn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM