-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
223 lines (205 loc) · 8 KB
/
Program.cs
File metadata and controls
223 lines (205 loc) · 8 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.Extensions.Configuration;
class Program
{
static void PrintUsage()
{
Console.WriteLine("Usage: dotnet run [-- [<localpath> <s3uri>] [--upload] [--hidelocal]]");
Console.WriteLine();
Console.WriteLine("Compares files in a local directory (non-recursive, top level only) against");
Console.WriteLine("the objects under an S3 prefix. Reports files missing from S3 and files in");
Console.WriteLine("S3 but not local. Match is by filename, case-insensitive.");
Console.WriteLine();
Console.WriteLine("Positional (optional, overrides appsettings.json):");
Console.WriteLine(" <localpath> local directory to read");
Console.WriteLine(" <s3uri> s3://bucket[/prefix/]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --upload upload files that are local-only to S3");
Console.WriteLine(" --hidelocal suppress the \"in S3 but not local\" report");
Console.WriteLine(" -h, --help, -?, /?, ? show this help and exit");
Console.WriteLine();
Console.WriteLine("Configuration (appsettings.json):");
Console.WriteLine(" S3:LocalPath, S3:S3Uri, S3:Region");
}
static async Task Main(string[] args)
{
if (args.Any(a => a is "-h" or "--help" or "-?" or "/?" or "?"))
{
PrintUsage();
return;
}
foreach (var a in args)
{
if ((a.StartsWith("-") || a.StartsWith("/")) && a is not ("--upload" or "--hidelocal"))
{
Console.WriteLine($"Unknown option: {a}");
Console.WriteLine();
PrintUsage();
Environment.ExitCode = 1;
return;
}
}
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
// Defaults from config
var localPath = configuration["S3:LocalPath"] ?? "";
var s3Uri = configuration["S3:S3Uri"] ?? "";
var region = configuration["S3:Region"] ?? "us-east-1";
// Parse args: positional args override localPath and s3Uri; --upload is a flag
bool upload = false;
bool hideMissingLocally = false;
var positional = new List<string>();
foreach (var arg in args)
{
if (arg == "--upload")
upload = true;
else if (arg == "--hidelocal")
hideMissingLocally = true;
else
positional.Add(arg);
}
if (positional.Count >= 1) localPath = positional[0];
if (positional.Count >= 2) s3Uri = positional[1];
Console.WriteLine("╔════════════════════════════════════════════════════════════╗");
Console.WriteLine("║ S3 vs Local File Checker ║");
Console.WriteLine("╚════════════════════════════════════════════════════════════╝");
Console.WriteLine();
Console.WriteLine($"Local path : {localPath}");
Console.WriteLine($"S3 URI : {s3Uri}");
Console.WriteLine($"Mode : {(upload ? "list + upload missing" : "list only")}");
Console.WriteLine();
// Validate local path
if (!Directory.Exists(localPath))
{
Console.WriteLine($"✗ Local path does not exist: {localPath}");
return;
}
// Parse S3 URI → bucket + prefix
if (!s3Uri.StartsWith("s3://"))
{
Console.WriteLine($"✗ S3 URI must start with s3:// Got: {s3Uri}");
return;
}
var withoutScheme = s3Uri.Substring(5); // strip "s3://"
var slashIndex = withoutScheme.IndexOf('/');
string bucket, prefix;
if (slashIndex < 0)
{
bucket = withoutScheme;
prefix = "";
}
else
{
bucket = withoutScheme.Substring(0, slashIndex);
prefix = withoutScheme.Substring(slashIndex + 1);
}
try
{
var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.GetBySystemName(region));
// --- List local files ---
Console.WriteLine("Reading local files...");
var localFiles = Directory.GetFiles(localPath)
.Select(f => Path.GetFileName(f))
.ToHashSet(StringComparer.OrdinalIgnoreCase);
Console.WriteLine($" {localFiles.Count} local files found");
Console.WriteLine();
// --- List S3 files at prefix ---
Console.WriteLine($"Reading S3 objects at s3://{bucket}/{prefix} ...");
var s3Files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var request = new ListObjectsV2Request
{
BucketName = bucket,
Prefix = prefix
};
ListObjectsV2Response response;
do
{
response = await s3Client.ListObjectsV2Async(request);
foreach (var obj in response.S3Objects)
{
// Strip the prefix to get just the filename portion
var key = obj.Key;
if (key.StartsWith(prefix))
key = key.Substring(prefix.Length);
// Skip "directory" entries (keys ending with /)
if (!string.IsNullOrEmpty(key) && !key.EndsWith("/"))
s3Files.Add(key);
}
request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated == true);
Console.WriteLine($" {s3Files.Count} S3 objects found");
Console.WriteLine();
// --- Compare ---
var missingFromS3 = localFiles.Where(f => !s3Files.Contains(f)).OrderBy(f => f).ToList();
var missingLocally = s3Files.Where(f => !localFiles.Contains(f)).OrderBy(f => f).ToList();
var inSync = localFiles.Count(f => s3Files.Contains(f));
Console.WriteLine("═══ Results ═══════════════════════════════════════════════");
Console.WriteLine($" In sync : {inSync}");
Console.WriteLine($" Missing from S3 : {missingFromS3.Count}");
Console.WriteLine($" Missing locally : {missingLocally.Count} (informational)");
Console.WriteLine();
if (missingFromS3.Count > 0)
{
Console.WriteLine("Files missing from S3:");
foreach (var f in missingFromS3)
Console.WriteLine($" - {f}");
Console.WriteLine();
}
if (!hideMissingLocally && missingLocally.Count > 0)
{
Console.WriteLine("Files in S3 but not local (informational):");
foreach (var f in missingLocally)
Console.WriteLine($" - {f}");
Console.WriteLine();
}
// --- Upload missing files ---
if (upload && missingFromS3.Count > 0)
{
Console.WriteLine($"Uploading {missingFromS3.Count} missing file(s) to s3://{bucket}/{prefix} ...");
int uploaded = 0;
int failed = 0;
foreach (var filename in missingFromS3)
{
var localFilePath = Path.Combine(localPath, filename);
var s3Key = prefix + filename;
try
{
var putRequest = new PutObjectRequest
{
BucketName = bucket,
Key = s3Key,
FilePath = localFilePath
};
await s3Client.PutObjectAsync(putRequest);
uploaded++;
Console.WriteLine($" ✓ Uploaded: {filename}");
}
catch (AmazonS3Exception ex)
{
failed++;
Console.WriteLine($" ✗ Failed: {filename} — {ex.Message}");
}
}
Console.WriteLine();
Console.WriteLine($"Upload complete: {uploaded} succeeded, {failed} failed");
}
else if (upload && missingFromS3.Count == 0)
{
Console.WriteLine("Nothing to upload — all local files are already in S3.");
}
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"✗ AWS S3 Error: {ex.Message}");
Console.WriteLine($" Error Code: {ex.ErrorCode}");
}
catch (Exception ex)
{
Console.WriteLine($"✗ Error: {ex.Message}");
}
}
}