在C#中,如何将本地文件的datemodified属性与AmazonS3文件进行比较?
我有一些本地文件需要偶尔从S3更新。我宁愿只在S3版本更新时更新它们。我不太明白如何使用这个词在C#中,如何将本地文件的datemodified属性与AmazonS3文件进行比较?,c#,amazon-s3,C#,Amazon S3,我有一些本地文件需要偶尔从S3更新。我宁愿只在S3版本更新时更新它们。我不太明白如何使用这个词 ModifiedSinceDate S3对象中的属性。事实上,我不确定是否要使用元数据,或者是否还有其他需要检查的内容 我在想象这样的事情: GetObjectRequest request = new GetObjectRequest().WithBucketName(my_bucketName).WithKey(s3filepath); using (
ModifiedSinceDate
S3对象中的属性。事实上,我不确定是否要使用元数据,或者是否还有其他需要检查的内容
我在想象这样的事情:
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);
}
}
有些东西坏了,我想可能WriterResponseStream是异步的,它正在同时尝试两个Response.WriterResponseStreamTofile(dest)。无论如何,如果我能把它弄清楚,我会更新它。您应该使用它来检索有关对象的信息,而不必实际下载对象本身
该类有一个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
}
}
嘿,杰夫-非常感谢你的帮助。这基本上是可行的,但是response.LastModified的结果可能会很奇怪。我不确定这是否是因为对象正在被缓存,但每次调用从S3返回的日期时间可能不同。你有过这个问题吗?否则,这会很好地工作。@Hairgami_大师-这很奇怪。我以前没有注意到这一点,尽管我很少使用
LastModified
属性。它们是随机的日期,还是总是一个最近的日期,好像文件又被上传了一样?老实说,我看不出一个模式。我试图理解,如果将相同的文件放在S3上两次,会产生相同的上次修改结果,还是会有所不同。换句话说,将文件复制到S3是否考虑修改它?@Hairgami_Master-是的,复制/覆盖文件肯定会更改上次修改的日期。谢谢Geoff-非常感谢您的帮助!