user2501886
user2501886

Reputation:

AWS S3 put object if not modified since (PHP)

want to put data only if existing remote data (if it exists) has not been modified since the local data.

there does not seem to be a mechanism to handle this in putObject, so my plan was to run headObject with IfModifiedSince key in the request to determine if a 304 Not modified is returned. However I'm not sure how to do this (can't find anything about http status codes in the $result object in the docs, $result looks like it is basically an associative array), also not sure if this is the way to do it or if there's a better way. i suspect in the case of 304 an exception will be raised and it's in $e->statusCode but i'm not sure; i'm curious in general what the response structure of headObject is in any case.

Thanks

Upvotes: 1

Views: 790

Answers (1)

John Hanley
John Hanley

Reputation: 81356

When you upload an object to S3, the last-modified-time is the time of the upload and not the last-modified-time of the source object.

When we write custom S3 software, we store a custom header in the key with the original last-modified-time so that later we can compare this to determine if a file update is required.

Custom headers start with "x-amz-meta-XXX". Replace the XXX with your custom header name. Then include the time with this header.

You will then read the key (object) headers and compare the dates to determine if an upload is necessary.

Another method is to create an MD5 of the source file and compare that to the MD5 value that AWS stores with the S3 object. AWS recommends creating an MD5 of the source file and providing that value to when uploading an object so that AWS can detect data corruption during data transfer.

Upvotes: 2

Related Questions