c# Azure Cannot set the blob tier - c#

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.

Related

Copy Azure CloudPageBlob to CloudBlockBlob

Have a load of Azure page blobs in an Azure storage account which need copying to block blobs so they can be set from "cold" to "Archive" storage tier. The page blobs are created by SQL server (pre 2016) BACKUP TO URL.
I spoke to Microsoft about this and they suggested AzCopy with Azure File blob as a stepping stone, e.g. Page blob -> block blob (file storage) -> block blob (Blob storage). This all works fine in AzCopy or GUI based Azure tools but I need to automate it and build in some resilience.
Problem I have is I cannot get a CloudPageBlob to copy to CloudBlockBlob
not matter how I cast it. This is a cut-down example to show (lack of) progress and how I'm going about this. I will wrap this in a Task<> in production but I cannot get the .StartCopy to work. Any ideas?
CredentialEntity ce = Utils.GetBlobCredentials();
StorageCredentials sc = new StorageCredentials(ce.Name, ce.Key);
CloudStorageAccount storageAccount =
new CloudStorageAccount(new StorageCredentials(ce.Name, ce.Key), true);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer sourceContainer = cloudBlobClient.GetContainerReference(#"archive");
CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference(#"xfer");
CloudPageBlob source = sourceContainer.GetPageBlobReference("test.bak");
CloudBlockBlob target = targetContainer.GetBlockBlobReference("new.bak");
//target.StartCopy(source);
//target.StartCopyAsync()

How to get all files from a directory in Azure BLOB

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"))

Storing a string in Azure blob storage and getting a reference to the blob with C#

guys,
I've got code making an API call, getting JSON back, parsing it and storing it in an SQL table. Now I'd like to take that JSON and also save it in Azure blob storage, then save a reference to it in the SQL table as a separate field.
So far, I've got this:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("MyConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mydata");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(dataString);
blockBlob.UploadTextAsync(resultContent);
What can I save as the reference string, for future retrieval of the blob? Is it blockBlob?
Every blob will have a uri like:
https://<yourstorageaccountname>.blob.core.windows.net/containername/blobname
This is what you would store in your database. You can even use this uri to provide a direct link, within your app, to an end-user directly (e.g. as a link in a web page). Note: for the direct-link to work externally (e.g. without storage account credentials), the container would need to be public, or you'd need to create a Shared Access Signature to allow for temporary public access, if it's in a private container.
Arguably, you can store just the container/blob name, but then, if you have multiple storage accounts, you'll need to know which one you stored it in.
Your code can be changed like following:
CloudBlockBlob blockBlob = container.GetBlockBlobReference("datastring.json");
You can later retrieve this with the same name i.e. datastring.json, and you can save the same blob name in your database.

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