|
@@ -0,0 +1,203 @@
|
|
1
|
+using Newtonsoft.Json;
|
|
2
|
+using System;
|
|
3
|
+using System.Collections.Generic;
|
|
4
|
+using System.IO;
|
|
5
|
+using System.Linq;
|
|
6
|
+using System.Net;
|
|
7
|
+using System.Security.Cryptography;
|
|
8
|
+using System.Text;
|
|
9
|
+using System.Threading.Tasks;
|
|
10
|
+
|
|
11
|
+namespace B2Uploader
|
|
12
|
+{
|
|
13
|
+ class Program
|
|
14
|
+ {
|
|
15
|
+ static void Main(string[] args)
|
|
16
|
+ {
|
|
17
|
+ if(args.Count() != 3)
|
|
18
|
+ {
|
|
19
|
+ Console.WriteLine("Need accountID, AppKey and Folder to upload");
|
|
20
|
+ return;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ if (!Directory.Exists(args[2]))
|
|
24
|
+ {
|
|
25
|
+ Console.WriteLine("Directory to upload MUST EXIST!");
|
|
26
|
+ return;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ var auth = AuthorizeUser(args[0], args[1]);
|
|
30
|
+ var buckets = ListBuckets(new ListBucketsRequest() { accountId = auth.accountId }, auth.authorizationToken, auth.apiUrl);
|
|
31
|
+
|
|
32
|
+ var bucket = buckets.buckets.First();
|
|
33
|
+
|
|
34
|
+ foreach(string s in Directory.GetFiles(args[2]))
|
|
35
|
+ {
|
|
36
|
+ var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.authorizationToken, auth.apiUrl);
|
|
37
|
+ var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ static AuthorizeResponse AuthorizeUser(string accountId, string applicationKey)
|
|
45
|
+ {
|
|
46
|
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.backblaze.com/b2api/v1/b2_authorize_account");
|
|
47
|
+ string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(accountId + ":" + applicationKey));
|
|
48
|
+ webRequest.Headers.Add("Authorization", "Basic " + credentials);
|
|
49
|
+ webRequest.ContentType = "application/json; charset=utf-8";
|
|
50
|
+ WebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
|
51
|
+ var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
52
|
+ response.Close();
|
|
53
|
+ return JsonConvert.DeserializeObject<AuthorizeResponse>(responseString);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ static ListBucketsResponse ListBuckets(ListBucketsRequest request, string authToken, string apiURL)
|
|
57
|
+ {
|
|
58
|
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiURL + "/b2api/v1/b2_list_buckets");
|
|
59
|
+ string body = JsonConvert.SerializeObject(request);
|
|
60
|
+ var data = Encoding.UTF8.GetBytes(body);
|
|
61
|
+ webRequest.Method = "POST";
|
|
62
|
+ webRequest.Headers.Add("Authorization", authToken);
|
|
63
|
+ webRequest.ContentType = "application/json; charset=utf-8";
|
|
64
|
+ webRequest.ContentLength = data.Length;
|
|
65
|
+ using (var stream = webRequest.GetRequestStream())
|
|
66
|
+ {
|
|
67
|
+ stream.Write(data, 0, data.Length);
|
|
68
|
+ stream.Close();
|
|
69
|
+ }
|
|
70
|
+ WebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
|
71
|
+ var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
72
|
+ response.Close();
|
|
73
|
+
|
|
74
|
+ return JsonConvert.DeserializeObject<ListBucketsResponse>(responseString);
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ static GetUploadURLResponse GetUploadURL(GetUploadURLRequest request, string apiUrl, string authToken)
|
|
78
|
+ {
|
|
79
|
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(apiUrl + "/b2api/v1/b2_get_upload_url");
|
|
80
|
+ string body = JsonConvert.SerializeObject(request);
|
|
81
|
+ var data = Encoding.UTF8.GetBytes(body);
|
|
82
|
+ webRequest.Method = "POST";
|
|
83
|
+ webRequest.Headers.Add("Authorization", authToken);
|
|
84
|
+ webRequest.ContentType = "application/json; charset=utf-8";
|
|
85
|
+ webRequest.ContentLength = data.Length;
|
|
86
|
+ using (var stream = webRequest.GetRequestStream())
|
|
87
|
+ {
|
|
88
|
+ stream.Write(data, 0, data.Length);
|
|
89
|
+ stream.Close();
|
|
90
|
+ }
|
|
91
|
+ WebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
|
92
|
+ var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
93
|
+ response.Close();
|
|
94
|
+ return JsonConvert.DeserializeObject<GetUploadURLResponse>(responseString);
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ static UploadFileResponse UploadFile(string authToken, string contentType, string filePath, string uploadUrl)
|
|
98
|
+ {
|
|
99
|
+ SHA1 sha = SHA1.Create();
|
|
100
|
+
|
|
101
|
+ byte[] bytes = File.ReadAllBytes(filePath);
|
|
102
|
+
|
|
103
|
+ String sha1 = ASCIIEncoding.ASCII.GetString(sha.ComputeHash(bytes));
|
|
104
|
+
|
|
105
|
+ HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
|
|
106
|
+ webRequest.Method = "POST";
|
|
107
|
+ webRequest.Headers.Add("Authorization", authToken);
|
|
108
|
+ webRequest.Headers.Add("X-Bz-File-Name", filePath.Replace('\\','_'));
|
|
109
|
+ webRequest.Headers.Add("X-Bz-Content-Sha1", sha1);
|
|
110
|
+ webRequest.ContentType = contentType;
|
|
111
|
+ using (var stream = webRequest.GetRequestStream())
|
|
112
|
+ {
|
|
113
|
+ stream.Write(bytes, 0, bytes.Length);
|
|
114
|
+ stream.Close();
|
|
115
|
+ }
|
|
116
|
+ WebResponse response = (HttpWebResponse)webRequest.GetResponse();
|
|
117
|
+ var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
118
|
+ response.Close();
|
|
119
|
+ var resp = JsonConvert.DeserializeObject<UploadFileResponse>(responseString);
|
|
120
|
+
|
|
121
|
+ if(resp.contentSha1 == sha1)
|
|
122
|
+ {
|
|
123
|
+ Console.WriteLine(responseString);
|
|
124
|
+ return resp;
|
|
125
|
+ }
|
|
126
|
+ else
|
|
127
|
+ {
|
|
128
|
+ //something went wrong!
|
|
129
|
+ return null;
|
|
130
|
+ }
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ public class AuthorizeResponse
|
|
135
|
+ {
|
|
136
|
+ public string accountId { get; set; }
|
|
137
|
+ public string apiUrl { get; set; }
|
|
138
|
+ public string authorizationToken { get; set; }
|
|
139
|
+ public string downloadUrl { get; set; }
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ public class CreateBucketResponse
|
|
143
|
+ {
|
|
144
|
+ public string bucketId { get; set; }
|
|
145
|
+ public string accountId { get; set; }
|
|
146
|
+ public string bucketName { get; set; }
|
|
147
|
+ public string bucketType { get; set; }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ public class CreateBucketRequest
|
|
151
|
+ {
|
|
152
|
+ public string accountId { get; set; }
|
|
153
|
+ public string bucketName { get; set; }
|
|
154
|
+ public string bucketType { get; set; }
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ public class GetUploadURLRequest
|
|
158
|
+ {
|
|
159
|
+ public string bucketId { get; set; }
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ public class GetUploadURLResponse
|
|
163
|
+ {
|
|
164
|
+ public string bucketId { get; set; }
|
|
165
|
+ public string uploadUrl { get; set; }
|
|
166
|
+ public string authorizationToken { get; set; }
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ public class ListBucketsRequest
|
|
170
|
+ {
|
|
171
|
+ public string accountId { get; set; }
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ public class ListBucketsResponse
|
|
175
|
+ {
|
|
176
|
+ public List<Bucket> buckets { get; set; }
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ public class Bucket
|
|
180
|
+ {
|
|
181
|
+ public string bucketId { get; set; }
|
|
182
|
+ public string accountId { get; set; }
|
|
183
|
+ public string bucketName { get; set; }
|
|
184
|
+ public string bucketType { get; set; }
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ public class FileInfo
|
|
188
|
+ {
|
|
189
|
+ public string author { get; set; }
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ public class UploadFileResponse
|
|
193
|
+ {
|
|
194
|
+ public string fileId { get; set; }
|
|
195
|
+ public string fileName { get; set; }
|
|
196
|
+ public string accountId { get; set; }
|
|
197
|
+ public string bucketId { get; set; }
|
|
198
|
+ public int contentLength { get; set; }
|
|
199
|
+ public string contentSha1 { get; set; }
|
|
200
|
+ public string contentType { get; set; }
|
|
201
|
+ public FileInfo fileInfo { get; set; }
|
|
202
|
+ }
|
|
203
|
+}
|