I have function app which connect to blob , read file content and post content to API. The function works perfect on debug from Visual Studio . The problem I am having is does not work from Azure when deployed . The error I ma getting is:
Exception while executing function: MyFunctionManager
Problem Id:System.ArgumentNullException at MYFUNCTION.FA.FileManager.BlobContainerManager.GetCloudBlobContainer
It seems cannot connect and find the blob storage. In the code I am getting the container using connection string set in the local.settings.json:
public static CloudBlobContainer GetCloudBlobContainer(string blobContainer)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
App.Settings.AzureFileStorageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(blobContainer);
container.CreateIfNotExistsAsync();
return container;
}
Any help appreciated
Thanks
The local.settings.json file is just for local development.
When running on Azure, make sure you have an Application Setting with key AzureFileStorageConnectionString and value to your storage account's connection string.
And you would have to do the same for the container name too since you mentioned that you are getting it from Application Settings.
Related
Is there any difference between azure storage of local environment and with online storage.
We have created a Azure local storage using storage emulator. Refer the below link.
https://learn.microsoft.com/en-us/azure/storage/common/storage-use-emulator
https://medium.com/oneforall-undergrad-software-engineering/setting-up-the-azure-storage-emulator-environment-on-windows-5f20d07d3a04
But, we are unable to access the files for (read files) Azure local storage. Refer the below code.
const string accountName = "devstoreaccount1";// Provide the account name
const string key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";// Provide the account key
var storageCredentials = new StorageCredentials(accountName, key);
var cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
// Connect to the blob storage
CloudBlobClient serviceClient = cloudStorageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference(**"container name"**);
container.SetPermissionsAsync(new
BlobContainerPermissions
{
PublicAccess =
BlobContainerPublicAccessType.Blob
});
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference("sample.txt");
blob.DownloadToFileAsync("sample.txt", System.IO.FileMode.Create);
// Get the blob file as text
string contents = blob.DownloadTextAsync().Result;
The above code works correctly for reading the files in online Azure storage. Anyone suggest how to resolve the issue in reading the files in local Azure storage.
The document explains clearly about differences between the Storage Emulator and Azure Storage.
If you would like to access the local storage, you could call this API. For more details about URI, see here.
Get http://<local-machine-address>:<port>/<account-name>/<resource-path>
i'm new to azure and i have the following issue:
i have this key in my web.config:
add key="BlobStorageConnectionString" value="xxxxxxxx"
The thing is when i add it to app settings in azure app service, when i search in the logs i get this:
Getting "BlobStorageConnectionString" from ServiceRuntime: FAIL
Getting "BlobStorageConnectionString" from ConfigurationManager: PASS.
i've already tried a few tutorials, but i still can't find a reason.
I'm running out of ideas, any suggestions?
If you add the Storage Account string in the Application Settings, it will be stored as an environment.So you could read it with Environment.GetEnvironmentVariable("storageconnectionstring").
Then parse it the code will be like below shows.
string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");
// Check whether the connection string can be parsed.
if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
{
try
{
// Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
........
........
}
}
And you could also configure your connection in your app like WebJob, you could use JobHostConfiguration(). And the code would be like this.And the connection name should be AzureWebJobsStorage.
var config = new JobHostConfiguration();
config.DashboardConnectionString = "";
Also you could choose to use Configuration Classes,about the details you could refer to this article.
Hope this could help you, if you still have other questions,please let me know.
I have logged onto to Azure Portal and created a storage account and the container to hold the images. (container has the access type set to container).
Now I'm trying to upload an image using the C# code.(Works fine on uploading an image via portal and able to access the image.)
// actName contains the storage account name and actKey contains the Access Key for that storage account.
StorageCredentials creds = new StorageCredentials(actName, actKey);
CloudStorageAccount imageStorageAccount = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient blobClient = imageStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("newimages");
container.CreateIfNotExists();
It is throwing an error:
No connection could be made because the target machine actively
refused it 51.136.40.24:443
Any input on this will be helpful.
Update 19/12/2016
It required me to change the Identity from ApplicationPoolIdentity to Custom user name and account. Is there a way to work on it without changing the ApplicationPoolIdentity?
I'm attempting to store a file in Azure Blob storage - I get no exceptions but the file does not appear. I am obviously doing something wrong. I have a basic test as follows:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mystorage1;AccountKey=1xxxxxusCw==");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myimagecontainer");
CloudBlockBlob blob = container.GetBlockBlobReference($"image.jpg");
using (var fileStream = System.IO.File.OpenRead(#"C:\temp\image.jpg"))
{
blob.UploadFromStreamAsync(fileStream);
}
Access Type is 'Blob'.
Any ideas why this would not work (obviously I have changed the account info etc)?
Thanks
The possible error is not being handled because of the async. Try awaiting the upload and/or double check how exceptions are being handled.
I develop a website on Windows Azure, MVC4. I try to upload and get photos from blobs.
My related code (that I got from other examples on web):
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
var blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("abc");
container.CreateIfNotExists();
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
My related web.config code:
<add name="StorageConnection" connectionString="DefaultEndPointsProtocol=https;AccountName=abc;AccountKey=*******"/>
When I run I got a format exception. Please note that it is web site.
As per some posts, when I put my connection string to app.config, no solution.
Thank you very much for any assistance.
You shall not use connectionStrings section to store Azure Storage Connection string!
Use appSettings instead and you will have no issues. It is confusion that the Storage credentials is also named "Connection string", but it has nothing to do with the connectionStrings section in your configuration file. connectionStrings section is intended solely for use DataBase connection strings.