Azure blob upload - if the pdf file already exists in the blob, then rename pdf file by adding a number at the end?

Vamshi 151 Reputation points
2022-08-05T17:59:26.62+00:00

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.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,165 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Laxmikant 216 Reputation points
    2022-08-05T19:52:00.83+00:00

    replace the file name value like

    string newFileName = request.FormFile.FileName   
      
    ----- other code  
      
                     if (PdfResponse.IsExist)  
                     {  
                          newFileName = PdfResponse.FileName + "01"  
                     }  
                       
                     // notice use of newFileName variable  
                     await _blobManager.UploadFileAsync("workflow-queue", newFileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);  
    

  2. Vamshi 151 Reputation points
    2022-08-05T21:43:56.1+00:00

    Hi,

    I tried 2 options,

    • option 2 is working by appending Guidid but the file name format is not as per the expectation (mentioned in my initial post).
    • option 1 is working only partially. lets assume the file samplefile.pdf already exists. when i upload the another file with same name samplefile.pdf , then per the code it is named as samplefile(1).pdf But again if i upload another file with same name samplefile.pdf, the issue here is it is replacing the previously samplefile(1).pdf named file to same name i.e. samplefile(1).pdf. Expectation in this scenario is to name the file as samplefile(2).pdf

    Need inputs to achieve the solution.

    • OPTION 1:
                int i = 0;  
                string fileName = string.Empty;              
               DownloadFileResponse PdfResponse = await _blobManager.DownloadPdfFileAsync("workflow-queue", request.FormFile.FileName, cancellationToken).ConfigureAwait(false);  
               if (PdfResponse.IsExist)  
               {  
                   // option 1  
                   if (i == 0)  
                       fileName = request.FormFile.FileName.Replace(".pdf", "(" + ++i + ")" + ".pdf");  
                   else  
                       fileName = request.FormFile.FileName.Replace("(" + i + ")" + ".pdf", "(" + ++i + ")" + ".pdf");  
                   await _blobManager.UploadFileAsync("workflow-queue", fileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);  
      
               } else {  
                   await _blobManager.UploadFileAsync("workflow-queue", request.FormFile.FileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);  
               }  
      
    • OPTION 2:
                int i = 0;  
               string fileName = string.Empty;              
               DownloadFileResponse PdfResponse = await _blobManager.DownloadPdfFileAsync("workflow-queue", request.FormFile.FileName, cancellationToken).ConfigureAwait(false);  
               if (PdfResponse.IsExist)  
               {  
                   // option 2  
                    fileName = request.FormFile.FileName.Replace(".pdf", "-" + Guid.NewGuid().ToString() + ".pdf");  
      
                   await _blobManager.UploadFileAsync("workflow-queue", fileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);  
      
               } else {  
                   await _blobManager.UploadFileAsync("workflow-queue", request.FormFile.FileName, request.FormFile.ToBytes(), cancellationToken).ConfigureAwait(false);  
               }  
      

    Thankyou.