xmorera
xmorera

Reputation: 1961

Access issue when uploading file to CloudBlockBlob - Windows Azure

I had to upgrade from Windows Azure 1.7 to 2.1. The only change in code I had

blob.UploadFromFile(tempImage); to blob.UploadFromFile(tempImage,FileMode.CreateNew);

However I am getting the following error: "Combining FileMode: CreateNew with FileAccess: Read is invalid."

Here is my code below (I added the "blob.OpenWrite();" just to try). Any ideas why I am getting this error?

  string blobUri;

        /*var acct = CloudStorageAccount.FromConfigurationSetting("ImagesConnectionString");*/

        var setting = CloudConfigurationManager.GetSetting("ImagesConnectionString");
         var acct = CloudStorageAccount.Parse(setting);

        var blobClient = acct.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName")); //.GetContainerReference("ContainerName");
        container.CreateIfNotExists(); //CreateIfNotExist

        var perms = container.GetPermissions();

        //upload blob image
        LocalResource local = RoleEnvironment.GetLocalResource("tempImages");
        string tempSlideImage = local.RootPath + mySlideName;
        myImage.Save(tempSlideImage);            

        CloudBlockBlob blob = container.GetBlockBlobReference(myImageName);
        blob.Properties.ContentType = "image/jpeg"; //photoToLoad.PostedFile.ContentType; //blob.Properties.ContentType = photoToLoad.PostedFile.ContentType;

        blobClient.ParallelOperationThreadCount = 3;
        blob.OpenWrite(); //this was added after the migration
        blob.UploadFromFile(tempImage,FileMode.CreateNew); //.UploadFile //blob.UploadFromStream(photoToLoad.FileContent);
        blobUri = blob.Uri.ToString();

Upvotes: 2

Views: 6036

Answers (3)

kwill
kwill

Reputation: 11008

The 2nd parameter to UploadFromFile (the FileMode) is referring to how you want to open the file on your local machine, not what you want to do with the blob in Azure storage. So to fix you can:

Change

blob.UploadFromFile(tempImage,FileMode.CreateNew); 

to

blob.UploadFromFile(tempImage,FileMode.Open); 

Also, what is tempImage? You either left out that portion of the code, or it should be tempSlideImage.

Upvotes: 17

1c1cle
1c1cle

Reputation: 433

In my case, the server incorrectly identifies the filename of the HttpPostedFileBase. Thus, load the inputstream, directly.

  HttpPostedFileBase file

   CloudBlockBlob blob;
    .......

   using (var fileStream = file.InputStream)
    {
        blob.UploadFromStream(fileStream);
    }

Upvotes: 1

xmorera
xmorera

Reputation: 1961

What kwill said will work, however I solved like this:

 using (var fileStream = System.IO.File.OpenRead(tempSlideImage))
        {
            blob.UploadFromStream(fileStream);
        }

Upvotes: 1

Related Questions