jjzjj

ios - 带有进度 block 的 Amazon S3 iOS SDK 后台上传

coder 2023-09-28 原文

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

这是使用 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;
}];

这有效,除了在后台传输。

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

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;
 }];

这样就解决了后台上传的问题。但它没有进度 block ,因此我无法跟踪进度。

我必须能够跟踪进度并从后台上传。这可能吗?或者有什么解决方法吗?

最佳答案

因此,我进行了大量挖掘并找到了答案。这可能对某人有帮助。

AWSS3TransferManager 有进度 block 来跟踪进度,但不支持后台传输。 AWSS3GetPreSignedURLRequest 支持后台传输,但无法跟踪进度。

他们有另一项服务,您可以通过该服务跟踪进度并在后台上传/下载。使用 AWSS3TransferUtility,您可以完成这两件事。请注意,这仍处于测试阶段。

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

我在这方面遇到了很多麻烦(部分原因是我的无知)。我希望这对遇到类似问题的任何人都有用。

关于ios - 带有进度 block 的 Amazon S3 iOS SDK 后台上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34810979/

有关ios - 带有进度 block 的 Amazon S3 iOS SDK 后台上传的更多相关文章

  1. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  2. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby - 在匿名 block 中产生 - 2

    我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - Ruby 中的单 block AES 解密 - 2

    我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec

  8. ruby - 如何在 ruby​​ 中运行后台线程? - 2

    我是ruby​​的新手,我认为重新构建一个我用C#编写的简单聊天程序是个好主意。我正在使用Ruby2.0.0MRI(Matz的Ruby实现)。问题是我想在服务器运行时为简单的服务器命令提供I/O。这是从示例中获取的服务器。我添加了使用gets()获取输入的命令方法。我希望此方法在后台作为线程运行,但该线程正在阻塞另一个线程。require'socket'#Getsocketsfromstdlibserver=TCPServer.open(2000)#Sockettolistenonport2000defcommandsx=1whilex==1exitProgram=gets.chomp

  9. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

  10. ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出 - 2

    我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e

随机推荐