VB.net code to push multiple files from local disk to blob container

Rajnilari2015
Posted by Rajnilari2015 under Azure category on | Points: 40 | Views : 187
The below code will do so

Private Sub PushToBlob()
Dim folderPath As String = ConfigurationManager.AppSettings("sourceFolder")
Dim connectionString As String = CloudConfigurationManager.GetSetting("StorageConnectionString")
Dim containerName As String = ConfigurationManager.AppSettings("containerName")
Dim blobName As String = ConfigurationManager.AppSettings("blobName")
Dim accountFiles = Getfiles(folderPath)

'BlobStorageUtility df = new BlobStorageUtility();
Dim blobContainer = GetCloudBlobContainer(connectionString, containerName)

For Each accountFile As var In accountFiles
Dim blob = blobContainer.GetBlockBlobReference(Path.GetFileName(accountFile))
Using fileStream = File.OpenRead(accountFile)
blob.UploadFromStream(fileStream)
End Using
Next
End Sub

'These are auxilary functions

Private Function Getfiles(folderPath As String) As List(Of String)
Return Directory.GetFiles(folderPath).ToList()
End Function

Private Function GetCloudBlobContainer(connectionString As String, containerName As String) As CloudBlobContainer
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(connectionString)
Dim blobclient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim blobcontainer As CloudBlobContainer = blobclient.GetContainerReference(containerName)

If blobcontainer.CreateIfNotExists() Then
blobcontainer.SetPermissions(New BlobContainerPermissions() With { _
Key .PublicAccess = BlobContainerPublicAccessType.Blob _
})
End If
Return blobcontainer
End Function

Comments or Responses

Login to post response