dimanche 30 novembre 2014

iOS / Amazon Web Services - AWSS3GetObjectRequest ifModifiedSince NOT WORKING

Building for iOS 7+ , Building on Xcode 6.1 , Using Amazon SDK AWSiOSSDKv2 2.0.12 , Testing on iPhone 5s and iPad 2


I am downloading images from my Amazon S3 bucket with the Amazon SDK for iOS. The downloading is working fine but I want to use the ifModifiedSince property to retrieve only images that have been modified since a certain date (see http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSS3GetObjectRequest.html#//api/name/ifModifiedSince )


However, this is not working. Even when I specify a ifModifiedSince date that is LATER THAN the modified date of the file on S3, the file is returned.


According to the Amazon documentation: ifModifiedSince -



Return the object only if it has been modified since the specified time, otherwise return a 304 (not modified).



So I am not sure if I am doing something wrong or Amazon has a bug in the SDK.


Here is my code:



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

}



Aucun commentaire:

Enregistrer un commentaire