指定的容器不存在

时间:2013-09-18 04:43:11

标签: c# visual-studio-2010 azure blob azure-storage

我遇到了这个错误The specified container does not exist.

让我解释一下,

CloudBlobClient blobStorage = GetBlobStorage("upload");
CloudBlockBlob blob = BlobPropertySetting(blobStorage, Guid.NewGuid().ToString().ToLower() + Path.GetExtension(file.FileName));
blob.UploadFromStream(file.InputStream);

public static CloudBlobClient GetBlobStorage(string cloudBlobContainserName)
    {
        CloudBlobClient blobStorage;
        try
        {
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
            blobStorage = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobStorage.GetContainerReference(cloudBlobContainserName); 
            container.CreateIfNotExist();
            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);
        }
        catch (Exception ex)
        {
            Logger.LogError(Log4NetLogger.Category.Exception, "Error in : BlobHandler.GetBlobStorage :>> Exception message: " + ex.Message);
            throw;
        }
        return blobStorage;
    }
    public static CloudBlockBlob BlobPropertySetting(CloudBlobClient cloudBlobClientReferenceName, string blobContentName)
    {
        CloudBlockBlob blob = cloudBlobClientReferenceName.GetBlockBlobReference(blobContentName);


        return blob;
    }

我的StorageConnectionString

      <Setting name="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=duw;AccountKey=bla bla" />

容器'上传'和存储帐户'duw'存在。

执行blob.UploadFromStream(file.InputStream);语句会导致错误。

堆栈跟踪:

  

at Microsoft.WindowsAzure.StorageClient.Tasks.Task 1.get_Result() at Microsoft.WindowsAzure.StorageClient.Tasks.Task 1.ExecuteAndWait()      在Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImpl(Func`1 impl)      在Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(流源,BlobRequestOptions选项)      在Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(流源)      在DAL.Handlers.BlobHandler.CreateAd(HttpPostedFileBase文件,广告模型)在D:\ DU Server \ trunk \ Du Server \ DAL \ Handlers \ BlobHandler.cs:第151行

内部异常:

{"The remote server returned an error: (404) Not Found."}

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:7)

简短版

请尝试BlobPropertySetting功能的以下代码:

 public static CloudBlockBlob BlobPropertySetting(CloudBlobClient cloudBlobClientReferenceName, string blobContentName)
    {
        CloudBlockBlob blob = cloudBlobClientReferenceName.GetBlockBlobReference("upload/" + blobContentName);
        return blob;
    }

现在适用于较长版本:)

您收到此错误的原因是您在CloudBlockBlob方法中构建BlobPropertySetting对象的方式。使用代码时,它会使用以下URI创建一个blob对象:https://duv.blob.core.windows.net/blobContentName。如果你注意到,那里没有容器名称。由于没有容器名称,存储客户端库假定您尝试在$root blob容器中创建一个blob,这是一个特殊的blob容器。您可以在此处详细了解:http://msdn.microsoft.com/en-us/library/windowsazure/hh488356.aspx。由于您的存储帐户没有此容器,因此会出现404 - Resource Not Found错误。

答案 1 :(得分:1)

我来晚了,但仍然想知道我的答案是否对任何人都有用。

我通过输入正确的“容器名称”解决了该错误。默认情况下是不同的。 我已经克隆了这个GIT项目:https://github.com/Azure-Samples/storage-blob-upload-from-webapp-node

const
      express = require('express')
    , router = express.Router()
    , azureStorage = require('azure-storage')
    , blobService = azureStorage.createBlobService()
    , containerName = 'container' // added container name here, as my container name
    , config = require('../config')
;