Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get upload date of a file Amazon S3, AWS IOS SDK

I have a S3 bucket with files in it. I can successfully download and display the file on my app.

However I want to learn that if the document is the latest or not. With Firefox S3 extension I can see that in bucket file name, file size and upload time are saved to S3. An example of upload time is 10/10/2012 11:35 PM

to get url I use

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        // Set the content type so that the browser will treat the URL as an image.
        S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
        override.contentType = @" ";

        // Request a pre-signed URL to picture that has been uplaoded.
        S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
        gpsur.key                     = _fileName;
        gpsur.bucket                  = [Constants pictureBucket];
        gpsur.expires                 = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
        gpsur.responseHeaderOverrides = override;

        // Get the URL
        NSError *error;
        NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];

How can I get upload date of a file on Amazon S3?

EDIT::::ANSWER

OK thanks to answer with following method I can get the date

S3GetObjectMetadataRequest *getMetadataRequest = [[S3GetObjectMetadataRequest alloc] initWithKey:_fileName withBucket:[Constants pictureBucket]];
        S3GetObjectMetadataResponse *getMetadataResponse = [self.s3 getObjectMetadata:getMetadataRequest];

        NSString *fileLastModifiedDate = [[NSString alloc] init];
        fileLastModifiedDate = [NSString stringWithFormat:@"%@",getMetadataResponse.lastModified];
        NSLog(@"Last Modified date %@",fileLastModifiedDate);
like image 572
Mord Fustang Avatar asked Jan 15 '23 08:01

Mord Fustang


2 Answers

You have to make a S3GetObjectMetadataRequest.

S3GetObjectMetadataRequest *getMetadataRequest = [[S3GetObjectMetadataRequest alloc] initWithKey:filePath withBucket:BUCKET_NAME];
S3GetObjectMetadataResponse *getMetadataResponse = [self getObjectMetadata:getMetadataRequest];

http://docs.amazonwebservices.com/AmazonS3/latest/dev/UsingMetadata.html

like image 181
IluTov Avatar answered Jan 17 '23 21:01

IluTov


Mord Fustang, one thing though about the code in your answer. You don't need to allocate an NSString object before accessing it from the response.

NSString *fileModifiedDate = getMetadataResponse.lastModified;
NSLog(@"Last Modified date %@", fileModifiedDate);
like image 41
donarb Avatar answered Jan 17 '23 21:01

donarb