首页 > 解决方案 > AWS Cognito 身份验证 + AWS 移动客户端 + API 网关 + S3 存储桶

问题描述

我使用 AWSCognitoAuth 为我的应用程序构建了一个登录屏幕,与提供的示例之一 ( https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoAuth-Sample ) 完全相同。这很好用 - 它SFSafariViewController在用户使用常规用户名 + 密码或通过 Facebook 或 Google 登录的实例中显示登录屏幕。因此,在此身份验证之后,我获得了访问和刷新令牌,然后我可以使用这些令牌从我的 API 中获取数据,这些数据通过 AWS API Gateway 进行路由。相应的 AWS API Gateway 路由配置为使用“用户池”授权,而不是“IAM”。

但现在我需要从不公开的 S3 存储桶中下载一些文件。所以需要做的是获取临时 AWS 凭证并使用它们访问存储桶。AWSMobileClient 库和 S3 传输实用程序能够做到这一点。但我的问题是,我不知道如何告诉 AWSMobileClient 用户现在已经登录的事实。AWSMobileClient 在 appDelegate 中初始化,didFinishLaunching:withOptions如下所示:

    let serviceConfiguration = AWSServiceConfiguration(
        region: .EUWest1,
        credentialsProvider: AWSMobileClient.sharedInstance().getCredentialsProvider()

    )

    //create a pool
    let configuration = AWSCognitoIdentityUserPoolConfiguration.init(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: CognitoIdentityUserPoolAppClientSecret, poolId: CognitoIdentityUserPoolId)
    AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: configuration, forKey: AWSCognitoUserPoolsSignInProviderKey)


    AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

    AWSFacebookSignInProvider.sharedInstance().setPermissions(["public_profile", "email"])
    AWSSignInManager.sharedInstance().register(signInProvider: AWSFacebookSignInProvider.sharedInstance())

    return AWSMobileClient.sharedInstance().interceptApplication(
        application,
        didFinishLaunchingWithOptions: launchOptions)

问题是,当我在执行 AWSCognitoAuth 登录后完全关闭应用程序并重新打开它时,AWSMobileClient 会以某种方式找到会话并且正确获取凭证并加载存储桶中的文件。但是我需要在用户登录后以某种方式手动触发它,我不能强迫用户退出并重新打开应用程序,而不是在 2018 年......我现在已经调试了很长时间,并且正在研究 AWSMobileClient 框架,试图找到如何将这两者联系在一起,但没有成功。我该怎么做?

标签: iosamazon-web-servicesamazon-s3aws-sdkaws-mobilehub

解决方案


但现在我需要从不公开的 S3 存储桶中下载一些文件。所以需要做的是获取临时 AWS 凭证并使用它们访问存储桶。AWSMobileClient 库和 S3 传输实用程序能够做到这一点。但我的问题是,我不知道如何告诉 AWSMobileClient 用户现在已经登录的事实。AWSMobileClient 在 appDelegate 的 didFinishLaunching:withOptions 中初始化

你已经尝试使用了Pre-signed url吗?它旨在满足您的要求。它允许临时访问 aws 资源,在您的情况下可以访问 S3 存储桶。

您在这里有使用 iOS sdk的示例,请查看S3TransferUtility-Sample

S3TransferUtility-Sample(Swift、Objective-C)。这是一个示例移动应用程序,演示如何使用 Amazon S3 PreSigned URL Builder 在后台下载/上传文件。涉及的 AWS 服务包括:

  • 亚马逊 S3 亚马逊
  • 认知身份

希望能帮助到你!


推荐阅读