c# - 在 C# 中,如何将本地文件的 datemodified 属性与 Amazon S3 文件进行比较?

标签 c# amazon-s3

我有一些本地文件需要偶尔从 S3 更新。我宁愿仅在 S3 版本更新时才更新它们。我不太明白如何使用

    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);
                    }

                }

有什么问题,我认为 WriteResponseStream 可能是异步的,并且它同时尝试两个 respnse.WriteResponseStreamtToFile(dest) 。无论如何,如果我能确定下来,我会更新它。

最佳答案

您应该使用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
    }

}

关于c# - 在 C# 中,如何将本地文件的 datemodified 属性与 Amazon S3 文件进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7668264/

相关文章:

Javascript S3 GET Bucket(列表对象)格式化?

javascript - 如何使用 Node 将图像上传到 S3

amazon-s3 - 参数中缺少必需的 key 'Bucket'

c# - 在 Kendo 窗口弹出窗口上禁用 Esc 键

c# - 在没有计时器的情况下保持 Windows 服务运行

c# - 一种接口(interface)方法的不同类型实现

amazon-web-services - 如何保护传输中的 AWS S3 上传/下载数据?

使用 WinRAR 的 C# 程序

C# 泛型类型推断与协变 - 错误或限制

java - 新手 : query amazon s3 database in Android client (Java)