Skip to content

Commit 82e4d9b

Browse files
committed
add stream support
1 parent 8a6dde3 commit 82e4d9b

14 files changed

Lines changed: 337 additions & 387 deletions

File tree

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -224,48 +224,24 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
224224
}
225225
}
226226

227-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
227+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
228228
{
229229
await GetFileAsync(virtualPath, cancellationToken);
230230
var path = GetPath(virtualPath);
231231

232232
try
233233
{
234-
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
235-
using var memoryStream = new MemoryStream();
236-
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);
234+
var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
237235

238-
return memoryStream.ToArray();
236+
return response.ResponseStream;
239237
}
240238
catch (Exception exception)
241239
{
242240
throw Exception(exception);
243241
}
244242
}
245243

246-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
247-
{
248-
await GetFileAsync(virtualPath, cancellationToken);
249-
var path = GetPath(virtualPath);
250-
251-
try
252-
{
253-
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
254-
using var memoryStream = new MemoryStream();
255-
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);
256-
257-
using var streamReader = new StreamReader(memoryStream);
258-
memoryStream.Position = 0;
259-
260-
return await streamReader.ReadToEndAsync();
261-
}
262-
catch (Exception exception)
263-
{
264-
throw Exception(exception);
265-
}
266-
}
267-
268-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
244+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
269245
{
270246
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
271247
{
@@ -276,37 +252,11 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
276252

277253
try
278254
{
279-
using var memoryStream = new MemoryStream(contents);
280-
var request = new PutObjectRequest
281-
{
282-
InputStream = memoryStream,
283-
BucketName = bucketName,
284-
Key = path
285-
};
286-
287-
await client.PutObjectAsync(request, cancellationToken);
288-
}
289-
catch (Exception exception)
290-
{
291-
throw Exception(exception);
292-
}
293-
}
294-
295-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
296-
{
297-
await GetFileAsync(virtualPath, cancellationToken);
298-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
299-
contents = existingContents.Concat(contents).ToArray();
300-
await DeleteFileAsync(virtualPath, cancellationToken);
255+
contents.Seek(0, SeekOrigin.Begin);
301256

302-
var path = GetPath(virtualPath);
303-
304-
try
305-
{
306-
using var memoryStream = new MemoryStream(contents);
307257
var request = new PutObjectRequest
308258
{
309-
InputStream = memoryStream,
259+
InputStream = contents,
310260
BucketName = bucketName,
311261
Key = path
312262
};
@@ -319,7 +269,7 @@ public override async Task AppendFileAsync(string virtualPath, byte[] contents,
319269
}
320270
}
321271

322-
private static Exception Exception(Exception exception)
272+
protected override Exception Exception(Exception exception)
323273
{
324274
if (exception is FileSystemException)
325275
{

FileSystem.Adapters.AzureBlobStorage/src/AzureBlobStorageAdapter.cs

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Azure;
@@ -203,46 +202,22 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
203202
}
204203
}
205204

206-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
205+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
207206
{
208207
await GetFileAsync(virtualPath, cancellationToken);
209208
var path = GetPath(virtualPath);
210209

211210
try
212211
{
213-
using var memoryStream = new MemoryStream();
214-
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);
215-
216-
return memoryStream.ToArray();
217-
}
218-
catch (Exception exception)
219-
{
220-
throw Exception(exception);
221-
}
222-
}
223-
224-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
225-
{
226-
await GetFileAsync(virtualPath, cancellationToken);
227-
var path = GetPath(virtualPath);
228-
229-
try
230-
{
231-
using var memoryStream = new MemoryStream();
232-
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);
233-
234-
using var streamReader = new StreamReader(memoryStream);
235-
memoryStream.Position = 0;
236-
237-
return await streamReader.ReadToEndAsync();
212+
return await client.GetBlobClient(path).OpenReadAsync(cancellationToken: cancellationToken);
238213
}
239214
catch (Exception exception)
240215
{
241216
throw Exception(exception);
242217
}
243218
}
244219

245-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
220+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
246221
{
247222
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
248223
{
@@ -253,38 +228,17 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
253228

254229
try
255230
{
256-
using var memoryStream = new MemoryStream(contents);
257-
258-
await client.UploadBlobAsync(path, memoryStream, cancellationToken);
259-
}
260-
catch (Exception exception)
261-
{
262-
throw Exception(exception);
263-
}
264-
}
265-
266-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
267-
{
268-
await GetFileAsync(virtualPath, cancellationToken);
269-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
270-
contents = existingContents.Concat(contents).ToArray();
271-
await DeleteFileAsync(virtualPath, cancellationToken);
272-
273-
var path = GetPath(virtualPath);
274-
275-
try
276-
{
277-
using var memoryStream = new MemoryStream(contents);
231+
contents.Seek(0, SeekOrigin.Begin);
278232

279-
await client.UploadBlobAsync(path, memoryStream, cancellationToken);
233+
await client.UploadBlobAsync(path, contents, cancellationToken);
280234
}
281235
catch (Exception exception)
282236
{
283237
throw Exception(exception);
284238
}
285239
}
286240

287-
private static Exception Exception(Exception exception)
241+
protected override Exception Exception(Exception exception)
288242
{
289243
if (exception is FileSystemException)
290244
{

FileSystem.Adapters.AzureFileStorage/src/AzureFileStorageAdapter.cs

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Azure;
@@ -185,7 +184,7 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
185184
}
186185
}
187186

188-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
187+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
189188
{
190189
await GetFileAsync(virtualPath, cancellationToken);
191190

@@ -198,45 +197,15 @@ public override async Task<byte[]> ReadFileAsync(string virtualPath, Cancellatio
198197
var directory = client.GetDirectoryClient(directoryPath);
199198
var download = await directory.GetFileClient(filePath).DownloadAsync(cancellationToken: cancellationToken);
200199

201-
using var memoryStream = new MemoryStream();
202-
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);
203-
204-
return memoryStream.ToArray();
205-
}
206-
catch (Exception exception)
207-
{
208-
throw Exception(exception);
209-
}
210-
}
211-
212-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
213-
{
214-
await GetFileAsync(virtualPath, cancellationToken);
215-
216-
var path = GetPath(virtualPath);
217-
var filePath = GetLastPathPart(path);
218-
var directoryPath = GetParentPathPart(path);
219-
220-
try
221-
{
222-
var directory = client.GetDirectoryClient(directoryPath);
223-
var file = directory.GetFileClient(filePath);
224-
var download = await file.DownloadAsync(cancellationToken: cancellationToken);
225-
226-
using var memoryStream = new MemoryStream();
227-
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);
228-
using var streamReader = new StreamReader(memoryStream);
229-
memoryStream.Position = 0;
230-
231-
return await streamReader.ReadToEndAsync();
200+
return download.Value.Content;
232201
}
233202
catch (Exception exception)
234203
{
235204
throw Exception(exception);
236205
}
237206
}
238207

239-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
208+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
240209
{
241210
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
242211
{
@@ -253,47 +222,18 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
253222
await directory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
254223
var file = directory.GetFileClient(filePath);
255224

256-
using var memoryStream = new MemoryStream(contents);
257-
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);
258-
259-
await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
260-
}
261-
catch (Exception exception)
262-
{
263-
throw Exception(exception);
264-
}
265-
}
266-
267-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
268-
{
269-
await GetFileAsync(virtualPath, cancellationToken);
270-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
271-
272-
var path = GetPath(virtualPath);
273-
var filePath = GetLastPathPart(path);
274-
var directoryPath = GetParentPathPart(path);
275-
276-
try
277-
{
278-
var directory = client.GetDirectoryClient(directoryPath);
279-
var file = directory.GetFileClient(filePath);
280-
281-
contents = existingContents.Concat(contents).ToArray();
282-
283-
using var memoryStream = new MemoryStream(contents);
284-
285-
await file.DeleteAsync(cancellationToken);
286-
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);
225+
contents.Seek(0, SeekOrigin.Begin);
287226

288-
await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
227+
await file.CreateAsync(contents.Length, cancellationToken: cancellationToken);
228+
await file.UploadRangeAsync(new HttpRange(0, contents.Length), contents, cancellationToken: cancellationToken);
289229
}
290230
catch (Exception exception)
291231
{
292232
throw Exception(exception);
293233
}
294234
}
295235

296-
private static Exception Exception(Exception exception)
236+
protected override Exception Exception(Exception exception)
297237
{
298238
if (exception is FileSystemException)
299239
{

0 commit comments

Comments
 (0)