How to get all files from a directory in Azure BLOB - c#

We are storing the application logs in Azure BLOB storage. We are currently downloading the files using the complete URI to the file. Every time when the settings of Azure BLOB is changed, the file name also gets changed. So we are getting error as file not exists.
Suggest a way to get the files using file extension from a directory without having file name in UI.

Use the ListBlobs() method to retrieve all blobs for a specific container and then you could use the static .NET Path.GetExtension() method to retrieve the file extension so you can filter them. Example:
var storageAccount = CloudStorageAccount.Parse("yourCS");
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(container);
var list = container.ListBlobs();
var blobs = list.OfType<CloudBlockBlob>()
.Where(b => Path.GetExtension(b.Name).Equals("yourextension"))

Related

Create container in Azure storage with c#

I have the following code:
var cloudStorageAccount = CloudStorageAccount.Parse(this._appSettings.BlobConnectionString);
this._cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = this._cloudBlobClient.GetContainerReference("dpd/textures/test");
await container.CreateIfNotExistsAsync();
I want to create the "test" directory inside dpd/textures/, but I keep getting this error:
The requested URI does not represent any resource on the server azure
container
What am I doing wrong?
Actually, there is no real directory in blob storage. The directory(not container) is always a part of the blob's name.
So you can just create a container like "dpd", then you can use any upload methods to upload a file like myfile.txt. During uploading file, you can specify your file name as "textures/test/myfile.txt" in your GetBlockBlobReference method. Then the directory "textures/test" is created automatically.
Simple code:
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
var myblob = cloudBlobContainer.GetBlockBlobReference("textures/test/myfile.txt");
myblob.UploadFromFile("your file path");
Test result:
And also note that, if the blob(myfile.txt) is deleted, then the directory "textures/test" will also be removed, since it's a part name of the blob.

c# Azure Cannot set the blob tier

CloudBlockBlob doesn't have any method to set the blob tier to hot/cool/archive. I have also checked the other blob types and they do not have a method that allows this either.
I.E this method: https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-tier
Is their any way to change the blob tier in code from hot to cold in C# with azure storage?
I think the method is exactly what you need: CloudBlockBlob.SetStandardBlobTier. Maybe you were not checking the latest version of Azure Storage Client Library?
Is their any way to change the blob tier in code from hot to cold in C# with azure storage?
As ZhaoXing Lu mentioned that we could use CloudBlockBlob.SetStandardBlobTier.
Note: The operation is allowed on a page blob in a premium storage account and on a block blob in a blob storage account
The following code works correctly on my side. I use the library WindowsAzure.Storage 9.1.1
var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("container");
var blob = container.GetBlockBlobReference("blob name");
blob.SetStandardBlobTier(StandardBlobTier.Cool);
blob.FetchAttributes();
var tier = blob.Properties.StandardBlobTier;
Using Azure Blob storage client library v12 for .NET, replace myaccount with the name of your storage account, mycontainer with your container name and myblob with the blob name for which the tier is to be changed:
var sharedKeyCredential = new StorageSharedKeyCredential("myaccount", storageAccountKey);
var baseBlobContainerUrl = string.Format("{0}.blob.core.windows.net", "myaccount");
var blobServiceClient = new BlobServiceClient(new Uri($"https://{baseBlobContainerUrl}"), sharedKeyCredential);
var containerClient = blobServiceClient.GetBlobContainerClient("mycontainer");
BlobClient blobClient = containerClient.GetBlobClient("myblob");
// Set access tier to cool.
await blobClient.SetAccessTierAsync(AccessTier.Cool);
If you are working with Azure Gov, use this url insteadl "{0}.blob.core.usgovcloudapi.net"
Keep in mind, your storage account should support Cool Storage.

How to upload List of images on azure and retrieve addresses of uploaded images

I am doing Mvc asp.net project. i want to upload four images on azure blob storage, i am uploading one image at a time using following method
public static string UploadToBlob(string fileName, byte[] data)
{
MemoryStream file = new MemoryStream(data);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("ConnectionSetting"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
blob.Properties.ContentType = "image/jpg";
blob.UploadFromStream(file);
string url = blob.Uri.ToString();
return url;
}
it's takes four call to azure server, so i want do it in one call i.e upload List of four images on azure and retrieve addresses of uploaded images.
You need to make separate service calls to upload separate blobs. However, you should be sharing most of the common code in that method. Namely, everything above actually getting the blob reference. This will save you several service calls by only calling CreateIfNotExists on the container once.

Azure storage blobs, C#, how can I determine what blob is currently in use?

I am writing console app to list and snaphot/restore azure blobs. As I understand, I can make snapshot, then fully turn off the machine(from azure portal, with deallocate), and copy some snapshot back with overwrite
In this example I'm getting blobs into a collection:
// get storage account based on credentials
CloudStorageAccount storageAccountObj = new CloudStorageAccount(creds, false);
// move all blobs to collection
CloudBlobClient blobClient = storageAccountObj.CreateCloudBlobClient();
var containers = blobClient.ListContainers();
List<CloudBlob> blobsArray = new List<CloudBlob>();
foreach (var container in containers)
{
foreach (var listBlobItem in container.ListBlobs())
{
var blobItem = (CloudBlob)listBlobItem;
blobsArray.Add(blobItem);
}
}
// display blobs
DisplayBlobCollection(blobsArray);
But there are blobs with same names but different ETag's:
How can I determine what blob is currently in use by virtual machine?
How come you get blobs with same name? Pring out the container name too, it may give you a clue which one is in use by the VM. There cannot be two blobs with same name within the same container.

Upload image to Azure blob storage from Windows Phone. Not creating

I am using Windows Azure to store images in my windows phone application.
The camera takes a photo and the chosen photo stream is then uploaded. However it is throwing NO error but is not uploading?
var blobContainer = CloudStorageContext.Current.Resolver.CreateCloudBlobClient();
var container = blobContainer.GetContainerReference("pics");
var blob = container.GetBlobReference("picture.jpg");
blob.UploadFromStream(e.ChosenPhoto, response => { MessageBox.Show(blob.Uri.ToString()) });
I don't have a clue what is happening. The Resolver contains the correct user, key and urls. The container "pics" does exist, but no image is being uploaded. The message box pops up with a url which does not exist.
UPDATE - There seems to be a simular (well almost identical) question posted here - Uploading a photo stream from camera into azure blob in WP7. However the upper case container name is not an issue here, so that solution did not fix this
I have an application (Windows Phone 8) that uploads an image taken to an Azure webrole, which in turn stores the image in an Azure Storage Blob. The code below is how the server stores the images. Again, this code does not run on the phone, but you can use it as a reference.
string randomGUID = locationID
+ "-"
+ Guid.NewGuid().ToString();
//Retrieve storage account from application settings
CloudStorageAccount storageAccount = GetStorageAccount();
//Create blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Retrieve reference to images container
CloudBlobContainer container = blobClient.GetContainerReference(
RoleEnvironment.GetConfigurationSettingValue("BlobContainer"));
//Retrieve references to the blob inside the container
CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomGUID);
blockBlob.UploadFromStream(imageToUpload);
The variable imageToUpload is of the type Stream.
As you can see, this is pretty straightforward code. Perhaps your problem has to do with the lambda expression you have in UploadFromStream?

Categories