首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Objective-C的亚马逊网络服务S3文件下载成功后失败

Objective-C的亚马逊网络服务S3文件下载成功后失败
EN

Stack Overflow用户
提问于 2015-06-12 20:11:43
回答 1查看 1K关注 0票数 0

我在使用亚马逊网络服务S3时遇到了一个奇怪的问题。第一次调用代码时,文件按预期从S3服务下载。下一次我调用代码下载其他文件时,它挂起了。

我使用的是亚马逊网络服务iOS软件开发工具包的2.1.2版本。我已经使用cocoapods下载并安装了SDK。我读过其他关于Stack Overflow的帖子,他们没有解决这个问题。

代码:

代码语言:javascript
复制
static AWSStaticCredentialsProvider *credentialsProvider;
static AWSServiceConfiguration *configuration;
static AWSS3 *transferManager;

//This code is run when the object is instantiated
-(id)init
{
    if ( self = [super init] ) {
    credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@“ACCESS KEY" secretKey:@“SECRET"];

    configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

    transferManager = [[AWSS3 alloc] initWithConfiguration:configuration];
    }

    return self;
}

-(UIImage *) getImageData:(NSString *)imageName
{

    AWSS3 *transferManager = [[AWSS3 alloc] initWithConfiguration:configuration];
    AWSS3GetObjectRequest *getImageRequest = [AWSS3GetObjectRequest new];
    getImageRequest.bucket = @"quizontapimages/quizimages";
    getImageRequest.key = imageName;
    getImageRequest.key = [getImageRequest.key stringByAppendingString:@".png"];


    BFTask *downloadTask1 = [[transferManager getObject:getImageRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {


        if(task.error)
        {
            NSLog(@"Error: %@",task.error);
        }
        else
        {
            NSData *data = [task.result body];
            image = [UIImage imageWithData:data];
        }
        return nil;
    }];

    [downloadTask1 waitUntilFinished];

    return image;


}

我还尝试注释掉getImageData方法的第一行,以使用初始化后的transferManager,并获得相同的结果。

当我在成功下载时使用AWS详细日志记录时,我会获得以下信息:

代码语言:javascript
复制
2015-06-11 16:12:51.199 QuizOnTap[7362:538728] AWSiOSSDKv2 [Debug] AWSSignature.m line:241 | -[AWSSignatureV4Signer signS3RequestV4:] | Canonical request: [GET
/quizontapimages/quizimages/WABD_QSI_333x118.png

content-type:binary/octet-stream
host:s3.amazonaws.com
user-agent:aws-sdk-iOS/2.0.17 iPhone-OS/8.3 en_US
x-amz-content-sha256:*some key*
x-amz-date:20150611T211251Z

content-type;host;user-agent;x-amz-content-sha256;x-amz-date
*some key*]
2015-06-11 16:12:51.199 QuizOnTap[7362:538728] AWSiOSSDKv2 [Debug] AWSSignature.m line:248 | -[AWSSignatureV4Signer signS3RequestV4:] | AWS4 String to Sign: [AWS4-HMAC-SHA256
20150611T211251Z
20150611/us-east-1/s3/aws4_request
*some key*]
2015-06-11 16:12:51.493 QuizOnTap[7362:539182] AWSiOSSDKv2 [Debug] AWSURLResponseSerialization.m line:258 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response header: [{
    "Accept-Ranges" = bytes;
    "Content-Length" = 15947;
    "Content-Type" = "image/png";
    Date = "Thu, 11 Jun 2015 21:12:52 GMT";
    Etag = "\"ff690cf7807b538278fc1590ce446785\"";
    "Last-Modified" = "Sun, 31 May 2015 16:14:42 GMT";
    Server = AmazonS3;
    "x-amz-id-2" = "QIu6OWhmuO53z6Qgo+Q/4gsN4dQbyCAPbwS4QyDm/pmmSRXj8M5O4x5GoXMQq/rgSh0AKqt0uVk=";
    "x-amz-request-id" = E0DB22A10F901130;
}]
2015-06-11 16:12:51.493 QuizOnTap[7362:539182] AWSiOSSDKv2 [Verbose] AWSURLResponseSerialization.m line:263 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: [(null)]

在后面的过程中,我调用了从未从downloadTask1 waitUntilFinished;返回的相同代码。以下是该交易的AWS详细日志。还要注意长时间运行的操作警告。

代码语言:javascript
复制
2015-06-11 16:14:03.075 QuizOnTap[7362:538689] AWSiOSSDKv2 [Debug] AWSSignature.m line:241 | -[AWSSignatureV4Signer signS3RequestV4:] | Canonical request: [GET
/quizontapimages/quizimages/neat_square_1.png

content-type:binary/octet-stream
host:s3.amazonaws.com
user-agent:aws-sdk-iOS/2.0.17 iPhone-OS/8.3 en_US
x-amz-content-sha256:*some key*
x-amz-date:20150611T211403Z

content-type;host;user-agent;x-amz-content-sha256;x-amz-date
*some key*]
2015-06-11 16:14:03.075 QuizOnTap[7362:538689] AWSiOSSDKv2 [Debug] AWSSignature.m line:248 | -[AWSSignatureV4Signer signS3RequestV4:] | AWS4 String to Sign: [AWS4-HMAC-SHA256
20150611T211403Z
20150611/us-east-1/s3/aws4_request
*some key*]

2015-06-11 16:14:03.076 QuizOnTap[7362:538689] Warning: A long-running operation is being executed on the main thread.
 Break on warnBlockingOperationOnMainThread() to debug.

2015-06-11 16:14:03.396 QuizOnTap[7362:540085] AWSiOSSDKv2 [Debug] AWSURLResponseSerialization.m line:258 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response header: [{
    "Accept-Ranges" = bytes;
    "Content-Length" = 4998;
    "Content-Type" = "image/png";
    Date = "Thu, 11 Jun 2015 21:14:04 GMT";
    Etag = "\"7b27202a6560ed99277acebf2235ba89\"";
    "Last-Modified" = "Fri, 22 May 2015 15:16:13 GMT";
    Server = AmazonS3;
    "x-amz-id-2" = "5TxaPQy8jblFr8qVXfiSg3pK0EfOqSdEWdykE0kfRKUdrfxvmxvYvzf0uXGYiXXliAG/DbM55tM=";
    "x-amz-request-id" = 9F7BC6AC5494D285;
}]
2015-06-11 16:14:03.396 QuizOnTap[7362:540085] AWSiOSSDKv2 [Verbose] AWSURLResponseSerialization.m line:263 | -[AWSXMLResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body: [(null)]
EN

回答 1

Stack Overflow用户

发布于 2015-06-13 02:29:57

您没有在- getImageData:中保留对transferManager对象的强引用。请记住,- getObject:是一个异步方法,它会立即返回。您需要保留对服务客户端的强引用,直到请求完成处理。

如果您使用用于Xcode 2.1.2的AWS Mobile SDK,iOS应该会在使用- initWithConfiguration:时给您一个编译器警告。该方法已被弃用,以减少API的滥用,例如本例。请使用+ defaultS3TransferManager+ S3TransferManagerForKey:检索AWSS3TransferManager对象。

(此外,日志显示您使用的是2.0.17,而不是2.1.2。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30802773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档