Create container in Azure storage with c# - 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.

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

Unable to access Azure blob in LocalStorage via its URL

I'm creating a Web Application as part of an assignment I have. The task is to upload an audio clip to an ASP page and then a Web Job will then generate from it a 20 second sample which is then displayed on the same page. I will eventually need to deploy it to Azure but at the moment I'm using local storage with the Azure Storage Emulator.
The page loads correctly and the audio is uploading to the Blob Container okay and the samples are being generated and placed in the correct folder. However the samples aren't loading into the player and I'm not able to access these samples through the browser, even when using the URL I'm reading right off the blobs in storage.
When I enter that URL into the browser (with the Application and Web Job both running) I get this message displayed:
My first instinct was that I didn't have the necessary permissions to access the container. However as far as I can tell the permissions have been configured correctly. Below is my BlobStorageService class that handles the permissions for the containers:
namespace Sampler
{
public class BlobStorageService
{
public CloudBlobContainer getCloudBlobContainer()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse
(ConfigurationManager.ConnectionStrings["AzureStorage"].ToString());
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("audiostorage");
if (container.CreateIfNotExists())
{
BlobContainerPermissions permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
return container;
}
}
So as far as I can tell the permissions should be Public for the whole container and the code I used for it came directly from Microsoft Docs. Any suggestions for next course of action greatly appreciated and happy to post any more code you might need to see.
It seems that there is logic issue in your code. If the container is existed, then the permisson of the container will not be public, default is private. If we try to list the blob in the private container, will get not found issue. Please have a try to use the following code. Or we could use the Microsoft Azure Storage Exploer to set the container public and try it again.
public CloudBlobContainer getCloudBlobContainer()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorage"].ToString());
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("audiostorage");
container.CreateIfNotExists(); // remove the if condition
BlobContainerPermissions permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
return container;
}

How to create a XML file in azure storage?

I'm new to Azure storage.I want to create a container and also a blob(XML file or whatever) in it. Here is the code for creating a container but I'm having trouble how to create XML (not upload form local).
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("XXXXXXXX");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
Anyone can provide some code example would be great!!
Thanks a lot!
Once you are done with the above code to get reference of a Container, do the below:
//Create reference to a Blob that May or Maynot exist under the container
CloudBlockBlob blockBlob = container.GetBlockBlobReference("something.xml");
// Create or overwrite the "something.xml" blob with contents from a string
string s = "your XML";
await blockBlob.UploadTextAsync(s);

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.

How to delete Azure blob from asp.net web api?

Uploading images from my web api to azure blob storage works fine, altough when trying to delete them i get the following error:
"An exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll but was not handled in user code"
This is the code im using:
// 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");
// Retrieve reference to a blob named "myblob.txt".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob.txt");
// Delete the blob.
blockBlob.Delete();
The code im trying to use comes from: https://azure.microsoft.com/sv-se/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs
Any help or input highly appreciated, thanks!
You should make sure that the correct file name or container name.
I use both methods.I'm sure it works in both.
Can you try code in below line ?
var result= blockBlob.DeleteIfExists();

Categories