重命名Azure容器中的System.InvalidCastException [英] System.InvalidCastException in Renaming Azure Container

查看:70
本文介绍了重命名Azure容器中的System.InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在azure blob存储中重命名一个容器.我能够成功重命名该容器.但是我注意到在某些情况下是在某些过程中.我遇到了一些错误.

I am trying to rename a container in azure blob storage. I was able to successfully rename the container. But I noticed in some few cases that during some process. I encountered some error.

这是错误消息.

System.InvalidCastException:'无法转换类型的对象 键入"Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory" "Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob".

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobDirectory' to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob'.'

下面是我的代码.

string ContainerName = "old-container-name";
    string NewContainerName = "new-container-name";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);
    CloudBlobContainer destcontainer = blobClient.GetContainerReference(NewContainerName);
    destcontainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
    IEnumerable<IListBlobItem> IE = container.ListBlobs(blobListingDetails: BlobListingDetails.Metadata);
    foreach (IListBlobItem item in IE)
    {
        CloudBlockBlob blob = (CloudBlockBlob)item;
        CloudBlockBlob destBlob = destcontainer.GetBlockBlobReference(blob.Name);
        destBlob.StartCopyAsync(new Uri(GetSharedAccessUri(blob.Name, container)));
    }

我在此行收到错误:

CloudBlockBlob blob = (CloudBlockBlob)item;

你们对此有解决办法吗?有关如何解决此问题的任何提示?

Do you guys have a fix on this one? Any tips on how to fix this?

推荐答案

出现此错误的原因是由于列出斑点的原因.

The reason you're getting this error is because of the way you're listing the blobs.

IEnumerable<IListBlobItem> IE = container.ListBlobs(blobListingDetails: BlobListingDetails.Metadata);

上面的代码行将同时列出Blob和虚拟文件夹.虚拟文件夹由CloudBlobDirectory表示.由于您尝试将类型CloudBlockBlob的对象强制转换为CloudBlobDirectory,因此会遇到此异常.

Above line of code will list both blobs and virtual folders. Virtual folders are represented by CloudBlobDirectory. Since you're trying to cast an object of type CloudBlockBlob as CloudBlobDirectory, you're getting this exception.

要列出blob容器中的所有blob,请使用以下ListBlobs方法替代:

To list all blobs in a blob container, please use the following override of ListBlobs method: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.blob.cloudblobcontainer.listblobs?view=azure-dotnet-legacy.

您需要为useFlatBlobListing参数传递true.然后,它将仅返回blob,而不返回虚拟文件夹.

You will need to pass true for useFlatBlobListing parameter. It will then return only the blobs and not virtual folders.

这篇关于重命名Azure容器中的System.InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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