AWSS3GetObjectRequest ifModifiedSince不起作用 [英] AWSS3GetObjectRequest ifModifiedSince not working

查看:313
本文介绍了AWSS3GetObjectRequest ifModifiedSince不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对 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上文件的修改日期,也会返回文件.

根据Amazon文档: ifModifiedSince -

仅当对象自指定以来已被修改时,才返回该对象 时间,否则返回304(未修改).

因此,我不确定我做错了什么还是Amazon 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 Mobile SDK.它应该解决时间格式问题.

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

}

解决方案

We've released the AWS Mobile SDK for iOS 2.0.14. It should address the time formatting issue.

这篇关于AWSS3GetObjectRequest ifModifiedSince不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆