Swift ios尝试将文件上传到s3但文件永远不会上传

时间:2017-03-22 17:18:05

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

我正尝试使用以下方法将文件上传到S3:

func uploadImage(with data: Data) {
        let expression = AWSS3TransferUtilityUploadExpression()
        expression.progressBlock = progressBlock

        transferUtility.uploadData(
            data,
            bucket: "MyBucket",
            key: "img.jpg",
            contentType: "image/jpeg",
            expression: expression,
            completionHandler: completionHandler).continueWith { (task) -> AnyObject! in
                if let error = task.error {
                    print("Error: \(error.localizedDescription)")
                    self.statusLabel.text = "Failed"
                }

                if let _ = task.result {
                    self.statusLabel.text = "Generating Upload File"
                    print("Upload Starting!")
                    // Do something with uploadTask.
                }

                return nil;
        }
    }


self.uploadImage(with: imageData)

我在我的控制台中看到了这一点:上传开始!但该文件永远不会上传

Bucket权限:

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::myBucket/*"
        }
    ]
}

oneClick_Cognito_MyUserUnauth_Role_xxxxxx政策:

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

我收到错误:Domain=com.amazonaws.AWSS3TransferUtilityErrorDomain Code=2

2 个答案:

答案 0 :(得分:1)

问题是我的Cognito用户在我们这个地区,而我的存储桶是在欧盟。

答案 1 :(得分:0)

在您的存储桶权限中,尝试添加s3:PutObject

的权限