2

我不得不从 Windows Azure 1.7 升级到 2.1。我唯一的代码更改

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

但是我收到以下错误:“将 FileMode:CreateNew 与 FileAccess 结合:读取无效。”

下面是我的代码(我添加了“blob.OpenWrite();”只是为了尝试)。任何想法为什么我会收到此错误?

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

3 回答 3

17

UploadFromFile 的第二个参数(FileMode)是指您希望如何在本地计算机上打开文件,而不是您希望对 Azure 存储中的 blob 执行什么操作。所以要修复你可以:

改变

blob.UploadFromFile(tempImage,FileMode.CreateNew); 

blob.UploadFromFile(tempImage,FileMode.Open); 

另外,什么是 tempImage?您要么省略了那部分代码,要么它应该是 tempSlideImage。

于 2013-10-24T04:30:00.483 回答
1

kwill所说的会起作用,但是我这样解决了:

 using (var fileStream = System.IO.File.OpenRead(tempSlideImage))
        {
            blob.UploadFromStream(fileStream);
        }
于 2013-10-25T16:25:20.327 回答
1

在我的例子中,服务器错误地识别了 HttpPostedFileBase 的文件名。因此,直接加载输入流。

  HttpPostedFileBase file

   CloudBlockBlob blob;
    .......

   using (var fileStream = file.InputStream)
    {
        blob.UploadFromStream(fileStream);
    }
于 2014-01-17T23:13:34.667 回答