gpt4 book ai didi

ios - 使用 Objective-C 下载 AWS S3 文件成功然后不成功

转载 作者:行者123 更新时间:2023-11-28 21:44:27 26 4
gpt4 key购买 nike

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

我使用的是 AWS iOS SDK 2.1.2 版。我已经使用 cocoapods 下载并安装了 SDK。我已阅读 Stack Overflow 上的其他主题,但它们并未解决此问题。

代码:

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 详细日志记录成功下载时,我得到以下信息:

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 详细日志。还要注意长时间运行的操作警告。

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 2.1.2 的 AWS 移动开发工具包,Xcode 应该会向您发出有关使用 -initWithConfiguration: 的编译器警告。该方法已被弃用,以减少 API 的滥用,例如这种情况。请使用 + defaultS3TransferManager+ S3TransferManagerForKey: 检索 AWSS3TransferManager 对象。

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

关于ios - 使用 Objective-C 下载 AWS S3 文件成功然后不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com