-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
113 lines (96 loc) · 2.86 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
namespace AWSSync
{
internal static class ProgramExtensions
{
public static IEnumerable<S3Object> ListAllObjects(this AmazonS3Client client, string bucketName, string prefix )
{
var files = new List<S3Object>();
var request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = prefix
};
while (true)
{
var response = client.ListObjects(request);
files.AddRange(response.S3Objects);
if (!response.IsTruncated)
{
break;
}
request.Marker = response.NextMarker;
}
return files;
}
}
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length != 6)
{
Console.WriteLine("Args: <directory> <profileName> <region> <bucket> <prefix> <# of concurrent uploads>");
return;
}
var directory = args[0];
var profileName = args[1];
var region = args[2];
var bucketName = args[3];
var prefix = args[4];
var maxConcurrentUploads = int.Parse(args[5]);
var credentials = new StoredProfileAWSCredentials(profileName);
using (var client = new AmazonS3Client(credentials, RegionEndpoint.GetBySystemName(region)))
{
var s3files = client.ListAllObjects(bucketName, prefix);
var localfiles = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories);
var uploadRequests = (
from local in localfiles
let remoteKey = prefix + "/" + RemoveFilePrefix(local, directory).Replace('\\', '/')
join s3 in s3files on remoteKey equals s3.Key into matches
let localInfo = new FileInfo(local)
let match = matches.FirstOrDefault()
where match == null || match.Size != localInfo.Length || match.LastModified < localInfo.LastWriteTime
select new PutObjectRequest
{
BucketName = bucketName,
Key = remoteKey,
FilePath = local,
}).ToList();
if (uploadRequests.Count == 0)
{
Console.WriteLine("Everything is up to date.");
return;
}
Console.WriteLine("Uploading {0} files", uploadRequests.Count);
Parallel.ForEach(uploadRequests, new ParallelOptions { MaxDegreeOfParallelism = maxConcurrentUploads }, request => {
// ReSharper disable once AccessToDisposedClosure - Parallel.ForEach will run before client is disposed
client.PutObject(request);
Console.WriteLine("{0} uploaded.", request.Key);
});
Console.WriteLine("done.");
}
}
private static string RemoveFilePrefix(string file, string prefix)
{
if (!file.StartsWith(prefix) || file.Length <= prefix.Length)
{
throw new Exception();
}
var position = prefix.Length;
if (file[position] == '\\' || file[position] == '/')
{
position++;
}
return file.Substring(position);
}
}
}