【问题标题】:S3 upload from iOS Swift - AWSS3TransferUtility not uploading从 iOS Swift 上传 S3 - AWSS3TransferUtility 未上传
【发布时间】:2021-03-17 13:58:26
【问题描述】:

各位开发者,

我需要在 S3 中以 cognito-unauthenticated + cognito 身份验证状态上传。

(上传应忽略 cognito 凭据,但可以在两种状态下发生)

我有一个 cognito 身份池,并为其创建了未经授权的角色。 我也有一个认知用户池。

我有一个专用的桶。多次尝试swift代码+S3策略+角色策略,还是不行。

斯威夫特:

    class func uploadfile(fileURL: URL, s3Key: String)
    {
        let credentialsProvider = AWSStaticCredentialsProvider(accessKey: awsAccessKey, secretKey: awsSecretKey)
        //let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.EUCentral1, identityPoolId:"cognito_identity_pool_id")
        
        let configuration = AWSServiceConfiguration(region:.EUCentral1, credentialsProvider:credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration
        
        let tuConf = AWSS3TransferUtilityConfiguration()
        tuConf.isAccelerateModeEnabled = false
        
        AWSS3TransferUtility.register(
            with: configuration!,
            transferUtilityConfiguration: tuConf,
            forKey: "utility-key"
        )
        
        guard let transferUtility = AWSS3TransferUtility.s3TransferUtility(forKey: "utility-key")
        else
        {
            return
        }

        let expression = AWSS3TransferUtilityUploadExpression()
        expression.progressBlock = {(task, progress) in
            print("progress \(progress.fractionCompleted)")
        }
        
        var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
        completionHandler = { (task, error) -> Void in
            print("task finished")
        }
        
        let bucketName = "bucket name (not ARN)"
        guard let data = try? Data.init(contentsOf: fileURL)
        else
        {
            return
        }
    
        transferUtility.uploadData(data as Data, bucket: bucketName, key: s3Key, contentType: "text/plain", expression: expression,
        completionHandler: completionHandler).continueWith
        {
            (task) -> AnyObject? in
            if let error = task.error {
                print("Error: \(error.localizedDescription)")
            }
            
            if let _ = task.result {
                print ("upload successful.")
            }
            
            return nil
        }
    }

这是存储桶政策:

{
    "Version": "2012-10-17",
    "Id": "PolicyID",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "ARN for Cognito Identity pool unauthorized role"
            },
            "Action": "s3:*",
            "Resource": "ARN for bucket to upload"
        }
    ]
}

这是Cognito身份池未授权角色的角色策略(在上面的存储桶策略中提到):

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

注意: 我想限制上述角色策略(因为这是访客未经授权上传)而不是完全访问权限,但即使完全访问权限上传也不成功。

我已经尝试过的:

  • 上面的 Swift 代码有我已经尝试过但失败的 cmets
  • 反复调用uploadfile(),我只得到“上传成功”。但不是“任务完成”或“进度”消息。第一次,我收到了所有 3 条消息。
  • 我已清除 Keychain DB 的缓存(用于模拟器) - 只是为了在 AWS 端更改后刷新凭证。没有结果。

感谢您的宝贵时间!

【问题讨论】:

    标签: swift amazon-s3 aws-sdk amazon-cognito awss3transferutility


    【解决方案1】:

    我通过以下解决方案解决了这个问题:

    存储桶政策:

    {
        "Version": "2012-10-17",
        "Id": "PolicyID",
        "Statement": [
            {
                "Sid": "StatementID",
                "Effect": "Allow",
                "Principal": {
                    "AWS": "bucket_arn"
                },
                "Action": [
                    "s3:GetObject",
                    "s3:PutObject"
                ],
                "Resource": "bucket_arn/*"
            }
        ]
    }
    

    与未经身份验证的 Cognito 角色附加的策略:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor1",
                "Effect": "Allow",
                "Action": "s3:PutObject",
                "Resource": "bucket_arn"
            }
        ]
    }
    

    iOS 代码更改(添加以下语句):

    tuConf.bucket = "bucket_name (not ARN, not full bucket URL, just name)"
    

    【讨论】:

      猜你喜欢
      • 2012-01-05
      • 2018-09-29
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多