具有进度块的Amazon S3 iOS SDK后台上传 [英] Amazon S3 iOS SDK background upload with progress block

查看:359
本文介绍了具有进度块的Amazon S3 iOS SDK后台上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个依赖于AWS​​进行文件上传和下载的应用程序.在上传的情况下,当我使用AWSS3TransferManagerUploadRequest上传视频文件时,可以使用进度块显示进度栏,但是当我按下主屏幕按钮并且应用进入背景时,上传会暂停(可能是因为它使用了NSURLConnection).仅当应用程序在前台运行时,才会进行上传.

I am working on an app that depends on AWS for file uploads and downloads. In the case of uploads, when I upload video files using AWSS3TransferManagerUploadRequest, I can show progress bar using progress block, but when I press home button and app enters background, the upload pauses (probably because it uses NSURLConnection). Only if the app is running in foreground will upload take place.

这是使用AWSS3TransferManagerUploadRequest上传文件的代码.

This is the code for uploading files using AWSS3TransferManagerUploadRequest.

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];

uploadRequest.bucket = bucketName;
uploadRequest.key = appropriateKey;

uploadRequest.body = [NSURL fileURLWithPath:filePath];

uploadRequest.contentType = contentType;

uploadRequest.uploadProgress = progressBlock;


[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
    if (task.error)
    {
        if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain])
        {
            switch (task.error.code)
            {
                case AWSS3TransferManagerErrorCancelled:
                case AWSS3TransferManagerErrorPaused:
                    break;

                default:
                    failBlock(task.error.code, task.error.localizedDescription);
                    break;
            }
        }
        else
        {
            failBlock(task.error.code, task.error.localizedDescription);
        }
    }
    if (task.result)
    {
        AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
        successBlock(uploadOutput);
    }
    return nil;
}];

此功能有效,但后台传输除外.

This works, except for transfer in background.

因此,我继续使用AWS的预签名URL.以下是相同的代码.

So, I went ahead and used the AWS's pre-signed URLs. The following is the code for the same.

AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
getPreSignedURLRequest.bucket = bucketName;

getPreSignedURLRequest.key = appropriateKey;
getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodPUT;
getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];


//Important: set contentType for a PUT request.
NSString *fileContentTypeStr = contentType;
getPreSignedURLRequest.contentType = fileContentTypeStr;
[[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:getPreSignedURLRequest]
 continueWithBlock:^id(AWSTask *task) {

     if (task.error) {
         NSLog(@"Error: %@",task.error);
     } else {

         NSURL *presignedURL = task.result;
         NSLog(@"upload presignedURL is: \n%@", presignedURL);

         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:presignedURL];
         request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
         [request setHTTPMethod:@"PUT"];
         [request setValue:fileContentTypeStr forHTTPHeaderField:@"Content-Type"];


         NSURLSession *session;
         NSURL *uploadFileUrl = [NSURL fileURLWithPath:filePath];
         NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:uploadFileUrl];
         //uploadTask is an instance of NSURLSessionDownloadTask.
         //session is an instance of NSURLSession.
         [uploadTask resume];

     }

     return nil;
 }];

这解决了后台上传的问题.但是它没有进度块,因此我无法跟踪进度.

This solves the problem of uploading in the background. But it does not have a progress block and because of that I cannot track progress.

我必须能够跟踪进度并从后台上传.这可能吗?还是有任何解决方法?

I have to be able to track progress and upload from background. Is this possible? Or is there any workaround?

推荐答案

因此,我进行了很多挖掘,并找到了答案.对某人可能会有帮助.

So, I did a lot of digging around and found an answer for this. It may be helpful for someone.

AWSS3TransferManager具有进度块以跟踪进度,但不支持后台传输.而且,AWSS3GetPreSignedURLRequest支持后台传输,但无法跟踪进度.

The AWSS3TransferManager has progress block to track progress but no support for background transfer. And AWSS3GetPreSignedURLRequest has support for background transfer but no way to track progress.

他们还有另一项服务,您可以通过该服务在后台跟踪进度以及上载/下载.使用AWSS3TransferUtility,您可以完成这两项操作.请注意,它仍处于测试阶段.

They have another service via which you can track progress and upload/download in the background. Using AWSS3TransferUtility, you can do both these things. Do note that this is still in beta.

有关更多信息,请参考文档- http://docs.aws .amazon.com/mobile/sdkforios/developerguide/s3transferutility.html

Refer the docs for more info - http://docs.aws.amazon.com/mobile/sdkforios/developerguide/s3transferutility.html

对此我有很多麻烦(部分是由于我的无知).我希望这对遇到类似问题的人有用.

I had a lot of trouble with this(partly due to my ignorance). I hope this is useful for anyone with similar problems.

这篇关于具有进度块的Amazon S3 iOS SDK后台上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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