-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWSworker.cs
119 lines (117 loc) · 3.65 KB
/
AWSworker.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
114
115
116
117
118
119
using System;
using System.IO;
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
using System.Threading.Tasks;
using Amazon.S3.Model;
using Amazon.Runtime.Internal.Util;
using ByteSizeLib;
namespace TelegramBot
{
public class AWSworker
{
public string bucketName { get; set; }
public RegionEndpoint bucketRegion { get; set; }
private IAmazonS3 s3Client;
public MemoryStream ms { get; set; }
public string filePlacement { get; set; }
public string AWSfoldRoot { get; set; }
public string lsSpit;
public void assignS3()
{
s3Client = new AmazonS3Client(bucketRegion);
}
public async Task UploadFileAsync()
{
try
{
TransferUtility fileTransferUtility = new TransferUtility(s3Client);
using (ms)
{
await fileTransferUtility.UploadAsync(ms, bucketName, filePlacement);
}
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public async Task DownloadFileAsync()
{
try
{
TransferUtility fileTransferUtility = new TransferUtility(s3Client);
await fileTransferUtility.DownloadAsync(filePlacement, bucketName, filePlacement);
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public async Task ListingObjectsAsync()
{
try
{
lsSpit = "";
ListObjectsV2Request request = new ListObjectsV2Request
{
BucketName = $"{bucketName}",
MaxKeys = 50,
Prefix = $"{filePlacement}"
};
ListObjectsV2Response response;
do
{
response = await s3Client.ListObjectsV2Async(request);
//proccess it tho
foreach(S3Object entry in response.S3Objects)
{
string size = ByteSize.FromBytes(entry.Size).ToString();
string temKey = entry.Key.Remove(0, AWSfoldRoot.Length);
lsSpit += String.Format("{0,-15} {1,15}\n", temKey, size);
}
//Console.WriteLine($"Next continue token: {response.NextContinuationToken}");
request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
Console.WriteLine(lsSpit);
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public async Task DeleteObjectBucketAsync()
{
try
{
var deleteObjectRequest = new DeleteObjectRequest
{
BucketName = $"{bucketName}",
Key = $"{filePlacement}"
};
await s3Client.DeleteObjectAsync(deleteObjectRequest);
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}