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
|
using COSXML;
using COSXML.Auth;
using COSXML.Transfer;
// Check I'm root
if (Environment.UserName != "root")
{
Console.WriteLine("You must run this program as root");
Environment.Exit(1);
}
// Read args to determine what file to upload
const string DefaultUploadFilePath = "/tmp/data.tar.xz";
string uploadFilePath = DefaultUploadFilePath;
string? uploadDestinationPath = null;
if (args.Length == 0)
{
Console.WriteLine("You don't specify the file to upload, will upload /tmp/data.tar.xz by default.");
Console.WriteLine("You don't specify the destination to upload, will use timestamp with proper file extension.");
}
else if (args.Length == 1)
{
if (args[0].Length == 0)
{
Console.Error.WriteLine("File to upload can't be empty string.");
Environment.Exit(2);
}
uploadFilePath = args[0];
Console.WriteLine("You don't specify the destination to upload, will use timestamp with proper file extension.");
}
else if (args.Length == 2)
{
if (args[0].Length == 0)
{
Console.Error.WriteLine("File to upload can't be empty string.");
Environment.Exit(2);
}
if (args[1].Length == 0)
{
Console.Error.WriteLine("Destination to upload can't be empty string.");
Environment.Exit(2);
}
uploadFilePath = args[0];
uploadDestinationPath = args[1];
}
else
{
// Write to stderr
Console.Error.WriteLine("You can only specify one optional file and one optional destination to upload.");
Environment.Exit(2);
}
// Check the upload exists
if (!File.Exists(uploadFilePath))
{
Console.Error.WriteLine($"The file {uploadFilePath} doesn't exist.");
Environment.Exit(3);
}
// Check the upload file is not a directory
if (File.GetAttributes(uploadFilePath).HasFlag(FileAttributes.Directory))
{
Console.Error.WriteLine($"The file {uploadFilePath} is a directory.");
Environment.Exit(4);
}
// Get config from environment variables
var configNameList = new List<string>{
"CRUPEST_AUTO_BACKUP_COS_SECRET_ID",
"CRUPEST_AUTO_BACKUP_COS_SECRET_KEY",
"CRUPEST_AUTO_BACKUP_COS_REGION",
"CRUPEST_AUTO_BACKUP_BUCKET_NAME"
};
var config = new Dictionary<string, string>();
foreach (var configName in configNameList)
{
var configValue = Environment.GetEnvironmentVariable(configName);
if (configValue is null)
{
Console.Error.WriteLine($"Environment variable {configName} is required.");
Environment.Exit(5);
}
}
var cosConfig = new CosXmlConfig.Builder()
.IsHttps(true)
.SetRegion(config["CRUPEST_AUTO_BACKUP_COS_REGION"])
.Build();
QCloudCredentialProvider cosCredentialProvider =
new DefaultQCloudCredentialProvider(
config["CRUPEST_AUTO_BACKUP_COS_SECRET_ID"],
config["CRUPEST_AUTO_BACKUP_COS_SECRET_KEY"],
60
);
CosXml cosXml = new CosXmlServer(cosConfig, cosCredentialProvider);
TransferConfig transferConfig = new TransferConfig();
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
if (uploadDestinationPath is null)
{
var uploadFileName = Path.GetFileName(uploadFilePath);
var firstDotPosition = uploadFileName.IndexOf('.');
uploadDestinationPath = DateTime.Now.ToString("s");
if (firstDotPosition != -1)
{
uploadDestinationPath += uploadFileName.Substring(firstDotPosition + 1);
}
}
Console.WriteLine($"Upload file source: {uploadFilePath}");
Console.WriteLine($"Upload COS region: {config["CRUPEST_AUTO_BACKUP_COS_REGION"]}");
Console.WriteLine($"Upload bucket name: {config["CRUPEST_AUTO_BACKUP_BUCKET_NAME"]}");
Console.WriteLine($"Upload file destination: {uploadDestinationPath}");
// 上传对象
COSXMLUploadTask uploadTask = new COSXMLUploadTask(config["CRUPEST_AUTO_BACKUP_BUCKET_NAME"], uploadDestinationPath);
uploadTask.SetSrcPath(uploadFilePath);
uploadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
try
{
COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask);
Console.WriteLine("Upload completed!");
}
catch (Exception e)
{
Console.Error.WriteLine("CosException: " + e);
Environment.Exit(6);
}
|