我有一个要求,即在创建公司时,会在我的storageaccount中创建一个关联的blob存储容器,并将容器名称设置为传入的字符串变量。我尝试过以下操作:
public void AddCompanyStorage(string subDomain)
{
//get the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
//blob client now
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//the container for this is companystyles
CloudBlobContainer container = new CloudBlobContainer("https://mystore.blob.core.windows.net/" + subDomain);
}
然而,这并没有像我预期的那样创建容器,我是否以错误的方式进行此操作?这可能吗?
干杯
答案 0 :(得分:26)
public void AddCompanyStorage(string subDomain)
{
//get the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
//blob client now
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//the container for this is companystyles
CloudBlobContainer container = blobClient.GetContainerReference(subDomain);
//Create a new container, if it does not exist
container.CreateIfNotExists();
}
关注容器名称的指南:
每个短划线( - )字符必须紧跟在前面和后面 一封信或一个数字;容器中不允许连续破折号 名。
容器名称中的所有字母都必须小写。
容器名称长度必须介于3到63个字符之间。
答案 1 :(得分:2)
CloudBlobContainer container = new CloudBlobContainer("https://mystore.blob.core.windows.net/" + subDomain);
这只是创建一个CloudBlobContainer
对象的实例。要在存储帐户中创建容器,您需要在此对象上调用CreateIfNotExists
或Create
函数。
请尝试使用此代码:
public void AddCompanyStorage(string subDomain)
{
//get the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
//blob client now
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//the container for this is companystyles
CloudBlobContainer container = blobClient.GetContainerReference(subDomain);
container.CreateIfNotExists();
}