Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 7 additions & 57 deletions FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,48 +224,24 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
}
}

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

try
{
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
using var memoryStream = new MemoryStream();
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);
var response = await client.GetObjectAsync(bucketName, path, cancellationToken);

return memoryStream.ToArray();
return response.ResponseStream;
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);
var path = GetPath(virtualPath);

try
{
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
using var memoryStream = new MemoryStream();
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);

using var streamReader = new StreamReader(memoryStream);
memoryStream.Position = 0;

return await streamReader.ReadToEndAsync();
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
{
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
{
Expand All @@ -276,37 +252,11 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b

try
{
using var memoryStream = new MemoryStream(contents);
var request = new PutObjectRequest
{
InputStream = memoryStream,
BucketName = bucketName,
Key = path
};

await client.PutObjectAsync(request, cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
contents = existingContents.Concat(contents).ToArray();
await DeleteFileAsync(virtualPath, cancellationToken);
contents.Seek(0, SeekOrigin.Begin);

var path = GetPath(virtualPath);

try
{
using var memoryStream = new MemoryStream(contents);
var request = new PutObjectRequest
{
InputStream = memoryStream,
InputStream = contents,
BucketName = bucketName,
Key = path
};
Expand All @@ -319,7 +269,7 @@ public override async Task AppendFileAsync(string virtualPath, byte[] contents,
}
}

private static Exception Exception(Exception exception)
protected override Exception Exception(Exception exception)
{
if (exception is FileSystemException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure;
Expand Down Expand Up @@ -203,46 +202,22 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
}
}

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

try
{
using var memoryStream = new MemoryStream();
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);

return memoryStream.ToArray();
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);
var path = GetPath(virtualPath);

try
{
using var memoryStream = new MemoryStream();
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);

using var streamReader = new StreamReader(memoryStream);
memoryStream.Position = 0;

return await streamReader.ReadToEndAsync();
return await client.GetBlobClient(path).OpenReadAsync(cancellationToken: cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

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

try
{
using var memoryStream = new MemoryStream(contents);

await client.UploadBlobAsync(path, memoryStream, cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
contents = existingContents.Concat(contents).ToArray();
await DeleteFileAsync(virtualPath, cancellationToken);

var path = GetPath(virtualPath);

try
{
using var memoryStream = new MemoryStream(contents);
contents.Seek(0, SeekOrigin.Begin);

await client.UploadBlobAsync(path, memoryStream, cancellationToken);
await client.UploadBlobAsync(path, contents, cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

private static Exception Exception(Exception exception)
protected override Exception Exception(Exception exception)
{
if (exception is FileSystemException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure;
Expand Down Expand Up @@ -185,7 +184,7 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
}
}

public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);

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

using var memoryStream = new MemoryStream();
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);

return memoryStream.ToArray();
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);

var path = GetPath(virtualPath);
var filePath = GetLastPathPart(path);
var directoryPath = GetParentPathPart(path);

try
{
var directory = client.GetDirectoryClient(directoryPath);
var file = directory.GetFileClient(filePath);
var download = await file.DownloadAsync(cancellationToken: cancellationToken);

using var memoryStream = new MemoryStream();
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);
using var streamReader = new StreamReader(memoryStream);
memoryStream.Position = 0;

return await streamReader.ReadToEndAsync();
return download.Value.Content;
}
catch (Exception exception)
{
throw Exception(exception);
}
}

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

using var memoryStream = new MemoryStream(contents);
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);

await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
{
await GetFileAsync(virtualPath, cancellationToken);
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);

var path = GetPath(virtualPath);
var filePath = GetLastPathPart(path);
var directoryPath = GetParentPathPart(path);

try
{
var directory = client.GetDirectoryClient(directoryPath);
var file = directory.GetFileClient(filePath);

contents = existingContents.Concat(contents).ToArray();

using var memoryStream = new MemoryStream(contents);

await file.DeleteAsync(cancellationToken);
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);
contents.Seek(0, SeekOrigin.Begin);

await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
await file.CreateAsync(contents.Length, cancellationToken: cancellationToken);
await file.UploadRangeAsync(new HttpRange(0, contents.Length), contents, cancellationToken: cancellationToken);
}
catch (Exception exception)
{
throw Exception(exception);
}
}

private static Exception Exception(Exception exception)
protected override Exception Exception(Exception exception)
{
if (exception is FileSystemException)
{
Expand Down
Loading