代码之家  ›  专栏  ›  技术社区  ›  LeNI

AWS:developeraAuthenticatedIdentity提供程序未启动IOS Swift

  •  4
  • LeNI  · 技术社区  · 7 年前

    我正在尝试集成S3上传来上传视频文件,并尝试了开发人员身份验证的方法。所有配置均按照 aws docs 说。

    developeraAuthenticatedIdentityProvider类:

    class DeveloperAuthenticatedIdentityProvider : AWSCognitoCredentialsProviderHelper {
        override func token() -> AWSTask<NSString> {
        //return AWSTask //with token and will set identityId
    }
    

    然后

    let devAuth = DeveloperAuthenticatedIdentityProvider(regionType: COGNITO_REGION, identityPoolId: COGNITO_POOL_ID, useEnhancedFlow: true, identityProviderManager:nil)
    let credentialsProvider = 
    AWSCognitoCredentialsProvider(regionType: COGNITO_REGION, identityProvider:devAuth)
    
    let configuration = 
    AWSServiceConfiguration(region: S3_REGION, credentialsProvider:credentialsProvider)
    
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    

    配置这些内容后,尝试使用 AWSS3转让管理人

    let transferManager = AWSS3TransferManager.default()
    
    let uploadingFileURL = URL(fileURLWithPath: "your/file/path/myTestFile.txt")
    
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    
    uploadRequest.bucket = "myBucket"
    uploadRequest.key = "myTestFile.txt"
    uploadRequest.body = uploadingFileURL
    transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
    
        if let error = task.error as? NSError {
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                switch code {
                case .cancelled, .paused:
                    break
                default:
                    print("Error uploading: \(uploadRequest.key) Error: \(error)")
                }
            } else {
                print("Error uploading: \(uploadRequest.key) Error: \(error)")
            }
            return nil
        }
    
        let uploadOutput = task.result
        print("Upload complete for: \(uploadRequest.key)")
        return nil
    })
    

    每当我调用Upload方法时,它都会显示

    [错误域=com.amazonaws.AWSCognitoIdentityErrorDomain代码=8 “(null)”UserInfo={\uu type=NotAuthorizedException, 消息=此标识不支持未经身份验证的访问 池。}]

    而且 开发人员身份验证提供程序 没有被解雇

    请帮忙。

    1 回复  |  直到 4 年前
        1
  •  6
  •   manukv    7 年前

    当您为cognito身份提供者使用开发人员身份验证的身份时,您不需要使用 AWSS3转让管理人。默认值() 您需要注册 AWS服务配置 AWSS3转让管理人 用钥匙。

    AWSS3转让管理人。注册(使用:configuration!,forKey: “密钥”)

    请尝试以下方法:

    let devAuth = DeveloperAuthenticatedIdentityProvider(regionType:  COGNITO_REGION, identityPoolId: COGNITO_POOL_ID, useEnhancedFlow: true, identityProviderManager:nil)
        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: COGNITO_REGION, identityProvider:devAuth)
        let configuration = AWSServiceConfiguration(region: S3_REGION, credentialsProvider:credentialsProvider)
    
        AWSS3TransferManager.register(with: configuration!, forKey: "YOUR_KEY")
    
    //Start Upload
    
    let uploadRequest = AWSS3TransferManagerUploadRequest()
    
     //Set all properties to uploadRequest
    
        AWSS3TransferManager.s3TransferManager(forKey: "YOUR_KEY").upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
                    // Do something with the response
    
                    if task.isCancelled {
                        print("Cancelled Upload")
    
                    }
                    else if (task.error != nil) {
    
                        print("Upload error --> \(task.error)")
                    }else{
    
                        print("Upload success!!! Be happy :)")
    
                    }
                    return task
                })
    

    试试看,我想可能有用。