如何从Blob容器中删除文件? [英] How to delete files from blob container?

查看:250
本文介绍了如何从Blob容器中删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private readonly CloudBlobContainer _blobContainer;

public void Remove()
{
    if (_blobContainer.Exists())
    {
       _blobContainer.Delete();
    }
}

如何删除整个容器而不是整个容器中的某些List<string> disks?

How to delete not a whole container but some List<string> disks that in the container?

推荐答案

这是我使用的代码:

private CloudBlobContainer blobContainer;

public void DeleteFile(string uniqueFileIdentifier)
{
    this.AssertBlobContainer();

    var blob = this.blobContainer.GetBlockBlobReference(fileName);
    blob.DeleteIfExists();
}

private void AssertBlobContainer()
{
    // only do once
    if (this.blobContainer == null)
    {
        lock (this.blobContainerLockObj)
        {
            if (this.blobContainer == null)
            {
                var client = this.cloudStorageAccount.CreateCloudBlobClient();

                this.blobContainer = client.GetContainerReference(this.containerName.ToLowerInvariant());

                if (!this.blobContainer.Exists())
                {
                    throw new CustomRuntimeException("Container {0} does not exist in azure account", containerName);
                }
            }
        }
    }

    if (this.blobContainer == null) throw new NullReferenceException("Blob Empty");
}

如果您知道不会同时访问该锁定代码,则可以忽略该锁定代码

You can ignore the locking code if you know this isn't going to be accessed simultaneously

很显然,您已经对blobContainer内容进行了排序,因此您所需要的只是DeleteFile方法而没有this.AssertBlobContainer().

Obviously, you have the blobContainer stuff sorted, so all you need is that DeleteFile method without the this.AssertBlobContainer().

这篇关于如何从Blob容器中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆