ios - Amazon AWS S3 从 iOS 设备上传

标签 ios swift amazon-web-services amazon-s3 aws-sdk

我一直在尝试按照他们的文档将图像/视频上传到 s3。但是,经过大量尝试后,我一直收到拒绝访问错误。

我已经为 s3:* 访问设置了一个 UnAuth 角色的认知池。但问题是我无法让它在 iOS 设备上运行。我使用 AWSTransferManager 创建上传功能,但这是我得到的响应:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message> 
<RequestId>DEE11EF134B50C8D</RequestId><HostId>/+qV8CLvPZHi4lIAHrVnKf1sDmWFcFS1dcfMX5XG0yfPG/TIASBfo/T5aGZRZg77wdn7siOrRVc=</HostId></Error>
Upload failed with error: (The operation couldn’t be completed. (com.amazonaws.AWSServiceErrorDomain error 11.))

桶政策-:

{
    "Version": "2012-10-17",
    "Id": "Policy1460611228663",
    "Statement": [
        {
            "Sid": "Stmt1460611220793",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::MY_ACCOUNT_ID:root"
            },
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::MY_BUCKET_NAME/*"
        }
    ]
}

IAM UnAuth 角色策略-:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:*",
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

我卡在这了。请提出我做错了什么?

let accK = "myaccesskey"
let secretK = "mysecretkey"
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accK, secretKey: secretK)
let configuration = AWSServiceConfiguration(region: AwsRegion, credentialsProvider: credentialsProvider)
AWSS3TransferManager.register(with: configuration!, forKey: "mys3")

最佳答案

配置了身份池和 IAM 策略,以便身份池的 unauth 角色可以访问存储桶,以下代码应该可以工作

//Setup credentials
let credentialProvider = AWSCognitoCredentialsProvider(regionType: YOUR-IDENTITY-POOL-REGION, identityPoolId: "YOUR-IDENTITY-POOL-ID")

//Setup the service configuration
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)

//Setup the transfer utility configuration
let tuConf = AWSS3TransferUtilityConfiguration()

//Register a transfer utility object
AWSS3TransferUtility.register(
with: configuration!,
transferUtilityConfiguration: tuConf,
forKey: "transfer-utility-with-advanced-options"
)

//Look up the transfer utility object from the registry to use for your transfers.
let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: "transfer-utility-with-advanced-options")


transferUtility.uploadData(
        testData,
        bucket: "BUCKET",
        key: "KEY",
        contentType: "video/mp4",
        expression: uploadExpression,
        completionHandler: uploadCompletionHandler
        ).continueWith (block: { (task) -> AnyObject? in
            XCTAssertNil(task.error)

            return nil
    })

请查看我之前发布的链接以获取更多信息。

关于ios - Amazon AWS S3 从 iOS 设备上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49913691/

相关文章:

ios - Spritekit - 剪断绳索,魔术帽效果。计算 Sprite 的速度并以相同的速度和物体的方向射出物体

ios - 如何从 xcode 卸载 swiftlint,或关闭它的警告

ios - 在 UIPickerView 问题中选择第一行

ios - Swift UITableView 删除所选行及其上方的任何行

java - 从另一个 Lambda 函数异步启动 AWS java Lambda 函数

ios - 将 tableView 连接到 .XIB 中的数据源

ios - 试图在 UICollectionViewCells 上同时加载不同的网站,但是当我滚动时它们被复制了

swift - 升级 Xcode 后如何修复 'Cannot override a self return type with a non-self return type'

java - AWS Api Gateway - Java SDK 生成 "Internal error occurred while generating SDK"

shell - 如何获取两个 S3 存储桶之间的文件差异?