-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStorage.cs
More file actions
271 lines (232 loc) · 13 KB
/
Storage.cs
File metadata and controls
271 lines (232 loc) · 13 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging.Abstractions;
using Bytewizer.Backblaze.Models;
namespace Bytewizer.Backblaze.Client
{
/// <summary>
/// Represents a base implementation of the <see cref="Storage"/> which uses <see cref="ApiClient"/> for making HTTP requests.
/// </summary>
public abstract partial class Storage : DisposableObject, IStorageClient
{
#region Lifetime
/// <summary>
/// Initializes a new instance of the <see cref="Storage"/> class.
/// </summary>
protected Storage(HttpClient client, IClientOptions options, ILoggerFactory logger, IMemoryCache cache)
{
try
{
// Initialize components
logger ??= NullLoggerFactory.Instance;
_client = new ApiClient(client, options, logger.CreateLogger<ApiClient>(), cache);
_logger = logger.CreateLogger<Storage>();
}
catch (Exception ex)
{
//Log exception error
_logger.LogError(ex, ex.Message);
//Continue error
throw;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Storage"/> class.
/// </summary>
protected Storage(IApiClient client, ILogger logger)
{
try
{
// Initialize components
_client = client ?? throw new ArgumentNullException(nameof(client));
_logger = logger ?? NullLogger.Instance;
// Connect to the Backblaze B2 API server
//_client.Connect();
}
catch (Exception ex)
{
//Log exception error
_logger.LogError(ex, ex.Message);
//Continue error
throw;
}
}
#region IDisposable
/// <summary>
/// Frees resources owned by this instance.
/// </summary>
/// <param name="disposing">
/// True when called via <see cref="IDisposable.Dispose()"/>, false when called from the finalizer.
/// </param>
protected override void Dispose(bool disposing)
{
// Only managed resources to dispose
if (!disposing)
return;
// Dispose owned objects
_client?.Dispose();
}
#endregion IDisposable
#endregion
#region Private Fields
/// <summary>
/// The <see cref="ILogger"/> used for application logging.
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The <see cref="IApiClient"/> connected client to Backblaze B2 Cloud Storage.
/// </summary>
private readonly IApiClient _client;
#endregion
#region Public Properties
/// <summary>
/// The identifier for the account.
/// </summary>
public string AccountId => _client?.AccountInfo.AccountId;
/// <summary>
/// The cancellation token associated with this <see cref="ApiClient"/> instance.
/// </summary>
public CancellationToken CancellationToken
{
get { return _cancellationToken; }
set { _cancellationToken = (value == null) ? CancellationToken.None : value; }
}
private CancellationToken _cancellationToken = CancellationToken.None;
#endregion
#region Public Methods
/// <summary>
/// Connect to Backblaze B2 Cloud Storage and initialize <see cref="AccountInfo"/>.
/// </summary>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public AuthorizeAccountResponse Connect() => _client.Connect();
/// <summary>
/// Connect to Backblaze B2 Cloud Storage and initialize <see cref="AccountInfo"/>.
/// </summary>
/// <param name="keyId">The identifier for the key.</param>
/// <param name="applicationKey">The secret part of the key. You can use either the master application key or a normal application key.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public AuthorizeAccountResponse Connect(string keyId, string applicationKey) => _client.Connect(keyId, applicationKey);
/// <summary>
/// Connect to Backblaze B2 Cloud Storage and initialize <see cref="AccountInfo"/>.
/// </summary>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public Task<AuthorizeAccountResponse> ConnectAsync() => _client.ConnectAsync();
/// <summary>
/// Connect to Backblaze B2 Cloud Storage and initialize <see cref="AccountInfo"/>.
/// </summary>
/// <param name="keyId">The identifier for the key.</param>
/// <param name="applicationKey">The secret part of the key. You can use either the master application key or a normal application key.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public Task<AuthorizeAccountResponse> ConnectAsync(string keyId, string applicationKey) => _client.ConnectAsync(keyId, applicationKey);
#region UploadAsync
/// <summary>
/// Upload content stream to Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="bucketId">The bucket id you want to upload to.</param>
/// <param name="fileName">The name of the file.</param>
/// <param name="content"> The content stream of the content payload.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<UploadFileResponse>> UploadAsync
(string bucketId, string fileName, Stream content)
{
var request = new UploadFileByBucketIdRequest(bucketId, fileName);
return await UploadAsync(request, content, null, _cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Upload content stream to Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="request">The <see cref="UploadFileRequest"/> to send.</param>
/// <param name="content"> The content stream of the content payload.</param>
/// <param name="progress">A progress action which fires every time the write buffer is cycled.</param>
/// <param name="cancel">The cancellation token to cancel operation.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<UploadFileResponse>> UploadAsync
(UploadFileByBucketIdRequest request, Stream content, IProgress<ICopyProgress> progress, CancellationToken cancel)
{
return await _client.UploadAsync(request, content, progress, cancel).ConfigureAwait(false);
}
#endregion
#region DownloadAsync
/// <summary>
/// Download a specific version of content by bucket and file name from Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="bucketName">The unique name of the bucket the file is in.</param>
/// <param name="fileName">The name of the file to download.</param>
/// <param name="content">The download content to receive.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<DownloadFileResponse>> DownloadAsync
(string bucketName, string fileName, Stream content)
{
var request = new DownloadFileByNameRequest(bucketName, fileName);
return await DownloadAsync(request, content, null, _cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Download a specific version of content by bucket and file name from Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="request">The <see cref="DownloadFileByIdRequest"/> to send.</param>
/// <param name="content">The download content to receive.</param>
/// <param name="progress">A progress action which fires every time the write buffer is cycled.</param>
/// <param name="cancel">The cancellation token to cancel operation.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<DownloadFileResponse>> DownloadAsync
(DownloadFileByNameRequest request, Stream content, IProgress<ICopyProgress> progress, CancellationToken cancel)
{
return await _client.DownloadAsync(request, content, progress, cancel).ConfigureAwait(false);
}
/// <summary>
/// Download a specific version of content by file id from Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="fileId">The unique id of the file to download.</param>
/// <param name="content">The download content to receive.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<DownloadFileResponse>> DownloadByIdAsync
(string fileId, Stream content)
{
var request = new DownloadFileByIdRequest(fileId);
return await DownloadByIdAsync(request, content, null, _cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Download a specific version of content by file id from Backblaze B2 Cloud Storage.
/// </summary>
/// <param name="request">The <see cref="DownloadFileByIdRequest"/> to send.</param>
/// <param name="content">The download content to receive.</param>
/// <param name="progress">A progress action which fires every time the write buffer is cycled.</param>
/// <param name="cancel">The cancellation token to cancel operation.</param>
/// <exception cref="AuthenticationException">Thrown when authentication fails.</exception>
/// <exception cref="CapExceededExecption">Thrown when a cap is exceeded or an account in bad standing.</exception>
/// <exception cref="InvalidHashException">Thrown when a checksum hash is not valid.</exception>
/// <exception cref="ApiException">Thrown when an error occurs during client operation.</exception>
public async Task<IApiResults<DownloadFileResponse>> DownloadByIdAsync
(DownloadFileByIdRequest request, Stream content, IProgress<ICopyProgress> progress, CancellationToken cancel)
{
return await _client.DownloadByIdAsync(request, content, progress, cancel).ConfigureAwait(false);
}
#endregion
#endregion
}
}