AWS S3 文件下载与 Objective-C 成功然后不成功



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

我使用的是 AWS iOS 开发工具包版本 2.1.2。 我已经使用cocoapods下载并安装了SDK。 我已经阅读了堆栈溢出上的其他线程,但它们没有解决此问题。

代码:

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方法的第一行,以使用具有相同结果的初始化传输管理器。

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

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 waitUntilDone]; 返回。 下面是该事务的 AWS 详细日志。 还要注意长时间运行的操作警告。

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)]

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

如果您使用适用于 iOS 的 AWS 移动开发工具包 2.1.2,Xcode 应向您提供编译器警告,以使用 - initWithConfiguration: 。该方法已弃用,以减轻 API 的误用,例如这种情况。请使用+ defaultS3TransferManager+ S3TransferManagerForKey:检索AWSS3TransferManager对象。

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

相关内容

最新更新