1

How do I create a connection between asp.net web app hosted on azure and Blob? The connection works perfectly when I upload files from my localhost to blob. But when i publish the web app on azure, the app is unable to store file in the blob. The only reason I can think why the file does not get uploaded is because of some sort of connection issue between the hosted asp.net web app and the blob

BlobStorageServices.cs

     public class BlobStorageServices
{
    public CloudBlobContainer GetCloudBlobContainer()
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("ImageStorageAccountConn"));
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("myimages");
        if (blobContainer.CreateIfNotExists())
        {
            blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        }
        return blobContainer;
    }
}

HomeController.cs

    BlobStorageServices _blobStorageService = new BlobStorageServices();
    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase image)
    {
        if (image.ContentLength > 0)
        {
            CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(image.FileName);
            blob.UploadFromStream(image.InputStream);
        }
        return RedirectToAction("Index");
    }

Index.cshtml

    <h2>Upload Image</h2>
    <p>
    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" name="image" />
        <input type="submit" value="Upload" />
    }
    </p>

Not sure if the BlobStorageServices.cs file gets uploaded to azure when we publish the asp.net app via visual studio. ImageStorageAccountConn is the connecting string.

2
  • Is this running in a web/worker role instance? Or VM? Or Web App (app service)? You mentioned web app, but you have a call to RoleEnvironment Commented Apr 11, 2017 at 20:55
  • 1
    Thanks David. I removed RoleEnvironment and it works now. I was struggling with this since last 2 days. :/ Commented Apr 11, 2017 at 21:03

1 Answer 1

1

Assuming you're actually using Web App (per your question): You are attempting to use the RoleEnvironment object to get the connection string. RoleEnvironment is specific to classic Cloud Services (web/worker role instances).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.