question

Vamshi-5948 avatar image
0 Votes"
Vamshi-5948 asked Vamshi-5948 commented

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

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.

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-core-webapidotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

llaxmikant avatar image
0 Votes"
llaxmikant answered Vamshi-5948 commented

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);


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you @llaxmikant for your time and the inputs but I observed an issue.

lets assume the file samplefile.pdf already exists. when i upload the another file with same name samplefile.pdf , then per the code you provided it is named as samplefile01.pdf But again if i upload another file with same name samplefile.pdf, the issue i observed here is it is replacing the previously samplefile01.pdf named file to same name i.e. samplefile01.pdf.

My expectation is to name the file as samplefile02.pdf



Thank you.

0 Votes 0 ·
Vamshi-5948 avatar image
0 Votes"
Vamshi-5948 answered Vamshi-5948 commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

you may try with random number like


 Random rnd = new Random();
    
    
   fileName = request.FormFile.FileName.Replace(".pdf", "-" + rnd.Next().ToString() + ".pdf");
    
 //or
 fileName = request.FormFile.FileName.Replace(".pdf", "-" + DateTime.Now.ToString() + ".pdf");


If you need a sequence numbers while renaming files then you will have to write some logic using string manipulation to get current and next sequence


1 Vote 1 ·

Thank you @llaxmikant for the inputs.

0 Votes 0 ·