This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
73 lines (62 loc) · 2.29 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
using System;
using System.Reflection;
using System.Threading.Tasks;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using static System.FormattableString;
namespace YcSignatureProblem
{
public static class Program
{
public async static Task<int> Main(string[] args)
{
if (args.Length != 3)
{
Console.Error.WriteLine(Invariant($"Usage: {Assembly.GetEntryAssembly().GetName().Name} <access key> <secret key> <bucket name>"));
return -1;
}
var credentials = new BasicAWSCredentials(args[0], args[1]);
var bucketName = args[2];
var config = new AmazonS3Config
{
LogMetrics = true,
ServiceURL = "https://storage.yandexcloud.net/",
};
var loggingConfig = AWSConfigs.LoggingConfig;
loggingConfig.LogTo = LoggingOptions.Console;
loggingConfig.LogResponses = ResponseLoggingOption.Always;
loggingConfig.LogMetricsFormat = LogMetricsFormatOption.JSON;
var client = new AmazonS3Client(credentials, config);
foreach (var name in new[] { "test-object", "test object" })
{
await Put(client, bucketName, name);
}
return 0;
}
private static async Task Put(AmazonS3Client client, string bucketName, string objectName)
{
var request = new GetObjectRequest
{
BucketName = bucketName,
Key = objectName,
};
try
{
(await client.GetObjectAsync(request)).Dispose();
}
catch (AmazonS3Exception exception) when (exception.StatusCode == System.Net.HttpStatusCode.NotFound)
{
Console.WriteLine(Invariant($"Missing object \"{objectName}\"."));
return;
}
catch (AmazonS3Exception exception) when (exception.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
Console.Error.WriteLine(Invariant($"Failed to get \"{objectName}\"."));
return;
}
Console.WriteLine(Invariant($"Successfully get \"{objectName}\"."));
}
}
}