Skip to content

Commit

Permalink
Transient storage: s3 delete (#706)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- spdbt-1930(s3 file deletion);
  • Loading branch information
peggy-quartech authored Jan 29, 2024
1 parent d4090c4 commit 3daa714
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Spd.Manager.Licence/PersonalLicenceAppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ await _documentRepository.ManageAsync(new CreateDocumentCmd
DocumentType2 = docType2,
SubmittedByApplicantId = appResponse.ContactId,
ExpiryDate = expiredDate,
ToTransientBucket = false
}, ct);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Spd.Resource.Applicants/Document/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public record CreateDocumentCmd : DocumentCmd
public DocumentTypeEnum? DocumentType { get; set; } //tag1
public DocumentTypeEnum? DocumentType2 { get; set; } //tag2
public DateOnly? ExpiryDate { get; set; }
public bool ToTransientBucket { get; set; } = false;
}

public record CreateStreamDocumentCmd : CreateDocumentCmd
Expand Down
56 changes: 31 additions & 25 deletions src/Spd.Resource.Applicants/Document/DocumentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ internal class DocumentRepository : IDocumentRepository
private readonly DynamicsContext _context;
private readonly IMapper _mapper;
private readonly IFileStorageService _fileStorageService;
private readonly ITransientFileStorageService _transientFileStorageService;
private readonly ITempFileStorageService _tempFileService;

public DocumentRepository(IDynamicsContextFactory ctx,
IMapper mapper,
IFileStorageService fileStorageService,
ITempFileStorageService tempFileService)
ITempFileStorageService tempFileService,
ITransientFileStorageService transientFileStorageService)
{
_context = ctx.Create();
_mapper = mapper;
_fileStorageService = fileStorageService;
_tempFileService = tempFileService;
_transientFileStorageService = transientFileStorageService;
}
public async Task<DocumentListResp> QueryAsync(DocumentQry qry, CancellationToken ct)
{
Expand Down Expand Up @@ -106,7 +109,7 @@ private async Task<DocumentResp> DocumentCreateAsync(CreateDocumentCmd cmd, Canc
_context.SetLink(documenturl, nameof(documenturl.spd_SubmittedById), contact);
}

await UploadFileAsync(cmd.TempFile, application.spd_applicationid, documenturl.bcgov_documenturlid, null, ct);
await UploadFileAsync(cmd.TempFile, application.spd_applicationid, documenturl.bcgov_documenturlid, null, ct, cmd.ToTransientBucket);
await _context.SaveChangesAsync(ct);
documenturl._spd_applicationid_value = application.spd_applicationid;
return _mapper.Map<DocumentResp>(documenturl);
Expand Down Expand Up @@ -208,7 +211,7 @@ private async Task<DocumentResp> DocumentUpdateAsync(UpdateDocumentCmd cmd, Canc
return _mapper.Map<DocumentResp>(documenturl);
}

private async Task UploadFileAsync(SpdTempFile tempFile, Guid? applicationId, Guid? docUrlId, bcgov_tag? tag, CancellationToken ct)
private async Task UploadFileAsync(SpdTempFile tempFile, Guid? applicationId, Guid? docUrlId, bcgov_tag? tag, CancellationToken ct, bool toTransientBucket = false)
{
if (applicationId == null) return;
if (docUrlId == null) return;
Expand Down Expand Up @@ -236,12 +239,19 @@ private async Task UploadFileAsync(SpdTempFile tempFile, Guid? applicationId, Gu
}
};

await _fileStorageService.HandleCommand(new UploadFileCommand(
UploadFileCommand uploadFileCmd = new UploadFileCommand(
Key: ((Guid)docUrlId).ToString(),
Folder: $"spd_application/{applicationId}",
File: file,
FileTag: fileTag
), ct);
FileTag: fileTag);
if (toTransientBucket)
{
await _transientFileStorageService.HandleCommand(uploadFileCmd, ct);
}
else
{
await _fileStorageService.HandleCommand(uploadFileCmd, ct);
}
}
else
{
Expand All @@ -262,31 +272,27 @@ await _fileStorageService.HandleCommand(new UploadFileCommand(
}
};

await _fileStorageService.HandleCommand(new UploadFileStreamCommand(
Key: ((Guid)docUrlId).ToString(),
Folder: $"spd_application/{applicationId}",
FileStream: fileStream,
FileTag: fileTag
), ct);
UploadFileStreamCommand uploadFileCmd = new UploadFileStreamCommand(
Key: ((Guid)docUrlId).ToString(),
Folder: $"spd_application/{applicationId}",
FileStream: fileStream,
FileTag: fileTag);
if (toTransientBucket)
{
await _transientFileStorageService.HandleCommand(uploadFileCmd, ct);
}
else
{
await _fileStorageService.HandleCommand(uploadFileCmd, ct);
}
}
}

private async Task DeleteFileAsync(Guid docUrlId, Guid applicationId, CancellationToken ct)
{
//set file-classification tag to be deleted
FileTag fileTag =
new FileTag()
{
Tags = new List<Utilities.FileStorage.Tag>
{
new Utilities.FileStorage.Tag("file-classification", "deleted"),
}
};
await _fileStorageService.HandleCommand(new UpdateTagsCommand(
await _transientFileStorageService.HandleDeleteCommand(new StorageDeleteCommand(
Key: ((Guid)docUrlId).ToString(),
Folder: $"spd_application/{applicationId}",
FileTag: fileTag
), ct);
Folder: $"spd_application/{applicationId}"), ct);

}

Expand Down
2 changes: 2 additions & 0 deletions src/Spd.Utilities.FileStorage/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface ITransientFileStorageService
{
Task<string> HandleCommand(StorageCommand cmd, CancellationToken cancellationToken);
Task<StorageQueryResults> HandleQuery(StorageQuery query, CancellationToken cancellationToken);
Task<string> HandleDeleteCommand(StorageDeleteCommand cmd, CancellationToken cancellationToken);
}

public abstract record StorageCommand(string Key, string? Folder);
Expand All @@ -21,6 +22,7 @@ public record UpdateTagsCommand(string Key, string? Folder, FileTag? FileTag) :
//copy the file from source to dest for the same bucket.
public record CopyFileCommand(string SourceKey, string? SourceFolder, string DestKey, string? DestFolder) : StorageCommand(SourceKey, SourceFolder);

public record StorageDeleteCommand(string Key, string? Folder);
public record FileTag
{
public IEnumerable<Tag> Tags { get; set; } = Array.Empty<Tag>();
Expand Down
28 changes: 28 additions & 0 deletions src/Spd.Utilities.FileStorage/FileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public async Task<StorageQueryResults> HandleQuery(StorageQuery query, Cancellat
};
}

public async Task<string> HandleDeleteCommand(StorageDeleteCommand cmd, CancellationToken cancellationToken)
{
return await DeleteStorageItem(cmd, cancellationToken);
}
private async Task<string> UploadStorageItem(UploadFileCommand cmd, CancellationToken cancellationToken)
{
File file = cmd.File;
Expand Down Expand Up @@ -65,6 +69,24 @@ private async Task<string> UploadStorageItem(UploadFileCommand cmd, Cancellation
return cmd.Key;
}

private async Task<string> DeleteStorageItem(StorageDeleteCommand cmd, CancellationToken cancellationToken)
{
var folder = cmd.Folder == null ? "" : $"{cmd.Folder}/";
var key = $"{folder}{cmd.Key}";

var request = new DeleteObjectRequest
{
Key = key,
BucketName = _config.Value.Bucket,
};

var response = await _amazonS3Client.DeleteObjectAsync(request, cancellationToken);

//If the delete action is successful, the service sends back an HTTP 204 response.
response.EnsureNoContent();
return cmd.Key;
}

private async Task<string> UploadStorageItemStream(UploadFileStreamCommand cmd, CancellationToken cancellationToken)
{
FileStream file = cmd.FileStream;
Expand Down Expand Up @@ -247,5 +269,11 @@ public static void EnsureSuccess(this AmazonWebServiceResponse response)
if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
throw new InvalidOperationException($"Operation failed with status {response.HttpStatusCode}");
}

public static void EnsureNoContent(this AmazonWebServiceResponse response)
{
if (response.HttpStatusCode != System.Net.HttpStatusCode.NoContent)
throw new InvalidOperationException($"Delete Operation failed with status {response.HttpStatusCode}");
}
}
}
2 changes: 1 addition & 1 deletion src/Spd.Utilities.FileStorage/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IServiceCollection AddFileStorageProxy(this IServiceCollection ser
Options.Create(options.MainBucketSettings)));

//create transient bucket
if (options.TransientBucketSettings?.Url != null && string.IsNullOrWhiteSpace(options.TransientBucketSettings?.Url.ToString()))
if (options.TransientBucketSettings?.Url != null && !string.IsNullOrWhiteSpace(options.TransientBucketSettings?.Url.ToString()))
{
var transientBucketConfig = new AmazonS3Config
{
Expand Down

0 comments on commit 3daa714

Please sign in to comment.