当前位置 : 主页 > 手机开发 > ios >

iOS – 同步上传到AWS S3

来源:互联网 收集:自由互联 发布时间:2021-06-11
所有aws-sdk-ios示例都使用AWSS3TransferManager异步上传数据. 例如:https://github.com/awslabs/aws-sdk-ios-samples/blob/master/S3TransferManager-Sample/Swift/S3TransferManagerSampleSwift/UploadViewController.swift#L81 现在通常
所有aws-sdk-ios示例都使用AWSS3TransferManager异步上传数据.

例如:https://github.com/awslabs/aws-sdk-ios-samples/blob/master/S3TransferManager-Sample/Swift/S3TransferManagerSampleSwift/UploadViewController.swift#L81

现在通常,这很好,但由于其他原因,我的工作已经在NSOperation子类中了,所以我想同步上传到S3以保持简单(否则,我需要实现异步NSOperation,并且更多样板……)

有人知道怎么做吗?

如果有人好奇,我想出来:

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1,
                                                            identityPoolId: AwsCognitoIdentityPoolId)
    let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1,
                                                credentialsProvider: credentialsProvider)
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

    let S3Client = AWSS3.defaultS3()
    let putObjectRequest = AWSS3PutObjectRequest()
    putObjectRequest.bucket = AwsBucketName
    putObjectRequest.key = key
    putObjectRequest.body = logFilePathURL

    do {
        let fileAttributes = try self.fileManager.attributesOfItemAtPath(logFilePathURL.path!)
        let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
        putObjectRequest.contentLength = NSNumber(longLong: fileSizeNumber.longLongValue)
    } catch _ as NSError {
        // TODO handle error
    }

    S3Client.putObject(putObjectRequest).continueWithBlock { (task: AWSTask) -> AnyObject? in
        return nil
    }.waitUntilFinished()

…是啊.我无法在任何地方找到一个例子,所以我必须阅读源代码并解决问题.至于那个aws-sdk-ios库,谈论过度工程……需要一段时间才能通过所有这些间接层来弄明白.

无论如何,在我返回nil的最后一个区块中,你处理错误等等.

网友评论