简体   繁体   English

使用 AWS SDK 2 和 Cognito 将文件放在 S3 上,供使用 iOS SDK 2 的未授权用户使用

[英]Put file on S3 with AWS SDK 2 & Cognito for unauth users using iOS SDK 2

I want to upload a file to one of my S3 buckets.我想将文件上传到我的 S3 存储桶之一。

In my app I have:在我的应用程序中,我有:

In my app delegate在我的应用程序委托中

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "us-east-1:05da3124-9aab-abd9-081231a31")
        let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

An Upload function上传功能

func uploadFile(fileURL: NSURL, type: MediaType) {
    var uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest.body = fileURL
    uploadRequest.key = fileURL.lastPathComponent
    uploadRequest.bucket = "xxx.xxx.dev"
    transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
        if let error = task.error {
            log.debug("Upload failed with error: \(error)")
        } else {
            log.debug("Object \(uploadRequest.key) uploaded with \(task.result)")
            XXXRESTManager.sharedInstance.doRegisterUpload(uploadRequest.key, type: type)
        }
        return nil
    }
}

A Policy attached to my Unauthenticated role in my identity pool:附加到我的身份池中未经身份验证的角色的策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3: PutObject"
            ],
            "Resource": "arn:aws:s3:::xxx.xxx.dev"
        },
        {
            "Effect": "Allow",
            "Action": [
                "sns:CreatePlatformEndpoint",
                "sns:Subscribe"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

The connection to cognito is correct (i now have one unauthenticated user in AWS console.与 Cognito 的连接是正确的(我现在在 AWS 控制台中有一个未经身份验证的用户。

However I still get a permission denied when I try to upload a file.但是,当我尝试上传文件时,我的权限仍然被拒绝。 What did I missed?我错过了什么?

AWSS3TransferManagerUploadRequest might need more permissions than just PutObject to work. AWSS3TransferManagerUploadRequest可能需要比 PutObject 更多的权限才能工作。 Have you tried giving broader permissions for S3 on your policy?您是否尝试过在您的策略中为 S3 提供更广泛的权限? Probably it needs at least GetObject, but try first with "Action": "s3:*" so we can make sure the problem is in that part.可能它至少需要 GetObject,但首先尝试使用"Action": "s3:*"以便我们可以确保问题出在该部分。

I don't know what caused the issue, but I managed to make it work.我不知道是什么导致了这个问题,但我设法让它起作用。 The code in the question IS correct.问题中的代码是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM