将文件上载到CloudBlockBlob时的访问问题 - Windows Azure

时间:2013-10-23 21:17:36

标签: azure azure-storage

我必须从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();

3 个答案:

答案 0 :(得分:16)

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

更改

blob.UploadFromFile(tempImage,FileMode.CreateNew); 

blob.UploadFromFile(tempImage,FileMode.Open); 

另外,什么是tempImage?你要么省略了那段代码,要么就是tempSlideImage。

答案 1 :(得分:1)

kwill说会有什么用,但我解决了这个问题:

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

答案 2 :(得分:1)

在我的情况下,服务器错误地识别HttpPostedFileBase的文件名。因此,直接加载输入流。

  HttpPostedFileBase file

   CloudBlockBlob blob;
    .......

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