Changing Metadata[“Filename”] of blob and Container when copying and deleting container

两盒软妹~` 提交于 2019-12-24 20:07:15

问题


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.

Below is my code.

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.

Below is the error message:

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?


回答1:


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.

So, just as @Gaurav Mantri said in comment, did you add a Filename metadata for each blob manually?

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();
        }
    }

In my test, the CP4.png blob's Filename metadata was updated to 123



来源:https://stackoverflow.com/questions/59152885/changing-metadatafilename-of-blob-and-container-when-copying-and-deleting-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!