I've got some local files that need to be updated from S3 occasionally. I'd prefer to update them only if the S3 version is newer. I can't quite understand how to use the
ModifiedSinceDate
property in S3 Objects. In fact, I'm not sure whether to use Metadata, or if there is something else I'm supposed to be checking.
I'm imagining something like this:
GetObjectRequest request = new GetObjectRequest().WithBucketName(my_bucketName).WithKey(s3filepath);
using (GetObjectResponse response = client.GetObject(request))
{
string s3DateModified = response.ModifiedSinceDate; // Complete psuedo code
DateTime s3_creationTime = DateTime.Convert(s3DateModified); //Complete psuedo code
DateTime local_creationTime = File.GetCreationTime(@"c:\file.txt");
if (s3_creationTime > local_CreationTime)
{
//download the S3 object;
}
}
Thanks so much!
EDIT---------------------------------------
I think I'm almost there... I'm writing the date modified as a meta-tag when I originally upload the object:
PutObjectRequest titledRequest = new PutObjectRequest();
.WithFilePath(savePath)
.WithBucketName(bucketName)
.WithKey(tempFileName)
.WithMetaData("Last-Modified", System.DateTime.Now.ToString());
Then using this for the check/download:
using (GetObjectResponse response = client.GetObject(request))
{
string lastModified = response.Metadata["x-amz-meta-last-modified"];
DateTime s3LastModified = Convert.ToDateTime(lastModified);
string dest = Path.Co开发者_开发问答mbine(@"c:\Temp\", localFileName);
DateTime localLastModified = File.GetLastWriteTime(dest);
if (!File.Exists(dest))
{
response.WriteResponseStreamToFile(dest);
}
if (s3LastModified > localLastModified)
{
response.WriteResponseStreamToFile(dest);
}
}
Something's breaking, I think perhaps the WriteResponseStream is asynchronous, and it's trying two respnse.WriteResponseStreamtToFile(dest) at the same time. Anyways, I'll update this if I can get it nailed down.
You should use GetObjectMetadata
to retrieve information about an object without actually downloading the object itself.
The GetObjectMetadataResponse
class has a LastModified
property or you could use custom Metadata I guess.
Then you would use GetObject
to actually download and save the file.
Something like:
using (GetObjectMetadataResponse response = client.GetObjectMetadata(request))
{
DateTime s3LastModified = response.LastModified;
string dest = Path.Combine(@"c:\Temp\", localFileName);
DateTime localLastModified = File.GetLastWriteTime(dest);
if (!File.Exists(dest) || s3LastModified > localLastModified)
{
// Use GetObject to download and save file
}
}
精彩评论