在复制和删除容器时更改Blob和容器的Metadata ["Filename"] [英] Changing Metadata["Filename"] of blob and Container when copying and deleting container
问题描述
我正在尝试在满足特定条件时更改容器名称和其中的特定文件.下面的第一个代码可以正常工作.它可以正确更改容器名称和blob.name.问题是下面的第二个代码.
I am trying to change the container name and specific files inside of it when a certain condition was met. The 1st code below works fine. It can change the container name and blob.name correctly. The issue is the 2nd code below.
下面是我的代码.
string ContainerName = "old-container-name";
string NewContainerName = "new-container-name";
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var blobClient = storageAccount.CreateCloudBlobClient();
var sourcecontainer = blobClient.GetContainerReference(ContainerName);
var destcontainer = blobClient.GetContainerReference(NewContainerName);
destcontainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
BlobContinuationToken continuationToken = null;
do
{
var blobsResult = sourcecontainer.ListBlobsSegmented(prefix: "", useFlatBlobListing: true, blobListingDetails: BlobListingDetails.All, maxResults: 1000, currentToken: continuationToken, options: new BlobRequestOptions(), operationContext: new OperationContext());
continuationToken = blobsResult.ContinuationToken;
var items = blobsResult.Results;
foreach (var item in items)
{
string newName = "";
var blob = (CloudBlob)item;
if (blob.Name.Contains("originalfilename"))
newName = blob.Name.Replace("originalfilename", "newfilename");
else
newName = blob.Name;
var targetBlob = destcontainer.GetBlobReference(newName);
targetBlob.StartCopy(blob.Uri);
}
}
while (continuationToken != null);
sourcecontainer.DeleteIfExists();
在我在上面的代码中添加此条件之前.
Before I added this condition inside the code above.
if (blob.Metadata["Filename"].Contains("originalfilename"))
{
blob.Metadata["Filename"] = blob.Metadata["Filename"].Replace("originalfilename", "newfilename");
targetBlob.SetMetadata();
}
我能够更改文件的元数据[文件名"],但是在检索文件之前遇到了错误,而这之前是没有发生的.我认为我更新元数据的方式是错误的.
I was able to change the metadata["filename"] of the file but I encountered an error while retrieving the files which before is not happening. I think the way I update the Metadata was wrong.
下面是错误消息:
异常详细信息:System.Collections.Generic.KeyNotFoundException: 字典中不存在给定的键.
Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
关于如何解决这一问题或更改元数据的更好方法的任何技巧?
Any tips on how to fix this one or a better way to change the metadata?
推荐答案
根据我的测试,Filename
不是blob的内置元数据.内置的是Name
,您无法更改.它始终等于Blob的名称.
Based on my test, Filename
is not an inbuilt metadata of blob. The inbuilt one is Name
, which you cannot change. It always equals to the blob's name.
因此,就像@Gaurav Mantri在评论中所说的那样,您是否为每个Blob手动添加了Filename
元数据?
So, just as @Gaurav Mantri said in comment, did you add a Filename
metadata for each blob manually?
如果您为blob添加了Filename
元数据,则可以按以下方式获取和更新它:
If you have added Filename
metadata for your blobs, then you can get and update it as following:
public static void Test()
{
string connString = "your_connection_string";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("pub");
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("CP4.png");
// Important, you need to fetch all attributes first!
cloudBlockBlob.FetchAttributes();
if (cloudBlockBlob.Metadata.ContainsKey("Filename"))
{
Console.WriteLine(cloudBlockBlob.Metadata["Filename"].ToString());
cloudBlockBlob.Metadata["Filename"] = "123";
cloudBlockBlob.SetMetadata();
}
else
{
cloudBlockBlob.Metadata.Add("Filename", "CP4.png");
cloudBlockBlob.SetMetadata();
}
}
在我的测试中,CP4.png
blob的Filename
元数据已更新为123
In my test, the CP4.png
blob's Filename
metadata was updated to 123
这篇关于在复制和删除容器时更改Blob和容器的Metadata ["Filename"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!