简体   繁体   English

(iOS) AWS S3 上传失败且没有错误(使用联合身份验证的用户 - Apple SSO)

[英](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.尽管 AWS Cognito 指示设备已登录且已获取 IdentityID,但我无法执行 S3 上传。

The storage error description is "Session expired could not fetch identity id".存储错误描述为“会话过期无法获取身份 ID”。 This is despite the identityID that was returned and passed into the s3 upload file function.尽管返回并传递到 s3 上传文件函数中的身份标识。

  1. Logs into AWS Cognito using the ASAuthorizationAppleIDCredential.identityToken使用 ASAuthorizationAppleIDCredential.identityToken 登录 AWS Cognito
  2. Also obtains the IdentityID同时获取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将数据上传到 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 设置造成的。 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. AWS Cognito 配置为“允许访问未经身份验证的用户”未选中,允许基本(经典)流选中,Apple 服务 ID 应为捆绑 ID,角色选择默认值,禁用属性。

This was done using the AWS Amplify Escape Hatch to AWS Mobile Client SDK with the AWSMobileClient.federatedSignIn这是使用 AWS Amplify Escape Hatch to AWS Mobile Client SDK 和 AWSMobileClient.federatedSignIn 完成的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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