-
Notifications
You must be signed in to change notification settings - Fork 2
feat: support multibuckets by recieving bucket from task parameters #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using MergerLogic.Batching; | ||
| using Amazon.S3.Model; | ||
| using MergerLogic.Batching; | ||
| using MergerLogic.Utils; | ||
|
|
||
| namespace MergerLogic.Clients | ||
|
|
@@ -7,5 +8,7 @@ public interface IS3Client : IDataUtils | |
| { | ||
| void UpdateTile(Tile tile); | ||
| Tile? GetTile(string key); | ||
| string Bucket { get; } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? |
||
| ListObjectsV2Response ListObject(ref string? continuationToken, string prefix, string startAfter, int? maxKeys = null); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,19 +11,22 @@ namespace MergerLogic.Clients | |
| { | ||
| public class S3Client : DataUtils, IS3Client | ||
| { | ||
| private readonly string _bucket; | ||
|
|
||
| private string _bucket; | ||
| private readonly IAmazonS3 _client; | ||
| private readonly ILogger _logger; | ||
| private readonly IPathUtils _pathUtils; | ||
|
|
||
| public S3Client(IAmazonS3 client, IPathUtils pathUtils, IGeoUtils geoUtils, IImageFormatter formatter, ILogger<S3Client> logger, | ||
| string bucket, string path) : base(path, geoUtils, formatter) | ||
| public S3Client(IAmazonS3 client, IPathUtils pathUtils, IGeoUtils geoUtils, IImageFormatter formatter, ILogger<S3Client> logger, string bucket, string path) : base(path, geoUtils, formatter) | ||
| { | ||
| this._client = client; | ||
| this._bucket = bucket; | ||
| this._client = client ?? throw new Exception("s3 configuration is required"); | ||
| this._pathUtils = pathUtils; | ||
| this._logger = logger; | ||
| this._bucket = bucket; | ||
| } | ||
|
|
||
| public string Bucket | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? |
||
| { | ||
| get { return this._bucket; } | ||
| } | ||
|
|
||
| private byte[]? GetImageBytes(string key) | ||
|
|
@@ -104,7 +107,9 @@ public void UpdateTile(Tile tile) | |
|
|
||
| var request = new PutObjectRequest() | ||
| { | ||
| BucketName = this._bucket, CannedACL = S3CannedACL.PublicRead, Key = String.Format(key) | ||
| BucketName = this._bucket, | ||
| CannedACL = S3CannedACL.PublicRead, | ||
| Key = String.Format(key) | ||
| }; | ||
|
|
||
| byte[] buffer = tile.GetImageBytes(); | ||
|
|
@@ -130,5 +135,35 @@ public void UpdateTile(Tile tile) | |
| this._logger.LogDebug($"[{methodName}] end z: {z}, x: {x}, y: {y}"); | ||
| return result; | ||
| } | ||
|
|
||
| public ListObjectsV2Response ListObject(ref string? continuationToken, string prefix, string startAfter, int? maxKeys = null) | ||
| { | ||
| ListObjectsV2Request listRequests = null; | ||
| if (maxKeys == null) | ||
| { | ||
| listRequests = new ListObjectsV2Request | ||
| { | ||
| BucketName = this._bucket, | ||
| Prefix = prefix, | ||
| StartAfter = startAfter, | ||
| ContinuationToken = continuationToken | ||
| }; | ||
| } | ||
| else | ||
| { | ||
| listRequests = new ListObjectsV2Request | ||
| { | ||
| BucketName = this._bucket, | ||
| Prefix = prefix, | ||
| StartAfter = startAfter, | ||
| ContinuationToken = continuationToken, | ||
| MaxKeys = maxKeys.Value | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only add this parameter it |
||
| }; | ||
| } | ||
|
|
||
| var task = this._client.ListObjectsV2Async(listRequests); | ||
| var response = task.Result; | ||
| return response; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,7 @@ public abstract class Data<TUtilsType> : IData where TUtilsType : IDataUtils | |
| protected ValFromCoordFunction ConvertOriginCoord; | ||
|
|
||
| protected Data(IServiceProvider container, DataType type, string path, int batchSize, Grid? grid, | ||
| GridOrigin? origin, bool isBase, Extent? extent = null) | ||
| GridOrigin? origin, bool isBase, Extent? extent = null, string? bucket = null) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, data should not contain a |
||
| { | ||
| string methodName = MethodBase.GetCurrentMethod().Name; | ||
| this._container = container; | ||
|
|
@@ -88,7 +88,7 @@ protected Data(IServiceProvider container, DataType type, string path, int batch | |
| this.Path = path; | ||
| this.BatchSize = batchSize; | ||
| var utilsFactory = container.GetRequiredService<IUtilsFactory>(); | ||
| this.Utils = utilsFactory.GetDataUtils<TUtilsType>(path); | ||
| this.Utils = utilsFactory.GetDataUtils<TUtilsType>(path, bucket); | ||
| this.GeoUtils = container.GetRequiredService<IGeoUtils>(); | ||
| this.Grid = grid ?? this.DefaultGrid(); | ||
| this.Origin = origin ?? this.DefaultOrigin(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,14 @@ | ||
| using Amazon.S3; | ||
| using Amazon.S3.Model; | ||
| using MergerLogic.Batching; | ||
| using MergerLogic.Clients; | ||
| using MergerLogic.Utils; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Reflection; | ||
|
|
||
| namespace MergerLogic.DataTypes | ||
| { | ||
| public class S3 : Data<IS3Client> | ||
| { | ||
| private IAmazonS3 _client; | ||
| private string _bucket; | ||
| private readonly List<int> _zoomLevels; | ||
| private IEnumerator<int> _zoomEnumerator; | ||
| private string? _continuationToken; | ||
|
|
@@ -22,8 +18,8 @@ public class S3 : Data<IS3Client> | |
|
|
||
| private readonly IPathUtils _pathUtils; | ||
|
|
||
| public S3(IPathUtils pathUtils, IServiceProvider container, string path, int batchSize, Grid? grid, GridOrigin? origin, bool isBase) | ||
| : base(container, DataType.S3, path, batchSize, grid, origin, isBase) | ||
| public S3(IPathUtils pathUtils, IServiceProvider container, string bucket, string path, int batchSize, Grid? grid, GridOrigin? origin, bool isBase) | ||
| : base(container, DataType.S3, path, batchSize, grid, origin, isBase, null, bucket) | ||
| { | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] Ctor started"); | ||
| this._pathUtils = pathUtils; | ||
|
|
@@ -40,12 +36,6 @@ public S3(IPathUtils pathUtils, IServiceProvider container, string path, int bat | |
|
|
||
| protected override void Initialize() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove completly if not needed... |
||
| { | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] start"); | ||
| var configurationManager = this._container.GetRequiredService<IConfigurationManager>(); | ||
| var client = this._container.GetService<IAmazonS3>(); | ||
| this._client = client ?? throw new Exception("s3 configuration is required"); | ||
| this._bucket = configurationManager.GetConfiguration("S3", "bucket"); | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] ended"); | ||
| } | ||
|
|
||
| protected override GridOrigin DefaultOrigin() | ||
|
|
@@ -97,18 +87,7 @@ public override List<Tile> GetNextBatch(out string currentBatchIdentifier, out s | |
| } | ||
|
|
||
| string path = $"{this.Path}/{this._zoomEnumerator.Current}/"; | ||
| var listRequests = new ListObjectsV2Request | ||
| { | ||
| BucketName = this._bucket, | ||
| Prefix = path, | ||
| StartAfter = path, | ||
| MaxKeys = missingTiles, | ||
| ContinuationToken = this._continuationToken | ||
| }; | ||
|
|
||
| var listObjectsTask = this._client.ListObjectsV2Async(listRequests); | ||
| var response = listObjectsTask.Result; | ||
|
|
||
| ListObjectsV2Response response = this.Utils.ListObject(ref this._continuationToken, path, path, missingTiles); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand the thought, but because of the generic use this is a mistake. |
||
| foreach (S3Object item in response.S3Objects) | ||
| { | ||
| Tile? tile = this.Utils.GetTile(item.Key); | ||
|
|
@@ -145,27 +124,19 @@ public override void setBatchIdentifier(string batchIdentifier) | |
|
|
||
| private bool FolderExists(string directory) | ||
| { | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] start"); | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] start, bucket: {this.Utils.Bucket}, directory: {directory}"); | ||
| directory = $"{this.Path}/{directory}"; | ||
|
|
||
| var listRequests = new ListObjectsV2Request | ||
| { | ||
| BucketName = this._bucket, | ||
| Prefix = directory, | ||
| StartAfter = directory, | ||
| MaxKeys = 1 | ||
| }; | ||
| var task = this._client.ListObjectsV2Async(listRequests); | ||
| var response = task.Result; | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] end"); | ||
| return response.KeyCount > 0; | ||
| string? continuationToken = null; | ||
| ListObjectsV2Response response = this.Utils.ListObject(ref continuationToken, directory, directory, 1); | ||
| bool exists = response.KeyCount > 0; | ||
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] end, bucket: {this.Utils.Bucket}, directory: {directory}, exists: {exists}"); | ||
| return exists; | ||
| } | ||
|
|
||
| public override bool Exists() | ||
| { | ||
| this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] bucket: {this._bucket}, path: {this.Path}"); | ||
| bool exists = FolderExists(""); | ||
| this._logger.LogInformation($"[{MethodBase.GetCurrentMethod().Name}] ended"); | ||
| return exists; | ||
| } | ||
|
|
||
|
|
@@ -174,20 +145,9 @@ public override long TileCount() | |
| this._logger.LogDebug($"[{MethodBase.GetCurrentMethod().Name}] start"); | ||
| long tileCount = 0; | ||
| string? continuationToken = null; | ||
|
|
||
| do | ||
| { | ||
| var listRequests = new ListObjectsV2Request | ||
| { | ||
| BucketName = this._bucket, | ||
| Prefix = this.Path, | ||
| StartAfter = this.Path, | ||
| ContinuationToken = continuationToken | ||
| }; | ||
|
|
||
| var task = this._client.ListObjectsV2Async(listRequests); | ||
| var response = task.Result; | ||
|
|
||
| ListObjectsV2Response response = this.Utils.ListObject(ref continuationToken, this.Path, this.Path); | ||
| tileCount += response.KeyCount; | ||
| continuationToken = response.NextContinuationToken; | ||
| } while (continuationToken != null); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this commented?