locked
Help with downloading a blob from azure storage. RRS feed

  • Question

  • I can't figure out what I'm doing wrong in the following code.  My MemoryStream is nothing with a length of 0.  I receive the following error when leaving the Using statement: The remote server returned an error: (400) Bad Request.  Any help is appreciated.  Thanks in advance.

    Dim strNewFilePath As String
    strNewFilePath = TempFileDirectory
     If TestSetting("AddDFP") AndAlso cSettings("AddDFP") > 0 Then
      m_RecordId = strRecordId
      strNewFilePath &= GetAddressDirectory(cSettings("AddDFP")) & "\"
     End If
     'if path not exits create here
     If Not Directory.Exists(strNewFilePath) Then
      Directory.CreateDirectory(strNewFilePath)
     End If
     
    Dim storageconn As String = System.Configuration.ConfigurationSettings.AppSettings.Get("StorageConnectionString")
    Dim storageacc As CloudStorageAccount = CloudStorageAccount.Parse(storageconn)
    Dim blobClient As CloudBlobClient = storageacc.CreateCloudBlobClient()
    Dim strAgency As String = GetAgencyName(gstrAgencyId)
    Dim container As CloudBlobContainer = blobClient.GetContainerReference(strAgency)
    Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(strFilePath)
     Using ms As MemoryStream = New MemoryStream()
      blockBlob.DownloadToStream(ms)
      ms.Position = 0
      System.IO.File.WriteAllBytes(strNewFilePath And LCase(strFileName), ms.ToArray())
     End Using

    Tuesday, February 25, 2020 5:38 PM

Answers

  • If you receive the exception “the remote server returned an error: (400) Bad Request” while creating/downloading a Blob container with Azure you are most likely violate the name conventions, Please check the container name and URL path.

    You may also refer to the suggestion mentioned in the SO threads. Which provides some idea on your query

    Hope this helps! 

    Kindly let us know if the above helps or you need further assistance on this issue.
    ------------------------------------------------------------------------------------------
    Do click on "Mark as Answer" and Upvote on the post that helps you, this can be beneficial to other community members.

    Wednesday, February 26, 2020 11:58 AM
  • Thanks for the help!!  I actually had two things wrong and my eyes just weren't seeing them.

    1st) Although I uploaded the file correctly, when trying to open it I forgot to "Lower Case" and remove spaces from strAgency which are requirements for container naming conventions.

    2nd) On strNewFilePath &= GetAddressDirectory(cSettings("AddDFP")) & "\" the "\" should have been "/".

    I've attached a modified version of the code below that works with a few minor changes.

     Dim JustFileName As String
          Dim JustPath As String
          Dim strAzureDirPath As String
          'JustFileName = IO.Path.GetFileName(txtFilename.Text)
          'JustFileName = sFileName.Substring(sFileName.LastIndexOf("\") + 1)

          JustPath = Strings.Left(strFilePath, Len(strFilePath) - Len(strFileName))
          Dim strNewFilePath As String
           strNewFilePath = cSettings("DefaultFilePath") & "\"


          If TestSetting("AddDFP") AndAlso cSettings("AddDFP") > 0 Then
           m_RecordId = strRecordId
           strAzureDirPath = GetAddressDirectory(cSettings("AddDFP")) & "/"
           strNewFilePath &= GetAddressDirectory(cSettings("AddDFP")) & "\"
          End If


          'if path not exits create here
          If Not Directory.Exists(strNewFilePath) Then
           Directory.CreateDirectory(strNewFilePath)
          End If


          Dim storageconn As String = System.Configuration.ConfigurationSettings.AppSettings.Get("StorageConnectionString")
          Dim storageacc As CloudStorageAccount = CloudStorageAccount.Parse(storageconn)
          Dim blobClient As CloudBlobClient = storageacc.CreateCloudBlobClient()
          Dim strAgency As String = GetAgencyName(gstrAgencyId)

          strAgency = LCase(strAgency.Replace(" ", "")).Trim
          Dim container As CloudBlobContainer = blobClient.GetContainerReference(strAgency)
          Dim ms As New MemoryStream
          Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(strAzureDirPath & strFileName)

    And all 3 of these options below work, I just used the most direct way to download to file as that is how I will access the image to be usable for reports.

          'blockBlob.DownloadToStream(ms)
          'ms.Position = 0
          blockBlob.DownloadToFile(strNewFilePath & strFileName, FileMode.OpenOrCreate)


          'Using ms As MemoryStream = New MemoryStream()
          ' blockBlob.DownloadToStream(ms)
          ' ms.Position = 0
          ' System.IO.File.WriteAllBytes(strNewFilePath And LCase(strFileName), ms.ToArray())
          'End Using


    Thanks again for your help!

    • Marked as answer by mrbill65 Wednesday, February 26, 2020 3:59 PM
    Wednesday, February 26, 2020 3:59 PM

All replies

  • If you receive the exception “the remote server returned an error: (400) Bad Request” while creating/downloading a Blob container with Azure you are most likely violate the name conventions, Please check the container name and URL path.

    You may also refer to the suggestion mentioned in the SO threads. Which provides some idea on your query

    Hope this helps! 

    Kindly let us know if the above helps or you need further assistance on this issue.
    ------------------------------------------------------------------------------------------
    Do click on "Mark as Answer" and Upvote on the post that helps you, this can be beneficial to other community members.

    Wednesday, February 26, 2020 11:58 AM
  • Thanks for the help!!  I actually had two things wrong and my eyes just weren't seeing them.

    1st) Although I uploaded the file correctly, when trying to open it I forgot to "Lower Case" and remove spaces from strAgency which are requirements for container naming conventions.

    2nd) On strNewFilePath &= GetAddressDirectory(cSettings("AddDFP")) & "\" the "\" should have been "/".

    I've attached a modified version of the code below that works with a few minor changes.

     Dim JustFileName As String
          Dim JustPath As String
          Dim strAzureDirPath As String
          'JustFileName = IO.Path.GetFileName(txtFilename.Text)
          'JustFileName = sFileName.Substring(sFileName.LastIndexOf("\") + 1)

          JustPath = Strings.Left(strFilePath, Len(strFilePath) - Len(strFileName))
          Dim strNewFilePath As String
           strNewFilePath = cSettings("DefaultFilePath") & "\"


          If TestSetting("AddDFP") AndAlso cSettings("AddDFP") > 0 Then
           m_RecordId = strRecordId
           strAzureDirPath = GetAddressDirectory(cSettings("AddDFP")) & "/"
           strNewFilePath &= GetAddressDirectory(cSettings("AddDFP")) & "\"
          End If


          'if path not exits create here
          If Not Directory.Exists(strNewFilePath) Then
           Directory.CreateDirectory(strNewFilePath)
          End If


          Dim storageconn As String = System.Configuration.ConfigurationSettings.AppSettings.Get("StorageConnectionString")
          Dim storageacc As CloudStorageAccount = CloudStorageAccount.Parse(storageconn)
          Dim blobClient As CloudBlobClient = storageacc.CreateCloudBlobClient()
          Dim strAgency As String = GetAgencyName(gstrAgencyId)

          strAgency = LCase(strAgency.Replace(" ", "")).Trim
          Dim container As CloudBlobContainer = blobClient.GetContainerReference(strAgency)
          Dim ms As New MemoryStream
          Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(strAzureDirPath & strFileName)

    And all 3 of these options below work, I just used the most direct way to download to file as that is how I will access the image to be usable for reports.

          'blockBlob.DownloadToStream(ms)
          'ms.Position = 0
          blockBlob.DownloadToFile(strNewFilePath & strFileName, FileMode.OpenOrCreate)


          'Using ms As MemoryStream = New MemoryStream()
          ' blockBlob.DownloadToStream(ms)
          ' ms.Position = 0
          ' System.IO.File.WriteAllBytes(strNewFilePath And LCase(strFileName), ms.ToArray())
          'End Using


    Thanks again for your help!

    • Marked as answer by mrbill65 Wednesday, February 26, 2020 3:59 PM
    Wednesday, February 26, 2020 3:59 PM