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);
Related
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.
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()
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.
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.
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();