指定的容器不存在

本文关键字:不存在 | 更新日期: 2023-09-27 18:31:38

我被这个错误困住了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)
{
    return cloudBlobClientReferenceName.GetBlockBlobReference(blobContentName);
}

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()at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImpl(Func'1 impl)at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(Stream source, BlobRequestOptions options)at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(Stream source)在达尔。Handlers.BlobHandler.CreateAd(HttpPostedFileBase file, Advertisement model) in D:''DU Server''trunk''Du Server''DAL''Handlers''BlobHandler.cs:line 151

内部异常:

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

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

指定的容器不存在

简短版本

尝试以下代码来BlobPropertySetting函数:

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

现在查看更长的版本:)

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

我很晚了,但仍然认为我的答案是否对任何人都有用。

我通过输入正确的"容器名称"解决了此错误。默认情况下是不同的。我已经克隆了这个 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')
;

确保还正确设置了 Blob 名称。尝试上传到缺少 Blob 名称的有效容器时,也会返回"404 未找到"。