-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3Methods.cs
More file actions
317 lines (249 loc) · 12.7 KB
/
S3Methods.cs
File metadata and controls
317 lines (249 loc) · 12.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using SharedMethods;
using DataProtection;
using System.Threading;
using S3_SimpleBackup.Models;
namespace S3_SimpleBackup
{
public class S3Methods
{
public IAmazonS3 GenerateS3Client(string s3Host, string s3AccessKey, SecureString s3SecureKey)
{
var config = new AmazonS3Config { ServiceURL = s3Host };
var s3Credentials = new BasicAWSCredentials(s3AccessKey, UnProtect.ConvertToInsecureString(s3SecureKey));
return new AmazonS3Client(s3Credentials, config);
}
/// <summary>
/// Attempts to list the contents of a given bucket
/// Once connection opens, success is assumed
/// </summary>
/// <param name="s3Host">An S3 host/REST URL. Must include https://</param>
/// <param name="s3AccessKey">The access / user key for S3 host</param>
/// <param name="s3SecureKey">The secret key for the S3 host</param>
/// <param name="bucketToTarget">The full name of the bucket to target</param>
/// /// <param name="parentWindow">Reference to the window calling the function. Normally equal to 'this'</param>
/// <returns></returns>
public async Task<string> Test_BucketConnectionAsync(string s3Host, string s3AccessKey, SecureString s3SecureKey, string bucketToTarget, Window parentWindow)
{
//TODO: Add an auto cancel token to cancel after 40 seconds
try
{
Output.WriteToUI($"[TEST] Testing Connection to Bucket: {bucketToTarget}", parentWindow);
Output.WriteToUI($"[TEST] Spawning S3Client", parentWindow);
IAmazonS3 _s3Client = GenerateS3Client(s3Host, s3AccessKey, s3SecureKey);
var request = new ListObjectsV2Request
{
BucketName = bucketToTarget,
MaxKeys = 1,
};
var response = new ListObjectsV2Response();
Output.WriteToUI($"[TEST] Connecting to server...", parentWindow);
response = await _s3Client.ListObjectsV2Async(request);
Output.WriteToUI($"[TEST] Connection Succeeded", parentWindow);
return "Connection Succeeded";
}
catch (AmazonS3Exception ex)
{
Debug.WriteLine($"Error encountered on server:'{ex.Message}'");
Output.WriteToUI($"[TEST] Error encountered on server:'{ex.Message}'", parentWindow);
return $"Error encountered: \n'{ex.Message}'";
}
}
public async Task<bool> Test_ListBucketContentsAsync(string s3Host, string s3AccessKey, SecureString s3SecureKey, string bucketToTarget, string rootFolderPath = "")
{
try
{
IAmazonS3 _s3Client = GenerateS3Client(s3Host, s3AccessKey, s3SecureKey);
rootFolderPath = rootFolderPath == "/" ? "" : rootFolderPath;
rootFolderPath = rootFolderPath[0] == '/' ? rootFolderPath.Substring(1) : rootFolderPath;
var request = new ListObjectsV2Request
{
BucketName = bucketToTarget,
MaxKeys = 1000,
Prefix = rootFolderPath
};
var response = new ListObjectsV2Response();
response = await _s3Client.ListObjectsV2Async(request);
foreach (S3Object obj in response.S3Objects)
{
if (rootFolderPath != "")
{
Debug.WriteLine("Object - " + obj.Key);
}
else
{
if ((obj.Key.Count(c => (c == '/')) == 1) && (obj.Key.Substring(obj.Key.IndexOf('/')).Length == 1))
{
Debug.WriteLine("Object - " + obj.Key.Substring(0, obj.Key.IndexOf('/')));
}
}
}
return true;
}
catch (AmazonS3Exception ex)
{
Debug.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects.");
return false;
}
}
/// <summary>
/// Uploads a given file to the given bucket
/// </summary>
/// <param name="s3Host">An S3 host/REST URL. Must include https://</param>
/// <param name="s3AccessKey">The access / user key for S3 host</param>
/// <param name="s3SecureKey">The secret key for the S3 host</param>
/// <param name="bucketToTarget">The full name of the bucket to target</param>
/// <param name="itemToUploadPath">Full path to single file to upload</param>
/// <returns></returns>
public async Task<bool> Test_UploadTestFile(string s3Host, string s3AccessKey, SecureString s3SecureKey, string bucketToTarget, string itemToUploadPath, Window parentWindow)
{
try
{
Output.WriteToUI($"[TEST] Spawning S3Client", parentWindow);
IAmazonS3 _s3Client = GenerateS3Client(s3Host, s3AccessKey, s3SecureKey);
var putRequest = new PutObjectRequest
{
BucketName = bucketToTarget,
Key = System.IO.Path.GetFileName(itemToUploadPath),
FilePath = itemToUploadPath
};
Output.WriteToUI($"[TEST] Target Bucket: {bucketToTarget}", parentWindow);
Output.WriteToUI($"[TEST] File to upload: {itemToUploadPath}", parentWindow);
Output.WriteToUI($"[TEST] Remote file name: {System.IO.Path.GetFileName(itemToUploadPath)}", parentWindow);
putRequest.Metadata.Add("x-amz-meta-title", "FileUploadTest");
Output.WriteToUI($"[TEST] Uploading file: {itemToUploadPath}", parentWindow);
await _s3Client.PutObjectAsync(putRequest);
Output.WriteToUI($"[TEST] Uploaded file: {itemToUploadPath}", parentWindow);
return true;
}
catch (AmazonS3Exception e)
{
Output.WriteToUI($"[TEST] Error during upload: {e.Message}", parentWindow);
Output.WriteToUI($"[TEST] Please check S3 setup and try again: {e.Message}", parentWindow);
return false;
}
}
public async Task<bool> UploadToS3(string s3Host, string s3AccessKey, SecureString s3SecureKey, string SourcePath, string s3BucketName, string JobName, bool RecursiveSync, Window parentWindow)
{
try
{
Output.WriteToUI($"Starting Job {JobName}", parentWindow);
Output.WriteToUI($"Indexing Objects in {SourcePath}", parentWindow);
List<FileInformation> objectsToUpload = SharedMethods.FileInteraction.FileIndexFromPath(SourcePath, false, RecursiveSync, parentWindow);
Output.WriteToUI($"Discovered {objectsToUpload.Count} objects in {SourcePath}", parentWindow);
// Create an S3 client
var s3Client = GenerateS3Client(s3Host, s3AccessKey, s3SecureKey);
int SuccessCount = 0;
int FailedCount = 0;
Output.WriteToUI($"Uploading {objectsToUpload.Count} objects to bucket {s3BucketName}", parentWindow);
foreach (var singleObject in objectsToUpload)
{
// Create a PutObjectRequest to upload the file / directory
PutObjectRequest request;
if (singleObject.isDirectory)
{
request = new PutObjectRequest
{
BucketName = s3BucketName,
Key = singleObject.ToS3Path(singleObject.FQPath) + "/",
ContentBody = ""
};
try
{
await s3Client.PutObjectAsync(request);
Output.WriteToUI($"Uploaded {singleObject.FQPath}", parentWindow);
SuccessCount++;
}
catch (Exception putException)
{
Output.WriteToUI($"Upload Failed {singleObject.FQPath}" +
$" ({putException.Message})", parentWindow);
FailedCount++;
}
}
else
{
using (var objectDataStream = new FileStream(singleObject.FQPath, FileMode.Open, FileAccess.Read))
{
Debug.WriteLine($"{singleObject.ObjectName} - {singleObject.FileHash}");
request = new PutObjectRequest
{
BucketName = s3BucketName,
Key = singleObject.ToS3Path(singleObject.FQPath),
InputStream = objectDataStream
};
request.Metadata.Add("FileHash", singleObject.FileHash);
try
{
await s3Client.PutObjectAsync(request);
string SizeTag = (singleObject.FileSize / 1024f) / 1024f > 1 ? $"{(singleObject.FileSize / 1024f) / 1024f}MB" : $"{singleObject.FileSize} bytes";
Output.WriteToUI($"Uploaded [{SizeTag}] {singleObject.FQPath}", parentWindow);
SuccessCount++;
}
catch (Exception putException)
{
Output.WriteToUI($"Upload Failed {singleObject.FQPath}" +
$" ({putException.Message})", parentWindow);
FailedCount++;
}
}
}
};
Output.WriteToUI($"Finished Job {JobName}", parentWindow);
Output.WriteToUI($"Job Summary - Success [{SuccessCount}] | Failed [{FailedCount}]", parentWindow);
return true;
}
catch (Exception e)
{
Output.WriteToUI($"An error occured with the S3 connection:\n{e.Message}",parentWindow);
return false;
}
}
public async void DeleteAllObjectsinBucket(string s3Host, string s3AccessKey, SecureString s3SecureKey,string bucketToTarget, Window parentWindow)
{
try
{
Output.WriteToUI($"Deleting all objects in bucket {bucketToTarget}", parentWindow);
// Create an S3 client
var config = new AmazonS3Config { ServiceURL = s3Host };
var s3Credentials = new BasicAWSCredentials(s3AccessKey, UnProtect.ConvertToInsecureString(s3SecureKey));
using (AmazonS3Client s3Client = new AmazonS3Client(s3Credentials, config))
{
var ListRequest = new ListObjectsV2Request
{
BucketName = bucketToTarget
};
ListObjectsV2Response ListofObjects;
do
{
ListofObjects = await s3Client.ListObjectsV2Async(ListRequest);
foreach (S3Object obj in ListofObjects.S3Objects)
{
Output.WriteToUI($"Deleting Object {obj.Key}", parentWindow);
await s3Client.DeleteObjectAsync(bucketToTarget, obj.Key);
}
ListRequest.ContinuationToken = ListofObjects.NextContinuationToken;
} while (ListofObjects.IsTruncated);
}
Output.WriteToUI($"Successfully deleted all contents from bucket {bucketToTarget}", parentWindow);
}
catch (Exception deleteException)
{
Output.WriteToUI($"An error occurred\n:{deleteException}\n\nPlease try again", parentWindow);
}
}
}
}