我有一些本地文件需要偶尔从S3更新。我倾向于只在S3版本较新的情况下更新它们。我不太明白如何使用
ModifiedSinceDateS3对象中的属性。事实上,我不确定是否使用元数据,或者是否有其他我应该检查的东西。
我的想象是这样的:
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;
}
}非常感谢!
编辑
我想我快到了..。当我最初上传对象时,我将修改后的日期写为元标记:
PutObjectRequest titledRequest = new PutObjectRequest();
.WithFilePath(savePath)
.WithBucketName(bucketName)
.WithKey(tempFileName)
.WithMetaData("Last-Modified", System.DateTime.Now.ToString());然后使用此命令进行检查/下载:
using (GetObjectResponse response = client.GetObject(request))
{
string lastModified = response.Metadata["x-amz-meta-last-modified"];
DateTime s3LastModified = Convert.ToDateTime(lastModified);
string dest = Path.Combine(@"c:\Temp\", localFileName);
DateTime localLastModified = File.GetLastWriteTime(dest);
if (!File.Exists(dest))
{
response.WriteResponseStreamToFile(dest);
}
if (s3LastModified > localLastModified)
{
response.WriteResponseStreamToFile(dest);
}
}有些地方出问题了,我认为WriteResponseStream可能是异步的,它同时尝试两个respnse.WriteResponseStreamtToFile(dest)。不管怎样,如果我能弄清楚,我会更新的。
发布于 2011-10-06 17:55:31
您应该使用GetObjectMetadata来检索有关对象的信息,而无需实际下载对象本身。
GetObjectMetadataResponse类有一个LastModified属性,我想您也可以使用定制的元数据。
然后,您将使用GetObject实际下载并保存文件。
类似于:
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
}
}https://stackoverflow.com/questions/7668264
复制相似问题