首页 > 解决方案 > 尝试写入 Blob 存储时出现 Head 404 依赖问题

问题描述

我有公共访问的存储和内部 Blob。但是当我尝试编写文档时,实时遥测显示在依赖错误下方。

下午 1 点 21 分 57 秒 | 依赖 | 404 | 65 头图像a| LogLevel=信息 | 斑点=255274.jpg

时间:下午 1 点 21 分 57 秒

持续时间:65 毫秒

传出命令:HEAD imagesa

结果代码:404

fileName =imageURL.Substring(imageURL.LastIndexOf(@"/") + 1);
var req = System.Net.WebRequest.Create(imageURL);
            using (Stream filestream = req.GetResponse().GetResponseStream())
            {
                // Get the reference to the block blob from the container
                CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
            //create a snapshot
            bool existsTask = await blockBlob.ExistsAsync();
            if (existsTask == true)
            {
                //  the base blob's metadata is copied to the snapshot.
                await blockBlob.CreateSnapshotAsync();
                blockBlob.Metadata.Clear();
            }
}

标签: azureazure-blob-storage

解决方案


我无法在控制台应用程序中使用相同的代码重现您的问题(如果您使用某些特殊设置/环境运行代码,请指出)。

请确保以下几点:

1.检查您是否已在 azure 门户中将 blob 访问权限设置为公共,并检查您的代码是否使用相同的 blob/容器。

2.请使用最新版本的WindowsAzure.Storage软件包,9.3.3。

还有一件事你还需要知道:在代码之后blockBlob.Metadata.Clear(),你需要使用blockBlob.SetMetadata()。否则它不会清除元数据。

我使用的代码:

            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("account", "key"), true);
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            var cloudBlobContainer = cloudBlobClient.GetContainerReference("test-2");

            var imageURL = "https://xx.blob.core.windows.net/test-2/sample.JPG";

            var fileName = imageURL.Substring(imageURL.LastIndexOf(@"/") + 1);
            var req = System.Net.WebRequest.Create(imageURL);
            using (Stream filestream = req.GetResponse().GetResponseStream())
            {
                CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);

                bool existsTask = await blockBlob.ExistsAsync();

                if (existsTask == true)
                {                    
                    await blockBlob.CreateSnapshotAsync();                   
                    blockBlob.Metadata.Clear();
                    blockBlob.SetMetadata(); // add this line of code to ensure the changes to metadata is committed.
                }
            }

如果您有更多问题,请告诉我。


推荐阅读