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
80 changes: 61 additions & 19 deletions MergerLogic/Clients/S3Client.cs
Comment thread
razbroc marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Amazon.S3.Model;
using MergerLogic.Batching;
using MergerLogic.DataTypes;
using MergerLogic.ImageProcessing;
using MergerLogic.Utils;
using Microsoft.Extensions.Logging;
using System.Reflection;
Expand All @@ -27,6 +28,21 @@
this._storageClass = new S3StorageClass(storageClass ?? S3StorageClass.Standard);
}

private bool IsKeyError(Exception e)
{
if (e is AmazonS3Exception ex)
{
return ex.ErrorCode == "NoSuchKey";
}

if (e.InnerException is AmazonS3Exception en)
{
return en.ErrorCode == "NoSuchKey";
}

return false;
}

private byte[]? GetImageBytes(string key)
{
string? methodName = MethodBase.GetCurrentMethod()?.Name;
Expand All @@ -52,63 +68,74 @@
}
catch (AggregateException e)
{
string message = $"exception while getting key {key}, Message: {e.Message}";
this._logger.LogError($"[{methodName}] {message}");
throw new Exception(message, e);
if (IsKeyError(e))
{
string message = $"exception while getting key {key}, Message: {e.Message}";
this._logger.LogDebug($"[{methodName}] {message}");
return null;
}
// In case there are other errors such as connection to S3
throw e;
}
}

public override Tile? GetTile(int z, int x, int y)
Comment thread
razbroc marked this conversation as resolved.
{
string methodName = MethodBase.GetCurrentMethod().Name;

Check warning on line 84 in MergerLogic/Clients/S3Client.cs

View workflow job for this annotation

GitHub Actions / Run Tests (6.0.x)

Dereference of a possibly null reference.
this._logger.LogDebug($"[{methodName}] start z: {z}, x: {x}, y: {y}");
var key = this.GetTileKey(z, x, y);
if (key == null)
string keyPrefix = this._pathUtils.GetTilePath(this.path, z, x, y, TileFormat.Jpeg, true);

byte[]? imageBytes = this.GetImageBytes(keyPrefix);
if (imageBytes == null)
{
this._logger.LogDebug($"[{methodName}] tileKey is null for z: {z}, x: {x}, y: {y}");
return null;
keyPrefix = this._pathUtils.GetTilePath(this.path, z, x, y, TileFormat.Png, true);
Comment thread
shimoncohen marked this conversation as resolved.
imageBytes = this.GetImageBytes(keyPrefix);
if (imageBytes == null)
{
return null;
}
}

byte[]? imageBytes = this.GetImageBytes(key);
this._logger.LogDebug($"[{methodName}] end z: {z}, x: {x}, y: {y}");
return this.CreateTile(z, x, y, imageBytes);
}

public Tile? GetTile(string key)
{
string methodName = MethodBase.GetCurrentMethod().Name;

Check warning on line 105 in MergerLogic/Clients/S3Client.cs

View workflow job for this annotation

GitHub Actions / Run Tests (6.0.x)

Dereference of a possibly null reference.
this._logger.LogDebug($"[{methodName}] start key: {key}");
Coord coords = this._pathUtils.FromPath(key, true);
byte[]? imageBytes = this.GetImageBytes(key);
if (imageBytes == null)
{
return null;
}

this._logger.LogDebug($"[{methodName}] end key: {key}");
Coord coords = this._pathUtils.FromPath(key, true);
Comment thread
razbroc marked this conversation as resolved.
return this.CreateTile(coords, imageBytes);
}

public override bool TileExists(int z, int x, int y)
{
string methodName = MethodBase.GetCurrentMethod().Name;

Check warning on line 120 in MergerLogic/Clients/S3Client.cs

View workflow job for this annotation

GitHub Actions / Run Tests (6.0.x)

Dereference of a possibly null reference.
this._logger.LogDebug($"[{methodName}] start z: {z}, x: {x}, y: {y}");
bool isExists = this.GetTileKey(z, x, y) != null;
bool exists = this.GetTileKey(z, x, y) != null;
this._logger.LogDebug($"[{methodName}] end z: {z}, x: {x}, y: {y}");
return isExists;
return exists;
}

public void UpdateTile(Tile tile)
{
string methodName = MethodBase.GetCurrentMethod().Name;

Check warning on line 129 in MergerLogic/Clients/S3Client.cs

View workflow job for this annotation

GitHub Actions / Run Tests (6.0.x)

Dereference of a possibly null reference.
this._logger.LogDebug($"[{methodName}] start {tile.ToString()}");
string key = this._pathUtils.GetTilePath(this.path, tile, true);

var request = new PutObjectRequest()
{
BucketName = this._bucket,
CannedACL = S3CannedACL.PublicRead,
Key = String.Format(key),
StorageClass=this._storageClass
BucketName = this._bucket,
CannedACL = S3CannedACL.PublicRead,
Key = String.Format(key),
StorageClass = this._storageClass
};

byte[] buffer = tile.GetImageBytes();
Expand All @@ -124,11 +151,26 @@

private string? GetTileKey(int z, int x, int y)
{
string methodName = MethodBase.GetCurrentMethod().Name;

Check warning on line 154 in MergerLogic/Clients/S3Client.cs

View workflow job for this annotation

GitHub Actions / Run Tests (6.0.x)

Dereference of a possibly null reference.
Comment thread
shimoncohen marked this conversation as resolved.
string keyPrefix = this._pathUtils.GetTilePathWithoutExtension(this.path, z, x, y, true);
var listRequests = new ListObjectsV2Request { BucketName = this._bucket, Prefix = keyPrefix, MaxKeys = 1 };
var listObjectsTask = this._client.ListObjectsV2Async(listRequests);
string? result = listObjectsTask.Result.S3Objects.FirstOrDefault()?.Key;
return result;

try
{
var getRequest = new GetObjectRequest { BucketName = this._bucket, Key = keyPrefix };
var getObjectTask = this._client.GetObjectAsync(getRequest);
Comment thread
razbroc marked this conversation as resolved.
string result = getObjectTask.Result.Key;
return result;
}
catch (AggregateException e)
{
if (IsKeyError(e))
{
this._logger.LogDebug($"[{methodName}] error getting key: {e.Message}");
return null;
Comment thread
razbroc marked this conversation as resolved.
}
// In case there are other errors such as connection to S3
throw e;
}
}
}
}
Loading
Loading