c#仅将新文件同步到亚马逊s3

时间:2016-10-27 23:54:34

标签: c# amazon-web-services amazon-s3 upload synchronization

我使用以下代码将所有本地文件上传到s3:

static string bucketName = "s3bucket";
static string directoryPath = @"C:\data\";    

private void btnUpload_Click(object sender, EventArgs e) {
    try {
        TransferUtility directoryTransferUtility =
            new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USWest2));

        TransferUtilityUploadDirectoryRequest request =
            new TransferUtilityUploadDirectoryRequest {
                BucketName = bucketName,
                Directory = directoryPath,
                SearchPattern = "*.xml"
            };

        directoryTransferUtility.UploadDirectory(request);
        MessageBox.Show("Upload completed");

    } catch (AmazonS3Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

如果我再次运行代码,所有文件都会重新上传到s3,如果我们说本地目录中有1000多个文件,那就不好了。

我知道我可以逐个文件进行比较,因为aws存储了每个文件的md5。 所以我的问题是我可以使用预装的命令执行此操作吗?我是否必须按文件递归比较文件?如果awscli包(aws s3 sync ./sourceDir s3://bucketname/)上存在同步命令,那么它是否也存在于c#中?

1 个答案:

答案 0 :(得分:0)

将替换所有文件。 S3 docs,但如果您仍想检查文件,可以使用此类功能

    ListObjectsRequest request = new ListObjectsRequest()
    {
        BucketName = "Your Bucket name",
        Delimiter = "/",
        Prefix = "location"
    };

    public bool CheckFile(ListObjectsRequest request)
    {
        bool res = false;

        try
        {

            ListObjectsResponse response = s3client.ListObjects(request);

            if (response.S3Objects != null && response.S3Objects.Count > 0)
            {
                S3Object o = response.S3Objects.Where(x => x.Size != 0 && x.LastModified > DateTime.Now.AddHours(-24)).FirstOrDefault();

                if (o != null)
                {
                    //you can use thes fields
                    // o.Key; //o.Size, //o.LastModified

                    res = true;

                }

            }
            else
            {
                res = false;

            }

        }
        catch (Exception)
        {
            throw;
        }

        return res;
    }