【问题标题】:iOS – Upload to AWS S3 synchronouslyiOS – 同步上传到 AWS S3
【发布时间】:2016-02-03 19:52:48
【问题描述】:

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

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

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

有人知道怎么做吗?

【问题讨论】:

    标签: ios amazon-s3 aws-sdk


    【解决方案1】:

    如果有人好奇,我想通了:

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

    无论如何,在最后一个块中,我 return nil 是您处理错误等的地方。

    【讨论】:

    • 在浪费了很多时间之后,这实际上是我需要做的!非常简单的答案。
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2018-09-29
    相关资源
    最近更新 更多