iOS –同步上传到AWS S3 [英] iOS – Upload to AWS S3 synchronously

查看:622
本文介绍了iOS –同步上传到AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有aws-sdk-ios示例都使用AWSS3TransferManager异步上传数据 .

All the aws-sdk-ios examples use the AWSS3TransferManager to upload data asynchronously.

例如:现在,通常,这很好,但是由于其他原因,我的工作已经在NSOperation子类中,因此我想同步上传到S3以使事情简单(否则,我需要实现异步NSOperation,并且还有很多样板...)

Now usually, this is good, but my work is already in an NSOperation subclass for other reasons, so I'd like to upload to S3 synchronously to keep things simple (otherwise, I'd need to implement an asynchronous NSOperation, and that has a lot more boilerplate...)

有人知道该怎么做吗?

推荐答案

万一有人好奇,我想通了:

In case anyone is curious, I figured it out:

    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库,谈论过度工程……花了一段时间才通过所有这些间接层来弄清楚它.

...yeah. I wasn't able to find an example anywhere, so I had to read the source and figure things out. As for that aws-sdk-ios library, talk about over-engineering... It took a while to figure it out through all those layers of indirection.

无论如何,在我return nil所在的最后一个块中,您将在其中处理错误等.

Anyways, in that final block where I return nil is where you'd handle errors, etc.

这篇关于iOS –同步上传到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆