-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add native S3 upload handler and receiver #192
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| using System.Text; | ||
| using Amazon.S3; | ||
| using SW.CloudFiles.S3; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.NativeAdapters.S3Receiver; | ||
|
|
||
| public class NativeS3Receiver : INativeInfolinkReceiver, IDisposable | ||
| { | ||
| private S3ReceiverInput _options = new(); | ||
| private CloudFilesService? _cloudFiles; | ||
| private AmazonS3Client? _s3Client; | ||
|
|
||
| public Task Initialize() | ||
| { | ||
| var options = new CloudFilesOptions | ||
| { | ||
| AccessKeyId = _options.AccessKeyId, | ||
| SecretAccessKey = _options.SecretAccessKey, | ||
| ServiceUrl = _options.ServiceUrl, | ||
| BucketName = _options.BucketName, | ||
| }; | ||
|
|
||
| _cloudFiles = new CloudFilesService(options); | ||
| _s3Client = options.CreateClient(); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public Task Finalize() | ||
| { | ||
| _cloudFiles?.Dispose(); | ||
| _s3Client?.Dispose(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| // Safety net: the DI container disposes scoped instances when the job's scope ends, | ||
| // even if Initialize/ListFiles/GetFile/DeleteFile threw and Finalize was never reached. | ||
| public void Dispose() | ||
| { | ||
| _cloudFiles?.Dispose(); | ||
| _s3Client?.Dispose(); | ||
| } | ||
|
|
||
| public async Task<IEnumerable<string>> ListFiles() | ||
| { | ||
| var files = await _cloudFiles.ListAsync(_options.FolderName ?? string.Empty); | ||
|
|
||
| return files | ||
| .Where(f => !f.Key.EndsWith("/")) | ||
| .Select(f => f.Key) | ||
| .Take(_options.BatchSize) | ||
| .ToList(); | ||
| } | ||
|
|
||
| public async Task<XchangeFile> GetFile(string fileId) | ||
| { | ||
| await using var stream = await _cloudFiles.OpenReadAsync(fileId); | ||
| using var memoryStream = new MemoryStream(); | ||
| await stream.CopyToAsync(memoryStream); | ||
| var bytes = memoryStream.ToArray(); | ||
|
|
||
| return (_options.ResponseEncoding ?? "utf8").ToLower() switch | ||
| { | ||
| "base64" => new XchangeFile(Convert.ToBase64String(bytes), fileId), | ||
| "utf8" => new XchangeFile(Encoding.UTF8.GetString(bytes), fileId), | ||
| _ => throw new ArgumentException( | ||
| $"Unknown {nameof(S3ReceiverInput.ResponseEncoding)} '{_options.ResponseEncoding}'") | ||
| }; | ||
| } | ||
|
|
||
| public async Task DeleteFile(string fileId) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(_options.DeleteMovesFileTo)) | ||
| { | ||
| // Preserve the path relative to FolderName so files with the same name in | ||
| // different subdirectories don't collide at the destination. | ||
| var relativePath = !string.IsNullOrEmpty(_options.FolderName) && fileId.StartsWith(_options.FolderName + "/") | ||
| ? fileId[(_options.FolderName.Length + 1)..] | ||
| : fileId; | ||
|
|
||
| // Server-side copy: S3 moves the object internally, so no bytes are | ||
| // downloaded or re-uploaded through this process. | ||
| var targetKey = $"{_options.DeleteMovesFileTo}/{relativePath}"; | ||
| await _s3Client!.CopyObjectAsync(_options.BucketName, fileId, _options.BucketName, targetKey); | ||
| } | ||
|
|
||
| await _cloudFiles.DeleteAsync(fileId); | ||
| } | ||
|
hamzahalq marked this conversation as resolved.
|
||
|
|
||
| public string Name => "NativeS3Receiver"; | ||
|
|
||
| public void InitializeStartupValues(IDictionary<string, string> settings) | ||
| { | ||
| _options = settings.ConvertTo<S3ReceiverInput>(); | ||
| } | ||
|
|
||
| public Type StartupValuesType => typeof(S3ReceiverInput); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.ComponentModel; | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace SW.Bitween.NativeAdapters.S3Receiver; | ||
|
|
||
| public class S3ReceiverInput | ||
| { | ||
| [Required] | ||
| public string AccessKeyId { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| [Secure] | ||
| public string SecretAccessKey { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ServiceUrl { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string BucketName { get; set; } = string.Empty; | ||
|
|
||
| public string? FolderName { get; set; } | ||
|
|
||
| [DefaultValue(50)] | ||
| public int BatchSize { get; set; } = 50; | ||
|
|
||
| [DefaultValue("utf8")] | ||
| public string ResponseEncoding { get; set; } = "utf8"; | ||
|
|
||
| public string? DeleteMovesFileTo { get; set; } | ||
| } |
48 changes: 48 additions & 0 deletions
48
SW.Bitween.NativeAdapters/S3UploadHandler/NativeS3UploadHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using SW.CloudFiles.S3; | ||
| using SW.PrimitiveTypes; | ||
|
|
||
| namespace SW.Bitween.NativeAdapters.S3UploadHandler; | ||
|
|
||
| public class NativeS3UploadHandler : INativeInfolinkHandler | ||
| { | ||
| private S3UploadHandlerInput _options = new(); | ||
|
|
||
| public async Task<XchangeFile> Handle(XchangeFile xchangeFile) | ||
| { | ||
| using var cloudFiles = new CloudFilesService(new CloudFilesOptions | ||
| { | ||
| AccessKeyId = _options.AccessKeyId, | ||
| SecretAccessKey = _options.SecretAccessKey, | ||
| ServiceUrl = _options.ServiceUrl, | ||
| BucketName = _options.BucketName, | ||
| }); | ||
|
|
||
| var key = _options.FileName; | ||
| if (string.IsNullOrWhiteSpace(key)) | ||
| { | ||
| var extension = _options.FileExtension?.TrimStart('.'); | ||
| var name = $"{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid():N}"; | ||
| key = string.IsNullOrEmpty(extension) ? name : $"{name}.{extension}"; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(_options.FolderName)) | ||
| key = $"{_options.FolderName}/{key}"; | ||
| } | ||
|
hamzahalq marked this conversation as resolved.
|
||
|
|
||
| await cloudFiles.WriteTextAsync(xchangeFile.Data, new WriteFileSettings | ||
| { | ||
| Key = key, | ||
| ContentType = _options.ContentType, | ||
| }); | ||
|
|
||
| return new XchangeFile(key, xchangeFile.Filename); | ||
| } | ||
|
|
||
| public string Name => "NativeS3UploadHandler"; | ||
|
|
||
| public void InitializeStartupValues(IDictionary<string, string> settings) | ||
| { | ||
| _options = settings.ConvertTo<S3UploadHandlerInput>(); | ||
| } | ||
|
|
||
| public Type StartupValuesType => typeof(S3UploadHandlerInput); | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
SW.Bitween.NativeAdapters/S3UploadHandler/S3UploadHandlerInput.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System.ComponentModel; | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace SW.Bitween.NativeAdapters.S3UploadHandler; | ||
|
|
||
| public class S3UploadHandlerInput | ||
| { | ||
| [Required] | ||
| public string AccessKeyId { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| [Secure] | ||
| public string SecretAccessKey { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ServiceUrl { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string BucketName { get; set; } = string.Empty; | ||
|
|
||
| public string? FolderName { get; set; } | ||
|
|
||
| public string? FileName { get; set; } | ||
|
|
||
| public string? FileExtension { get; set; } | ||
|
|
||
| [DefaultValue("text/plain")] | ||
| public string ContentType { get; set; } = "text/plain"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| <PackageReference Include="Rebex.Pop3" Version="8.0.9673" /> | ||
| <PackageReference Include="Rebex.Sftp" Version="8.0.9673" /> | ||
| <PackageReference Include="Scriban" Version="7.0.6" /> | ||
| <PackageReference Include="SimplyWorks.CloudFiles.S3" Version="8.1.1" /> | ||
|
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. 🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check NuGet for the latest version and any security advisories.
curl -s "https://api.nuget.org/v3-flatcontainer/simplyworks.cloudfiles.s3/index.json" | jq '.versions[-3:]'
gh api graphql -f query='
{
securityVulnerabilities(first: 5, ecosystem: NUGET, package: "SimplyWorks.CloudFiles.S3") {
nodes {
advisory { summary severity publishedAt }
vulnerableVersionRange
firstPatchedVersion { identifier }
}
}
}'Repository: simplify9/Bitween-api Length of output: 244 Upgrade 🤖 Prompt for AI Agents |
||
| <PackageReference Include="SimplyWorks.PrimitiveTypes" Version="8.1.2" /> | ||
| </ItemGroup> | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.