【问题标题】:How to make my iOS app secure work with AWS S3如何使用 AWS S3 使我的 iOS 应用程序安全工作
【发布时间】:2016-02-24 04:41:27
【问题描述】:

我真的需要帮助,因为我整天都在战斗并且没有结果。我在 AWS S3 上有我想通过 url 获取的文件。我有读取这些文件的 iOS 应用程序,一切正常。 但我不会保护我的文件,所以增加安全性,在这之后我遇到了很多麻烦。 所以,我做了什么:

我添加了存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "Policy1448129771756",
    "Statement": [
        {
            "Sid": "Stmt1448129767926",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::646483417075:role/Cognito_MyAppAuth_Role"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my.files.info/*"
        }
    ]
}

我创建了 Cognito 身份池,它为我生成了代码:

然后初始化凭证提供者:

// Initialize the Amazon Cognito credentials provider  

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
initWithRegionType:AWSRegionUSEast1
identityPoolId:@"us-east-1:6f261e92-dca3-xxxxx-xxxxxxx-xxxxxxxxx"];

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

把我写在 Swift 上,所以我重写了这段代码:

let awsCognitoCredentialsProvider: AWSCognitoCredentialsProvider
let configuration: AWSServiceConfiguration

self.awsCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1,
    identityPoolId: "us-east-1:6f261e92-dca3-xxxxx-xxxxx-xxxxxxxxxxxx")

self.configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: self.awsCognitoCredentialsProvider)
            AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = self.configuration

它可以正常工作,但我在 AWS Cognito 控制台中看不到某些身份。我发现了很多代码示例,但都像我的一样。谁能帮帮我!

【问题讨论】:

    标签: ios swift amazon-web-services amazon-s3 amazon-cognito


    【解决方案1】:

    您是否使用 Cognito 进行过 API 调用?除非您这样做,否则它不会创建 id/get 凭据。请参阅Cognito docs 中有关获取 Cognito id 的部分。这将创建 id 或给你一个错误。

    【讨论】:

    • 我试图调用 credentialsProvider.getIdentityId 但它总是返回 will (((
    • 对不起,返回的是什么?
    • 调用 getidentityid 或 refresh 应该创建一个 id 或给出一个错误。如果它没有创建一个 id,你能提供你得到的错误吗?
    • 嗯,它应该给出一个错误或一个id。您正在使用 getIdentityId,而不仅仅是访问 identityId?如果没有设置,identityId 将给出 nil。你能显示你用来调用它的代码吗?这将有助于缩小范围。您是否尝试过刷新,我的其他建议?
    • 谢谢你!我解决了我的问题。我以前只做 self.awsCognitoCredentialsProvider.getIdentityId( 当我把它改成 var myId = self.awsCognitoCredentialsProvider.getIdentityId() 一切都修复了。
    猜你喜欢
    • 2021-01-10
    • 2011-04-29
    • 2021-08-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2011-07-04
    • 2019-10-02
    • 2012-07-08
    相关资源
    最近更新 更多