ios - 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 框架,试图找到如何将这两者联系在一起,但没有成功。我该怎么做?
解决方案
但现在我需要从不公开的 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 亚马逊
- 认知身份
希望能帮助到你!
推荐阅读
- docker - 带有 ASP.NET Core 2.2 的 AWS CodeBuild Docker 映像
- java - 如何为使用 keyclaok rest api 的用户获取更多/可选数据?
- mysql - 错误:ER_NO_DB_ERROR:Node.js 中没有选择数据库?
- angular - 电子商务 Angular 应用程序的产品页面在浏览器视图源中查看时显示不同源代码的任何原因?
- c# - 每次按下按钮时,如何将字符串前面的 n 个字符连续移动到末尾?
- python - Python中模块的函数用法
- 3d-modelling - 3d 建模 - 如何制作复杂的 3d 模型?
- python - 如何解析执行javascript的html网页
- ocaml - OCaml:为什么在未定义变量时编译?
- python - Tensorflow / keras:“logits和labels必须具有相同的第一维”如何挤压logits或扩展标签?