创建动态blob容器名称

时间:2014-10-14 21:45:34

标签: asp.net-mvc azure asp.net-mvc-5 azure-storage-blobs

我正在尝试创建一个blob容器,其名称基于作为参数传递给函数的值(AssetID)。

    public CloudBlobContainer GetCloudBlobContainer(int id)
    {

        // Retrieve storage account from connection-string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);

        // Create the blob client 
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container 
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("AssetDocs" + id);

        try
        {

            // Create the container if it doesn't already exist
            if (blobContainer.CreateIfNotExist())
            {

                blobContainer.SetPermissions(
                   new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }
                );
            }
        }
        catch (WebException e)
        {
            return blobContainer;
        }

        return blobContainer;
    }

当我运行代码时,我得到以下异常:

An exception of type 'Microsoft.WindowsAzure.StorageClient.StorageClientException' occurred in Microsoft.WindowsAzure.StorageClient.dll but was not handled in user code

Additional information: One of the request inputs is out of range.

我尝试添加try ... catch但是这似乎没有捕获异常,所以我可以找到更多细节。

如果尝试添加id.ToString()并完全删除id,但这也不起作用。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

破解了!容器名称必须为小写。

 // Retrieve a reference to a container 
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("assetdocs" + id);