Azure Blob Storage list container and blobs - c#

I am working on a Azure Storage project where i need to upload and download blobs in a container and list the container and blob in a listbox. I am unable to display the container and the blobs in my listbox.
This is my code to List:
And finally the code behind the interface where i call my upload, download and list methods:

The reason why you don't see any result when you click on Button3 in your webform is because you don't get back any data from the ListBlob method.
Change the ListBlob method to return a result like:
public List<string> GetBlobs()
{
List<string> blobs = new List<string>();
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof (CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob) item;
blobs.Add(string.Format("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri));
}
else if (item.GetType() == typeof (CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob) item;
blobs.Add(string.Format("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri));
}
else if (item.GetType() == typeof (CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory) item;
blobs.Add(string.Format("Directory: {0}", directory.Uri));
}
}
return blobs;
}
Than in your webform, I assume you have a ListBox with the name ListBox1. Call the method like:
protected void Button3_Click(object sender, EventArgs e)
{
ListBox1.DataSource = GetBlobs();
ListBox1.DataBind();
}

It isn't clear to me what problem you are experiencing as you haven't explained fully. Listing blobs within a container including paging support is demonstrated in the following code extracted from this sample.
BlobContinuationToken token = null;
do
{
BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);
token = resultSegment.ContinuationToken;
foreach (IListBlobItem blob in resultSegment.Results)
{
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
}
} while (token != null);

Related

Get blobs in a virtual folder on azure (BlobService.ListBlobs) (C#)

I am working with Azure Storage and I am programming in C#. I have a container all setup on Azure, I am able to save files into virtual folders inside our container.
However I am having trouble accessing blobs inside virtual folders inside my container. For example say i have:
container1/virtualfolder/file1.png
I want to be able to list all the blobs (such as file1.png, etc...) inside virtualfolder. How can I reference only these files? I have tried using BlobServices.ListBlob but it returns everything in the container. I have tried to modify the container path but it returns "BadRequest". I think this is an issue with path.
Here is my code:
public void getBlobOffServer()
{
string resourcePath = "container";
StartCoroutine(blobService.ListBlobs(getBlobListComplete, resourcePath));
}
private void getBlobListComplete(IRestResponse<BlobResults> response)
{
if (response.IsError)
{
Debug.Log("FAILED" + response.StatusCode + response.ErrorMessage);
}
else
{
for (int i = 0; i < response.Data.Blobs.Length; i++)
{
Debug.Log(response.Data.Blobs[i].Name);
}
isComplete = true;
}
}
In general, you can use ListBlobsSegmentedAsync of GetDirectoryReference to get all blobs from virtualfolder.
string containerName = "container-1";
string directoryName = "virtualfolder";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlobDirectory directory = container.GetDirectoryReference(directoryName);
var blobList = directory.ListBlobsSegmentedAsync(null).Result;
foreach (var blob in blobList.Results)
{
Console.WriteLine(blob.Uri.OriginalString.Substring(blob.Uri.OriginalString.LastIndexOf('/')+1));
}
RESULT:

How do I iterate CloudBlobDirectory to copy the data?

I have written below code to iterate through the Gen2 storage blob
CloudStorageAccount sourceAccount = CloudStorageAccount.Parse(sourceConnection);
CloudStorageAccount destAccount = CloudStorageAccount.Parse(destConnection);
CloudBlobClient sourceClient = sourceAccount.CreateCloudBlobClient();
CloudBlobClient destClient = destAccount.CreateCloudBlobClient();
CloudBlobContainer sourceBlobContainer = sourceClient.GetContainerReference(sourceContainer);
// Find all blobs that haven't changed since the specified date and time
IEnumerable<ICloudBlob> sourceBlobRefs = FindMatchingBlobsAsync(sourceBlobContainer, transferBlobsNotModifiedSince).Result;
private static async Task<IEnumerable<ICloudBlob>> FindMatchingBlobsAsync(CloudBlobContainer blobContainer, DateTime transferBlobsNotModifiedSince)
{
List<ICloudBlob> blobList = new List<ICloudBlob>();
BlobContinuationToken token = null;
// Iterate through the blobs in the source container
do
{
BlobResultSegment segment = await blobContainer.ListBlobsSegmentedAsync(prefix: "", currentToken: token);
foreach (CloudBlobDirectory VARIABLE in segment.Results)
{
BlobResultSegment segment2 = await VARIABLE.ListBlobsSegmentedAsync(currentToken: token);
foreach (CloudBlobDirectory VARIABLE2 in segment2.Results)//Bad coding
{
//how do I get children count ?
}
}
}while (token != null);
}
This will iterate only 2 levels but not dynamically till the inner levels. I have blob in below hierarchy
--Container
--FolderA
--FolderAA
--FolderAA1
--File1.txt
--File2.txt
--FolderAA2
--File1.txt
--File2.txt
--FolderAA3
--FolderAB
--File8.txt
--FolderAC
--File9.txt
This hierarchy is dynamic
How do I loop and copy the blob content.
Note: I do not want to use CLI commands to copy. Because I won't have any control once copy started.
Update
Found some samples here: https://csharp.hotexamples.com/examples/Microsoft.WindowsAzure.Storage.Blob/CloudBlobContainer/ListBlobsSegmented/php-cloudblobcontainer-listblobssegmented-method-examples.html
Please see the sample code below:
class Program
{
static void Main(string[] args)
{
var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
var client = storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference("test");
var blobs = FindMatchingBlobsAsync(container).GetAwaiter().GetResult();
foreach (var blob in blobs)
{
Console.WriteLine(blob.Name);
}
Console.WriteLine("-------------------------------------");
Console.WriteLine("List of all blobs fetched. Press any key to terminate the application.");
Console.ReadKey();
}
private static async Task<IEnumerable<ICloudBlob>> FindMatchingBlobsAsync(CloudBlobContainer blobContainer)
{
List<ICloudBlob> blobList = new List<ICloudBlob>();
BlobContinuationToken token = null;
// Iterate through the blobs in the source container
do
{
BlobResultSegment segment = await blobContainer.ListBlobsSegmentedAsync(prefix: "", useFlatBlobListing: true, BlobListingDetails.None, 5000, token, new BlobRequestOptions(), new OperationContext());
token = segment.ContinuationToken;
foreach(var item in segment.Results)
{
blobList.Add((ICloudBlob)item);
}
} while (token != null);
return blobList;
}
}

Method not found: 'Boolean Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists

I'm trying to create and delete blobs with the following code. Why am I getting this error? CreateIfNotExists - Method Not Found. I get the same when I try and ListBlobs.
What am I missing? What is the correct version of Microsoft.WindowsAzure.Storage
Here's my code
var storageAccount = CloudStorageAccount.Parse(BlobConnString);
var myClient = storageAccount.CreateCloudBlobClient();
var container = myClient.GetContainerReference(Mycontainer);
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
foreach (IListBlobItem blobItem in container.ListBlobs())
{
if (blobItem is CloudBlobDirectory)
{
CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
if (directory.Uri.AbsoluteUri.Contains(NIPRprocfolder))
{
IEnumerable<IListBlobItem> blobs = directory.ListBlobs(true);
ICloudBlob bi;
foreach (var blob in blobs)
{
if (blob is CloudBlockBlob)
{
bi = blob as CloudBlockBlob;
if (bi.Name.Contains(".xml"))
{
_logger.Info($"Deleting report : {bi.Name} from {NIPRprocfolder}");
bi.DeleteIfExists();
}
}
}
}
}
}
thx

How I get all blobs and not only the blobs in the root directory

How I become all blobs from my azure storage container and not only the blobs in the root?
private CloudBlobContainer GetCloudBlobContainer()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=*;AccountKey=*;EndpointSuffix=core.windows.net");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("test123");
return container;
}
public ActionResult ListBlobs()
{
CloudBlobContainer container = GetCloudBlobContainer();
List<string> blobs = new List<string>();
BlobResultSegment resultSegment = container.ListBlobsSegmentedAsync(null).Result;
foreach (IListBlobItem item in resultSegment.Results)
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
blobs.Add(blob.Name);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob blob = (CloudPageBlob)item;
blobs.Add(blob.Name);
}
//Directory
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory dir = (CloudBlobDirectory)item;
blobs.Add(dir.Uri.ToString());
}
}
return View(blobs);
}
The code only returns the blobs on the root directory. But I want also get the blobs inside a Directory.
Thanks
Please specify useFlatBlobListing to true when calling ListBlobsSegmentedAsync method.

how to search for a specific file in blob storage

I am kind of new to Blob storage and i need to access a specific file from blob storage.
i.e when i type in a specific folder it should list out all the blobs underneath it.
Can anyone help me with that
here's the code which i am trying to do.
if (AccountFileTransfer != null)
{
BlobClientFileTransfer = AccountFileTransfer.CreateCloudBlobClient();
ContainerFileTransfer = BlobClientFileTransfer.GetContainerReference(CONTAINER);
CloudBlob blob = ContainerFileTransfer.GetBlobReference(txtFileSearch.Text);
if (blob.Uri == null)
{
System.Windows.Forms.MessageBox.Show("Not a Valid blob search");
}
else
{
lvFileTransfer.Items.Add(blob.Uri);
}
}
Use Azure Search to index and search for files in Blob storage
Try this
if (AccountFileTransfer != null)
{
CloudBlobClient blobClient =
new CloudBlobClient(blobEndpoint,
new StorageCredentialsAccountAndKey(accountName, accountKey));
CloudBlobContainer container = blobClient.GetContainerReference(CONTAINER);
foreach (var blobItem in container .ListBlobs())
{
lvFileTransfer.Items.Add(blobItem .Uri);
}
}
Try this and If the blob in a Directory in container, in that case following format
container.GetBlobReference("Images/" + fileName);
public static bool BlobExists(CloudBlobContainer container, string fileName)
{
var blob = container.GetBlobReference(fileName);
try
{
blob.FetchAttributes();
return true;
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound)
{
return false;
}
}
return false;
}

Categories