XCode Swift AWS S3上传不起作用

时间:2017-02-23 06:15:58

标签: swift xcode amazon-web-services amazon-s3 upload

出于某种原因,上传到S3确实不起作用,而我能够让下载功能正常工作。

这是我的上传代码:

@IBAction func uploadFile(_ sender:UIButton){

    let CognitoRegionType = AWSRegionType.USWest2  // e.g. AWSRegionType.USEast1
    let CognitoIdentityPoolId = "us-west-2:3c00122a-866c-4ce4-9dd3-ee23c16e58f3"

    let DefaultServiceRegionType = AWSRegionType.USWest1 // e.g. AWSRegionType.USEast1
    let S3BucketName = "snappcastphotos"


    let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType, identityPoolId: CognitoIdentityPoolId)
    let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType , credentialsProvider:credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration


    let uploadRequest = AWSS3TransferManagerUploadRequest()
    uploadRequest?.bucket = S3BucketName
    uploadRequest?.key = "bingo"
    uploadRequest?.body = URL(fileURLWithPath: "/users/rhom/desktop/test.rtf")

    let transferManager = AWSS3TransferManager.default()
    transferManager.upload(uploadRequest!).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

        if let error = task.error as? NSError {
            if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
                switch code {
                case .cancelled, .paused:
                    break
                default:
                    print("Error uploading: \(uploadRequest?.key) Error: \(error)")
                }
            } else {
                print("Error uploading: \(uploadRequest?.key) Error: \(error)")
            }
            return nil
        }

        let uploadOutput = task.result
        print("Upload complete for: \(uploadRequest?.key)")
        return nil
    })
}

并且此代码产生以下错误/输出:

上传错误:可选(“bingo”)错误:错误域= com.amazonaws.AWSS3ErrorDomain代码= 0“(null)”UserInfo = {HostId = KAsbvIqiY67dr / 64f3uvZPB1Lr5Vj7eNNV198DLai / RG / tA + v3To8CBbnbFSM00V2COZnzebx / M =,Message =请求正文意外终止,Code = IncompleteBody,RequestId = A95236E53A1B8FE8}

有人有什么想法吗?我找不到任何线索,为什么下载使用Cognito和S3工作,但上传不起作用....

2 个答案:

答案 0 :(得分:0)

它看起来更像是授权错误。检查您是否拥有此特定存储桶的写入权限,并检查您提供的路径是否正确,就好像存在不匹配一样。

答案 1 :(得分:0)

我终于解决了这个问题:)

事实证明我已经安装了旧版本的AWS Frameworks,因为我正在使用较旧的教程开始...并且已经使用Cocoapods在IOS 8.0的podfile中指定了框架,但我现在是IOS 10.0! !所以将podfile更改为10.0版就解决了这个问题。

我最终使用Cocoapods重新安装了AWS Frameworks,并使用10.0重新安装了正确的podfile ...但我认为有一种方法可以更新pod,但我会稍后再研究

希望这可以帮助任何有这个问题的人:)