Reputation: 125
I want to lookup a storage account in Microsoft Azure, list the blobs in a particular container(in the storage account),then store the listed blob names to a database table. Anyone please suggest a c# code to store the list of blob names to database table.
namespace ListStorageAccntFiles
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
//Code to list the blobnames in Console
CloudStorageAccount StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var BlobClient = StorageAccount.CreateCloudBlobClient();
var Container = BlobClient.GetContainerReference("samples-workitems");
var list = Container.ListBlobs();
List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
blobNames.ForEach(Console.WriteLine);
//Code to store blobnames under the column header "name" in a database table
}
}
}
Upvotes: 0
Views: 1459
Reputation: 3293
with the scenario!!!! program that can look at storage account and process all new & modified files and take an action if there is any new ones
In my opinion, I recommend using BlobTrigger of WebJobs SDK to achieve your purpose.Here is my code sample for you to have a better understanding of it:
WebJob Class
public class Functions
{
//This function will get triggered/executed when a blob is created or updated on an Azure Blob container called trigger-blob-input.
public static void TriggerAzureBlob([BlobTrigger("trigger-blob-input/{name}")] ICloudBlob blob)
{
Console.WriteLine("Blob name:" + blob.Name);
//do something with the created/updated blob
}
}
When blobs are created/updated as follows:
You could detect the blobs via BlobTrigger and get the following output:
For more details, you could refer to the following tutorials:
how to create a WebJob and deploy it to Azure
how to trigger a function when a blob is created or updated
Additionally, the WebJobs SDK scans log files to monitor new or changed blobs. This process is not read-time, so a function might not be triggered for a short or longer time after the blob is created/updated.
If the limitation of blob trigger is not meet your application, you could create a queue message when you create/update the blob, then use the QueueTrigger instead of BlobTrigger on the function that processes your blob.
Upvotes: 1
Reputation: 4094
Going forward with your code. Add this line to convert it into a delimited string
string namesAsString = blobNames.Aggregate((i, j) => i + "UNIQUE_DELIMITER_CHARACTER_OF_YOUR_CHOICE" + j);
For instance, you can use ':' as your delimiter
Now you can easily store this string in your table. In order to update this data in case the parent data changes, you can update it by Index (index of object in original collection
Upvotes: 0
Reputation: 976
I think you can store the values as comma separated String. While retrieving the values back in C#, you can split(",") to get the values as Array. [In java, string split method returns array. Not sure about c#]
If you think that your values contain comma in them, use some other character in the place of comma.
Upvotes: 0