'How to list out the blob properties in an azure subscription?
I am a beginner in Python and I tried listing out all the storage accounts, container name and blob properties in an azure subscription in Python. Instead of putting in hard values I want to retrieve values via loop. Any idea how I get through it?
from azure.identity import AzureCliCredential
import pandas as pd
from azure.mgmt import subscription
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
credential = AzureCliCredential()
subscription_client = subscription.SubscriptionClient(credential = credential)
dataset: list = []
for data in subscription_client.subscriptions.list():
data: dict = data.as_dict()
dataset.append({
'resource_id': data.get('id'),
'subscription_id': data.get('subscription_id'),
'subscription_name': data.get('display_name')
})
print(dataset)
blob_service_client = BlobServiceClient.()
container_client=blob_service_client.get_container_client("container")
containers = blob_service_client.list_containers()
for c in containers:
print(c)
container = ContainerClient.()
blobs_list = container.list_blobs()
for b in blobs_list:
print(b)
In similar fashion I want to retrieve specific properties of the containers and blobs of all the storage accounts in a subscription through iteration.
Solution 1:[1]
Here is the sample code to get the Blob Properties Content Type and Content Language
public static async Task SetBlobPropertiesAsync(BlobClient blob)
{
Console.WriteLine("Setting blob properties...");
try
{
// Get the existing properties
BlobProperties properties = await blob.GetPropertiesAsync();
BlobHttpHeaders headers = new BlobHttpHeaders
{
// Set the MIME ContentType every time the properties
// are updated or the field will be cleared
ContentType = "text/plain",
ContentLanguage = "en-us",
// Populate remaining headers with
// the pre-existing properties
CacheControl = properties.CacheControl,
ContentDisposition = properties.ContentDisposition,
ContentEncoding = properties.ContentEncoding,
ContentHash = properties.ContentHash
};
// Set the blob's properties.
await blob.SetHttpHeadersAsync(headers);
}
catch (RequestFailedException e)
{
Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
Here are a few links and documents with related discussions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SaiSakethGuduru-MT |
