Mit*_*tch 10 azure azure-storage azure-storage-blobs azure-blob-storage asp.net-core
以前在使用 Azure Blob 存储 SDK V11 时,如果您想创建容器但不确定容器是否存在,则可以使用 CreateIfNotExists。
但是在 V12 版本中,CreateIfNotExists 不再可用,我从 Microsoft 找到的唯一示例是简单地创建一个 Container,而不检查它是否已经存在。
那么,有没有人知道 V12 中在尝试创建容器之前检查容器是否存在的最佳实践。
顺便说一句,我正在为 ASP.Net Core 3.1 开发。
Iva*_*ang 19
在 v12 中,有两种方法可以检查容器是否存在。
1.
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
//get a BlobContainerClient
var container = blobServiceClient.GetBlobContainerClient("the container name");
//you can check if the container exists or not, then determine to create it or not
bool isExist = container.Exists();
if (!isExist)
{
container.Create();
}
//or you can directly use this method to create a container if it does not exist.
container.CreateIfNotExists();
Run Code Online (Sandbox Code Playgroud)
您可以直接创建一个BlobContainerClient,然后使用下面的代码:
//create a BlobContainerClient
BlobContainerClient blobContainer = new BlobContainerClient("storage connection string", "the container name");
//use code below to check if the container exists, then determine to create it or not
bool isExists = blobContainer.Exists();
if (!isExist)
{
blobContainer .Create();
}
//or use this code to create the container directly, if it does not exist.
blobContainer.CreateIfNotExists();
Run Code Online (Sandbox Code Playgroud)
接受的答案很好。但我通常使用它的异步版本。
var _blobServiceClient = new BlobServiceClient(YOURCONNECTIONSTRING);
var containerClient = _blobServiceClient.GetBlobContainerClient(YOURCONTAINERNAME);
await containerClient.CreateIfNotExistsAsync(Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer);
Run Code Online (Sandbox Code Playgroud)
我使用的版本是Azure.Storage.Blobs v12.4.1
| 归档时间: |
|
| 查看次数: |
8243 次 |
| 最近记录: |