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