|
@@ -8,6 +8,7 @@ using System.Linq;
|
8
|
8
|
using System.Net;
|
9
|
9
|
using System.Security.Cryptography;
|
10
|
10
|
using System.Text;
|
|
11
|
+using System.Threading;
|
11
|
12
|
using System.Threading.Tasks;
|
12
|
13
|
|
13
|
14
|
namespace B2Uploader
|
|
@@ -79,8 +80,31 @@ namespace B2Uploader
|
79
|
80
|
}
|
80
|
81
|
else
|
81
|
82
|
{
|
82
|
|
- var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
|
83
|
|
- var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
|
|
83
|
+ bool uploaded = false;
|
|
84
|
+ int retries = 0;
|
|
85
|
+ while (!uploaded && retries < 3)
|
|
86
|
+ {
|
|
87
|
+ try {
|
|
88
|
+ var uploadURL = GetUploadURL(new GetUploadURLRequest { bucketId = bucket.bucketId }, auth.apiUrl, auth.authorizationToken);
|
|
89
|
+ var response = UploadFile(uploadURL.authorizationToken, "b2/x-auto", s, uploadURL.uploadUrl);
|
|
90
|
+ if(response != null)
|
|
91
|
+ {
|
|
92
|
+ uploaded = true;
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+ catch(Exception ex)
|
|
96
|
+ {
|
|
97
|
+ Console.WriteLine("Uploaded Failed. Retrying");
|
|
98
|
+ Console.WriteLine(ex.Message);
|
|
99
|
+ uploaded = false;
|
|
100
|
+ retries += 1;
|
|
101
|
+ Thread.Sleep(TimeSpan.FromSeconds(30));
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+ if (!uploaded)
|
|
105
|
+ {
|
|
106
|
+ Console.WriteLine("Uploaded Failed 3 times... Please retry later!");
|
|
107
|
+ }
|
84
|
108
|
}
|
85
|
109
|
});
|
86
|
110
|
return 1;
|
|
@@ -109,7 +133,6 @@ namespace B2Uploader
|
109
|
133
|
|
110
|
134
|
string responseString = MakeRequest(apiUrl + "/b2api/v1/b2_list_buckets", headers, JsonConvert.SerializeObject(request));
|
111
|
135
|
|
112
|
|
-
|
113
|
136
|
return JsonConvert.DeserializeObject<ListBucketsResponse>(responseString);
|
114
|
137
|
}
|
115
|
138
|
|
|
@@ -140,8 +163,7 @@ namespace B2Uploader
|
140
|
163
|
|
141
|
164
|
static UploadFileResponse UploadFile(string authToken, string contentType, string filePath, string uploadUrl)
|
142
|
165
|
{
|
143
|
|
- FileStream fs = System.IO.File.OpenRead(filePath);
|
144
|
|
- String sha1 = GetSha1(fs);
|
|
166
|
+ String sha1 = GetSha1(filePath);
|
145
|
167
|
|
146
|
168
|
var headers = GetAuthHeaders(authToken);
|
147
|
169
|
|
|
@@ -150,19 +172,23 @@ namespace B2Uploader
|
150
|
172
|
headers.Add(new Tuple<string, string>("X-Bz-File-Name", fileName));
|
151
|
173
|
headers.Add(new Tuple<string, string>("X-Bz-Content-Sha1", sha1));
|
152
|
174
|
|
153
|
|
- string responseString = MakeRequest(uploadUrl, headers, fs, contentType);
|
|
175
|
+ using (FileStream fs = System.IO.File.OpenRead(filePath))
|
|
176
|
+ {
|
154
|
177
|
|
155
|
|
- var resp = JsonConvert.DeserializeObject<UploadFileResponse>(responseString);
|
|
178
|
+ string responseString = MakeRequest(uploadUrl, headers, fs, contentType);
|
156
|
179
|
|
157
|
|
- if(resp.contentSha1 == sha1)
|
158
|
|
- {
|
159
|
|
- Console.WriteLine(responseString);
|
160
|
|
- return resp;
|
161
|
|
- }
|
162
|
|
- else
|
163
|
|
- {
|
164
|
|
- //something went wrong!
|
165
|
|
- return null;
|
|
180
|
+ var resp = JsonConvert.DeserializeObject<UploadFileResponse>(responseString);
|
|
181
|
+
|
|
182
|
+ if (resp.contentSha1 == sha1)
|
|
183
|
+ {
|
|
184
|
+ Console.WriteLine(responseString);
|
|
185
|
+ return resp;
|
|
186
|
+ }
|
|
187
|
+ else
|
|
188
|
+ {
|
|
189
|
+ //something went wrong!
|
|
190
|
+ return null;
|
|
191
|
+ }
|
166
|
192
|
}
|
167
|
193
|
}
|
168
|
194
|
|
|
@@ -224,19 +250,22 @@ namespace B2Uploader
|
224
|
250
|
|
225
|
251
|
|
226
|
252
|
|
227
|
|
- private static string GetSha1(Stream inputStream)
|
|
253
|
+ private static string GetSha1(string fileName)
|
228
|
254
|
{
|
229
|
255
|
using (SHA1Managed sha1 = new SHA1Managed())
|
230
|
256
|
{
|
231
|
|
- var hash = sha1.ComputeHash(inputStream);
|
232
|
|
- var sb = new StringBuilder(hash.Length * 2);
|
233
|
|
-
|
234
|
|
- foreach (byte b in hash)
|
|
257
|
+ using (FileStream fs = System.IO.File.OpenRead(fileName))
|
235
|
258
|
{
|
236
|
|
- // can be "x2" if you want lowercase
|
237
|
|
- sb.Append(b.ToString("X2"));
|
|
259
|
+ var hash = sha1.ComputeHash(fs);
|
|
260
|
+ var sb = new StringBuilder(hash.Length * 2);
|
|
261
|
+
|
|
262
|
+ foreach (byte b in hash)
|
|
263
|
+ {
|
|
264
|
+ // can be "x2" if you want lowercase
|
|
265
|
+ sb.Append(b.ToString("X2"));
|
|
266
|
+ }
|
|
267
|
+ return sb.ToString();
|
238
|
268
|
}
|
239
|
|
- return sb.ToString();
|
240
|
269
|
}
|
241
|
270
|
}
|
242
|
271
|
}
|