针对 iOS 7+ 构建, 基于 Xcode 6.1 构建, 使用 Amazon SDK AWSiOSSDKv2 2.0.12 、 在iPhone 5s 和 iPad 2 上进行测试
我正在使用适用于 iOS 的 Amazon SDK 从我的 Amazon S3 存储桶下载图像。 下载工作正常,但我想使用 ifModifiedSince 属性来仅检索自特定日期以来已修改的图像 (参见http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3GetObjectRequest.html#//api/name/ifModifiedSince)
但是,这不起作用。即使我指定的 ifModifiedSince 日期晚于 S3 上文件的修改日期,也会返回该文件。
根据亚马逊文档: ifModifiedSince -
Return the object only if it has been modified since the specified time, otherwise return a 304 (not modified).
所以我不确定是我做错了什么还是亚马逊的 SDK 中有错误。
这是我的代码:
-(void)downloadPhotoWithName:(NSString*)name completed:(retrievedImage)completed {
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [cachePaths firstObject];
NSString *filename = [NSString stringWithFormat:@"%@.jpg", name];
NSString *filePath = [cacheDirectory stringByAppendingPathComponent:filename];
AWSS3 *transferManager = [AWSS3 defaultS3];
AWSS3GetObjectRequest *profilePhoto = [[AWSS3GetObjectRequest alloc] init];
profilePhoto.bucket = S3BUCKETNAMEIMAGE;
profilePhoto.key = filename;
if ([manager fileExistsAtPath:filePath isDirectory:NULL]) {
NSDictionary *att = [manager attributesOfItemAtPath:filePath error:nil];
if (att) {
//Get the date of when the file was modified so we can request to retrieve
//the file only if it was modified SINCE that date
NSDate *modifiedDate = [att objectForKey:NSFileModificationDate];
if (modifiedDate) {
profilePhoto.ifModifiedSince = modifiedDate;
}
}
}
[[transferManager getObject:profilePhoto] continueWithBlock:^id(BFTask *task) {
//If it was working we should get a 304 not the image file
if (task.result) {
AWSS3GetObjectOutput *output = (AWSS3GetObjectOutput*)task.result;
NSData *imageData = (NSData*)output.body;
if (imageData) {
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:output.lastModified, NSFileModificationDate, NULL];
//The log confirms that the date I am passing for ifModifiedSince is LATER THAN
//the last modified date of the file on S3
//but the the image file is returned anyway.. is it a problem with Amazon ?
NSLog(@"output.lastModified: %@\nmodifiedDate: %@", output.lastModified, profilePhoto.ifModifiedSince);
if ([manager createFileAtPath:filePath contents:imageData attributes:attr]) {
completed(imageData);
}
else {
NSLog(@"Could not save image to disk for some reason");
completed(nil);
}
}
else {
completed(nil);
}
}
else if (task.error) {
NSLog(@"DownloadPhotoError: %@", task.error);
completed(nil);
}
return nil;
}];
}
最佳答案
我们发布了适用于 iOS 2.0.14 的 AWS 移动开发工具包。它应该解决时间格式问题。
关于ios - AWSS3GetObjectRequest ifModifiedSince 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27190654/