Skip to content

Commit

Permalink
Updated GetFileStreamAsync impl
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Jan 4, 2024
1 parent a7beda3 commit 3fbee0d
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/Foundatio.AWS/Storage/S3FileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class S3FileStorage : IFileStorage {
public S3FileStorage(S3FileStorageOptions options) {
if (options == null)
throw new ArgumentNullException(nameof(options));

_serializer = options.Serializer ?? DefaultSerializer.Instance;
_logger = options.LoggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance;

_bucket = options.Bucket;
_useChunkEncoding = options.UseChunkEncoding ?? true;
_cannedAcl = options.CannedACL;
Expand All @@ -56,22 +56,30 @@ public S3FileStorage(Builder<S3FileStorageOptionsBuilder, S3FileStorageOptions>
: this(builder(new S3FileStorageOptionsBuilder()).Build()) { }

ISerializer IHaveSerializer.Serializer => _serializer;

public AmazonS3Client Client => _client;
public string Bucket => _bucket;
public S3CannedACL CannedACL => _cannedAcl;

public async Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default) {
[Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(FileAccess)} instead to define read or write behaviour of stream")]
public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
=> GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

public async Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default)

Check failure on line 68 in src/Foundatio.AWS/Storage/S3FileStorage.cs

View workflow job for this annotation

GitHub Actions / build / build

The type or namespace name 'StreamMode' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 68 in src/Foundatio.AWS/Storage/S3FileStorage.cs

View workflow job for this annotation

GitHub Actions / build / build

The type or namespace name 'StreamMode' could not be found (are you missing a using directive or an assembly reference?)
{
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

if (streamMode is StreamMode.Write)
throw new NotSupportedException($"Stream mode {streamMode} is not supported.");

var req = new GetObjectRequest {
BucketName = _bucket,
Key = NormalizePath(path)
};

_logger.LogTrace("Getting file stream for {Path}", req.Key);

var response = await _client.GetObjectAsync(req, cancellationToken).AnyContext();
if (!response.HttpStatusCode.IsSuccessful()) {
_logger.LogError("[{HttpStatusCode}] Unable to get file stream for {Path}", response.HttpStatusCode, req.Key);
Expand Down Expand Up @@ -161,7 +169,7 @@ public async Task<bool> ExistsAsync(string path) {
InputStream = stream.CanSeek ? stream : AmazonS3Util.MakeStreamSeekable(stream),
UseChunkEncoding = _useChunkEncoding
};

_logger.LogTrace("Saving {Path}", req.Key);
var response = await _client.PutObjectAsync(req, cancellationToken).AnyContext();
return response.HttpStatusCode.IsSuccessful();
Expand Down Expand Up @@ -199,7 +207,7 @@ public async Task<bool> ExistsAsync(string path) {
_logger.LogError("[{HttpStatusCode}] Unable to delete renamed {Path}", deleteResponse.HttpStatusCode, deleteRequest.Key);
return false;
}

return true;
}

Expand Down Expand Up @@ -255,7 +263,7 @@ public async Task<bool> ExistsAsync(string path) {
continue;

deleteRequest.Objects.AddRange(keys);

_logger.LogInformation("Deleting {FileCount} files matching {SearchPattern}", keys.Length, searchPattern);
var deleteResponse = await _client.DeleteObjectsAsync(deleteRequest, cancellationToken).AnyContext();
if (deleteResponse.DeleteErrors.Count > 0) {
Expand Down Expand Up @@ -301,10 +309,10 @@ private async Task<NextPageResult> GetFiles(SearchCriteria criteria, int pageSiz
};

_logger.LogTrace(
s => s.Property("Limit", req.MaxKeys),
s => s.Property("Limit", req.MaxKeys),
"Getting file list matching {Prefix} and {Pattern}...", criteria.Prefix, criteria.Pattern
);

var response = await _client.ListObjectsV2Async(req, cancellationToken).AnyContext();
return new NextPageResult {
Success = response.HttpStatusCode.IsSuccessful(),
Expand All @@ -317,7 +325,7 @@ private async Task<NextPageResult> GetFiles(SearchCriteria criteria, int pageSiz
private string NormalizePath(string path) {
return path?.Replace('\\', '/');
}

private class SearchCriteria {
public string Prefix { get; set; }
public Regex Pattern { get; set; }
Expand All @@ -326,14 +334,14 @@ private class SearchCriteria {
private SearchCriteria GetRequestCriteria(string searchPattern) {
if (String.IsNullOrEmpty(searchPattern))
return new SearchCriteria { Prefix = String.Empty };

string normalizedSearchPattern = NormalizePath(searchPattern);
int wildcardPos = normalizedSearchPattern.IndexOf('*');
bool hasWildcard = wildcardPos >= 0;

string prefix = normalizedSearchPattern;
Regex patternRegex = null;

if (hasWildcard) {
patternRegex = new Regex($"^{Regex.Escape(normalizedSearchPattern).Replace("\\*", ".*?")}$");
int slashPos = normalizedSearchPattern.LastIndexOf('/');
Expand Down

0 comments on commit 3fbee0d

Please sign in to comment.