【问题标题】:Changing Metadata["Filename"] of blob and Container when copying and deleting container复制和删除容器时更改 blob 和容器的元数据[“文件名”]
【发布时间】:2020-03-27 21:22:01
【问题描述】:

当满足特定条件时,我正在尝试更改容器名称和其中的特定文件。下面的第一个代码工作正常。它可以正确更改容器名称和 blob.name。问题是下面的第二个代码。

下面是我的代码。

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

在我在上面的代码中添加这个条件之前。

        if (blob.Metadata["Filename"].Contains("originalfilename"))
        {
            blob.Metadata["Filename"] = blob.Metadata["Filename"].Replace("originalfilename", "newfilename");
            targetBlob.SetMetadata();
        }

我能够更改文件的元数据[“filename”],但在检索以前没有发生的文件时遇到错误。我认为我更新元数据的方式是错误的。

以下是错误信息:

异常详细信息:System.Collections.Generic.KeyNotFoundException: 字典中没有给定的键。

关于如何解决这个问题或更改元数据的更好方法的任何提示?

【问题讨论】:

  • 您的 Blob 元数据是否包含名称为 Filename 的键?
  • 是的,我们确实使用这个 sn-p 添加了元数据。 CloudBlockBlob blockBlob = container.GetBlockBlobReference(System.Guid.NewGuid().ToString() + "_" + 文件名); blockBlob.Metadata.Add("文件名", 文件名);

标签: c# asp.net azure azure-blob-storage


【解决方案1】:

根据我的测试,Filename 不是 blob 的内置元数据。内置的是Name,您无法更改。它始终等于 blob 的名称。

所以,正如@Gaurav Mantri 在评论中所说,您是否手动为每个 blob 添加了 Filename 元数据?

如果您为您的 blob 添加了 Filename 元数据,那么您可以按以下方式获取和更新它:

    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

【讨论】:

  • 感谢 Jack 的回答。是的,我们确实使用这个 sn-p 添加了元数据。 CloudBlockBlob blockBlob = container.GetBlockBlobReference(System.Guid.NewGuid().ToString() + "_" + 文件名); blockBlob.Metadata.Add("文件名", 文件名);
  • 感谢您的提示和代码杰克。我的代码现在工作正常。非常感谢你。
猜你喜欢
  • 2019-04-13
  • 2014-11-16
  • 1970-01-01
  • 2016-07-29
  • 2021-10-09
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多