CreateIfnotExists throws a connection error - azure

CreateIfNotExists method throws an error:
No connection could be made because the target machine actively refused it 52.140.168.120:443
when an Application pool identity is used.
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
if (container.CreateIfNotExists())
{
BlobContainerPermissions containerPermissions = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob };
container.SetPermissions(containerPermissions);
container.CreateIfNotExists();
}
But this works on using the custom user account and password.
Is there a way to get this working using the Application Pool Identity.

Related

How to download Azure BLOB?

I am not able to download azure blob file to my local disk. Please find below the java code I am using to download the file. When I run and do a HTTP trigger test, I do not see the file downloaded into my Local path. Also, I have given the authentication as public access.And , I am able to read the contents of the txt file using the blob.downloadText(). But I am not able to download it to a file.
My requirement is to download a pdf in the Blob Storage to my Local Disk.
#FunctionName("BlobDownload-Java")
public void run1(
#HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
try {
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("doccontainer");
CloudBlockBlob blob1 = container.getBlockBlobReference("AssembledDoc.pdf");
context.getLogger().info("File Name Check 1 ----->" + blob1.getName());
CloudBlockBlob blob2 = container.getBlockBlobReference("Test.txt");
String s= blob2.downloadText();
context.getLogger().info("Text Document content ----->" + s );
File file = new File("C:\\Users\\wb541348\\Project\\Temp.txt");
blob2.downloadToFile(file.getAbsolutePath());
If you just want to download PDF file to local, here is my test code , you could have a try.
#Test
public void downloadBlob(){
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
//Create the service client object for credentialed access to the Blob service.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.getContainerReference("blobcontainer");
CloudBlob blob1 =container.getBlockBlobReference("thinking-in-java.pdf");
blob1.download(new FileOutputStream("C:\\Users\\georgec\\Documents\\" + blob1.getName()));
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
If you still have questions, please let me know.
#Prasanna, Your can't download the blob into your localpath because the pattern of your path is wrong, it should be "c:/Users/wb541348/Project/Temp.txt", there is another point to pay attention, that is the filename is needed, which means that path shouldn't be only folder, hope it benefits.
you can also click this sample of azure blob for java sdk for reference.

How do I upload multiple files to Azure blob storage using an ID parameter as a container?

I've created a storage account on Azure for uploading files to. I am using KenoUI Async upload widget which allows multiple file uploads. What I'd like to do is have the system create a container based on the passing in Id parameter if it doesn't already exist.
I'm struggling with the actual upload part however, I'm not sure how to pass the enumerable files to the storage account. Here is my code so far:
public ActionResult Async_Save(IEnumerable<HttpPostedFileBase> files, string id)
{
//Connect to Azure
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));
//Create Blob Client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Retrieve a reference to a container
CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);
try
{
// Create the container if it doesn't already exist
blobContainer.CreateIfNotExists();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
}
foreach (var file in files)
{
//This doesn't seem right to me and it's where I'm struggling
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(blobContainer, fileName);
file.SaveAs(physicalPath);
}
// Return an empty string to signify success
return Content("");
}
What I've attempted to do is create a method that connects to my Azure storage account and:
Check for the existence of a container with the same ID as the
the parameter that's passed in, if it doesn't exist, create it.
Upload the files to the existing or newly created directory and give them a prefix of the ID i.e. "_".
As Gaurav Mantri said above, we will need to use Azure Storage SDK.
For better performance, we can do as below:
Parallel.ForEach(files, file =>
{
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);
blob.UploadFromStream(file.InputStream);
file.InputStream.Close();
});
Your code will be better as below:
public ActionResult Async_Save(List<HttpPostedFileBase> files, string id)
{
// Connect to Azure
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));
try
{
//Create Blob Client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//Retrieve a reference to a container
CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);
// Create the container if it doesn't already exist
blobContainer.CreateIfNotExists();
Parallel.ForEach(files, file =>
{
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);
blob.UploadFromStream(file.InputStream);
file.InputStream.Close();
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
}
// Return an empty string to signify success
return new ContentResult();
}
More information about how to use Azure Storage SDK, we can refer to:
Get Quick Start with Storage SDK
To upload a blob in Azure Storage, you will need to use Azure Storage SDK.
Please replace the following code:
foreach (var file in files)
{
//This doesn't seem right to me and it's where I'm struggling
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(blobContainer, fileName);
file.SaveAs(physicalPath);
}
With something like:
foreach (var file in files)
{
var blob = blobContainer.GetBlockBlobReference(file.FileName);
using (var s = file.InputStream)
{
blob.UploadFromStream(s);
}
}

How to download files from Azure BLOB storage from ASP.NET MVC5

I have the ability to download files from my Azure Blob storage from a web application.
Once I get my Azure Blob storage list back, it's not downloaded.
Error entering download blob.
I would appreciate it if you could look at my code and let me know what the problem is.
public string DownloadBlob()
{
CloudBlobContainer container = GetCloudBlobContainer();
CloudBlockBlob blob = container.GetBlockBlobReference("myBlob");
using (var fileStream = System.IO.File.OpenWrite(#"C:\BlobTest"))
{
blob.DownloadToStream(fileStream);
}
return "success!";
}
I have tested your code and it works in my MVC web application as below.
public ActionResult Download()
{
ContentResult contentResult = new ContentResult();
contentResult.Content = DownloadBlob();
return contentResult;
}
public static string DownloadBlob()
{
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=leeliublob;AccountKey=O7xB6ebGq8e86XQSy2vkvSi/x/e9l9FhLqayXcbz1R+E0mIcJ5Wjkly1DsQPYY5dF2JrAVHtBozbJo29ZrrGJA==;EndpointSuffix=core.windows.net");
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference("mycontainer");
CloudBlockBlob blob = container.GetBlockBlobReference("4.PNG");
using (var fileStream = System.IO.File.OpenWrite(#"C:\Test\BlobTest.PNG"))
{
blob.DownloadToStream(fileStream);
}
return "success!";
}
catch(Exception ex)
{
return ex.StackTrace;
}
}
I think this is the problem about the permission of your MVC application.
Your MVC application may use a virtual account of IIS as below, it may have no access to your C disk.
To solve this problem, here is two ways:
Please try to download to other disk like "E" or "D".
Please add the user group "Authenticated Users" to that file or folder like below.
Update:
I have reproduced your problem as below:
We need to create a folder under root directory of C disk as below, because your web application doesn't have the permission to the root directory of C disk:

Azure blob download to stream has errors

I am trying to download an Azure blob to MemoryStream, and I am getting error "'memoryStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'". But I am able to download to file.
The code snippet is below
private static void GetExcelBlobData(string filename, string connectionString, string containerName)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Retrieve reference to a blob named "test.xlsx"
CloudBlockBlob blockBlobReference = container.GetBlockBlobReference(filename);
DataSet ds;
using (var memoryStream = new MemoryStream())
{
//downloads blob's content to a stream
blockBlobReference.DownloadToStream(memoryStream);
}
}
Debug console error on line "blockBlobReference.DownloadToStream(memoryStream)" is shown
Does anyone know what I'm doing wrong here?

Upload and retrieve file from blob storage

I want to upload file in azure blob storage and retrieve that file in my own application.
My Code:
protected void btnUploadtoCloud_Click(object sender, EventArgs e)
{
UploadFile();
}
public string UploadFile()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("vest");
blobContainer.CreateIfNotExist();
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Container;
blobContainer.SetPermissions(containerPermissions);
CloudBlob blobText = blobContainer.GetBlobReference(fupPicture.FileName);
blobText.Properties.ContentType = fupPicture.PostedFile.ContentType;
string blobURL = blobText.Uri.ToString();
blobText.UploadFromStream(fupPicture.FileContent);
imgTest.ImageUrl = blobURL;
return blobURL;
}
But my problem is first line of UploadFile.I include image because I want to show my ref dll too.
.
Why error in RoleEnvironment. May i missing some thing to include.
Note : I have window azure account and I deploy other sites easily.
EDIT
After Gaurav suggestion I try :
string cnn = "<add name=\"testconnectionstring\" DefaultEndpointsProtocol=\"http;AccountName=mystorage;AccountKey=A9Jlajksdh69szut9zOfsxWMD/qLaP3Gy6ID9mIarNmCDasdhlyNlAR9wV0NGjqJUZeM4x6x5bTumLv+1nC51clHDl5GTYYPA==" + " />";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(cnn);
but unable to work I got error invalid combination.
The reason you're getting this error is because RoleEnvironment is only available when your code is running in context of a Cloud Service (Web Role) either in local compute emulator or in cloud.
Since it is a web application and I am guessing that you will be deploying it in an Azure Website, you can simply use AppSettingsReader to read from Web.Config file. Something like:
var rdr = new AppSettingsReader();
string connectionString = (string) rdr.GetValue("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", typeof(String));
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
If your application is connecting to a cloud storage account, your connection string would look something like:
DefaultEndpointsProtocol=https;AccountName=your-account-name;AccountKey=your-account-key
For connecting to local storage emulator, your connection string would look like:
UseDevelopmentStorage=true

Resources