Hi Expert team,
Below is my method and I am able to check if the file already exists in the blob
Regarding Azure blob upload - if the file already exists in the blob, then I want to rename pdf file by adding a number at the end.?
For Example: if a file "blobfile.pdf" already exists in the blob, then if i upload the same file or file with same name blobfile.pdf then it should name as "blobfile-1.pdf", suppose if i again upload the same file then it should name the file as "blobfile-2.pdf"
Need inputs to achieve the solution.
public override async Task HandleAsync(UploadWorkflowDocumentCommand request, CancellationToken cancellationToken)
{
if (request.FormFile == null)
{
throw new ValidationException($"Please select a file.");
}
List<string> allowedExtensions = new List<string> { Constants.PdfFileFormat };
List<string> allowedContentTypes = new List<string> { Constants.PdfFileFormatContentType };
if (!GenericHelper.ValidateFile(request.FormFile.FileName, request.FormFile.ContentType, allowedExtensions.ToArray(), allowedContentTypes.ToArray()))
{
throw new ValidationException($"Only PDF files are allowed.");
}
DownloadFileResponse PdfResponse = await _blobManager.DownloadPdfFileAsync("workflow-queue", request.FormFile.FileName, cancellationToken).ConfigureAwait(false);
if (PdfResponse.IsExist)
{
//if the file already exists then logic here to add the file name and save it to the blob
}
// if the file does not exists in the blob, then below condition is invoked to save the file to blob.
await _blobManager.UploadFileAsync("workflow-queue", request.FormFile.FileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);
}
Thank you.