在c#中从Windows azure中删除blob

时间:2015-03-26 17:38:23

标签: c# windows-phone-8 azure-storage azure-storage-blobs

我有将blob插入存储的代码,并允许用户查看blob列表和单个blob。但是,我现在无法删除blob,出现的错误是

"类型' System.ServiceModel.FaultException`1'的例外情况发生在System.ServiceModel.ni.dll中但未在用户代码中处理。附加信息:远程服务器返回错误:(404)Not Found。"

WCF服务中的代码是

public void DeleteBlob(string guid, string uri)
{
    //create the storage account with shared access key
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(accountDetails);

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(guid);

    CloudBlockBlob blob = container.GetBlockBlobReference(uri);
    blob.DeleteIfExists();
}

然后我通过SOAP服务在移动客户端应用程序中访问它:

private void mnuDelete_Click(object sender, EventArgs e)
{
    MessageBoxResult message = MessageBox.Show("Are you sure you want to delete this image?", "Delete", MessageBoxButton.OKCancel);
    if (message == MessageBoxResult.OK)
    {
        Service1Client svc = new Service1Client();
        svc.DeleteBlobCompleted += new EventHandler<AsyncCompletedEventArgs>(svc_DeleteBlobCompleted);
        svc.DeleteBlobAsync(container, uri);
    }
}
void svc_DeleteBlobCompleted(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error == null) {
        NavigationService.Navigate(new Uri("/Pages/albums.xaml", UriKind.Relative));
    }
    else {
        MessageBox.Show("Unable to delete this photo at this time", "Error", MessageBoxButton.OK);
    }
}

我也首先使用SAS令牌来保存blob - 我不知道这是否有所作为?

3 个答案:

答案 0 :(得分:6)

在Azure Storage Client Library 4.0中,我们更改了Get * Reference方法以仅接受相对地址。因此,如果您使用的是最新的库和参数&#34; uri&#34;是绝对地址,您应该将其更改为blob名称,或者应使用带有Uri和CloudBlockBlob constructor对象的StorageCredentials

请在GitHub repository

中查看所有此类重大更改

答案 1 :(得分:1)

我在ASP.NET核心MVC网络应用程序(v1.1.3)中使用 WindowsAzure.Storage(v8.1.4)

我的网络应用程序上有图像裁剪和调整大小功能,所以我决定使用Azure Blob存储来存储原始(用户上传的)图片和裁剪(调整大小后)图片。

即使您将CloudBlockBlob构造函数与绝对uri一起使用,也要记住一件重要的事情,即您仍然需要将存储帐户凭据传递到CloudBlockBlob构造

public class AzureBlobStorageService : IBlobStorageService
{
    private readonly AzureBlobConnectionConfigurations _azureBlobConnectionOptions;
    private readonly CloudStorageAccount _storageAccount;
    private readonly CloudBlobClient _blobClient;

    public AzureBlobStorageService(IOptions<AzureBlobConnectionConfigurations> azureBlobConnectionAccessor)
    {
        _azureBlobConnectionOptions = azureBlobConnectionAccessor.Value;

        _storageAccount = CloudStorageAccount.Parse(_azureBlobConnectionOptions.StorageConnectionString);
        _blobClient = _storageAccount.CreateCloudBlobClient();
    }

    public async Task<Uri> UploadAsync(string containerName, string blobName, IFormFile image)
    {
        ...
    }

    public async Task<Uri> UploadAsync(string containerName, string blobName, byte[] imageBytes)
    {
        ...
    }

    public async Task<byte[]> GetBlobByUrlAsync(string url, bool deleteAfterFetch = false)
    {
        // This works
        var blockBlob = new CloudBlockBlob(new Uri(url), _storageAccount.Credentials);

        // Even this will fail
        //var blockBlob = new CloudBlockBlob(new Uri(url));

        await blockBlob.FetchAttributesAsync();

        byte[] arr = new byte[blockBlob.Properties.Length];
        await blockBlob.DownloadToByteArrayAsync(arr, 0);

        if (deleteAfterFetch)
        {
            await blockBlob.DeleteIfExistsAsync();
        }

        return arr;
    }

    private async Task<CloudBlobContainer> CreateContainerIfNotExistAsync(string containerName)
    {
        var container = _blobClient.GetContainerReference(containerName);
        if (!await container.ExistsAsync())
        {
            await container.CreateAsync();
            await container.SetPermissionsAsync(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
        }

        return container;
    }
}

希望这有帮助。

答案 2 :(得分:0)

在Azure存储客户端库4.0中,必须将get Reference方法更改为接受相对地址,而别无其他。因此,这不支持早于此的库,

您应该将其更改为blob名称,或者应该使用带有Uri和StorageCredentials对象的CloudBlockBlob构造函数。