【问题标题】:AWS Cognito Authentication + AWS Mobile Client + API Gateway + S3 BucketAWS Cognito 身份验证 + AWS 移动客户端 + API 网关 + S3 存储桶
【发布时间】:2018-11-26 20:40:10
【问题描述】:

我使用 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 框架,试图找到如何将这两者联系在一起,但没有成功。我该怎么做?

【问题讨论】:

标签: ios amazon-web-services amazon-s3 aws-sdk aws-mobilehub


【解决方案1】:

但现在我需要从 S3 存储桶下载一些文件,这不是 上市。所以需要做的是获取临时 AWS 凭证 并使用它们访问存储桶。 AWSMobileClient 库一起 使用 S3 Transfer Utility 可以做到这一点。但我的问题是,我 不知道如何告诉 AWSMobileClient 这个事实,即 用户现在已登录。AWSMobileClient 在 appDelegate 的 didFinishLaunching:withOptions

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

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

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

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

希望对你有帮助!

【讨论】:

  • 感谢您的回答!我正在使用transferUtility.downloadData(),它使用预签名的url,就像在示例项目中所做的那样(就像在这个类中一样:github.com/awslabs/aws-sdk-ios-samples/blob/master/…)。这在我登录后重新启动应用程序时也有效,但不是在使用 AWS Cognito SDK 登录后直接启动。
猜你喜欢
  • 2018-06-03
  • 1970-01-01
  • 2018-05-22
  • 2021-05-05
  • 2022-01-01
  • 2018-01-09
  • 2017-01-02
  • 2016-01-09
  • 2018-01-29
相关资源
最近更新 更多