可以枚举并查看存储帐户中的blob但无法访问它

时间:2016-06-05 17:36:55

标签: c# azure azure-storage azure-storage-blobs

我正在为我的本地VS工作(2013年为一个项目,2015年为另一个项目。) 我不熟悉blob和安全性,我不得不选择其他人的上传速度很慢的项目,因为他们从客户端浏览器上传到blob存储,然后获取blob,创建媒体资产并将资产编码为mp4 。好吧,我正在尝试将媒体资产的编码和创建纳入后台进程。

当我运行localhost时,我可以毫无问题地上传并保存到blob存储。我可以在Azure门户中看到blob,甚至将blob上的访问类型设置为容器。但是当我尝试从后台存储中检索blob时,看起来我正在获取blob的句柄,但是当我尝试调用blob.FetchAttributes()时,我得到了404.我正在使用正确的存储连接串。有一个SAS令牌添加到URL(适用于1年),我尝试使用和不使用令牌。

我写了下面显示的这个快速控制台应用程序,当枚举容器中的blob时,我看到我上传的blob,并且它具有正确的大小。然而,每次调用blob.Exists()都会失败。我用于fileName var以获取blob引用的编辑URL正在直接从Azure门户复制。我的连接字符串中有正确的凭据,所以我不确定这里发生了什么。任何解释都将不胜感激。

private static void ListBlobs()
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString"));
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    var container=  blobClient.GetContainerReference("streamingfiles");

    // Loop over items within the container and output the length and URI.
    foreach (IListBlobItem item in container.ListBlobs(null, false))
    {
        if (item.GetType() == typeof(CloudBlockBlob))
        {
            CloudBlockBlob blob = (CloudBlockBlob)item;
            Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
        }
    }

    var fileName = "https://redacted.blob.core.windows.net/streamingfiles/47a49fb7-6f44-4f56-9695-37a4ddcd0f4a_56.mp4";//tried also with sas token
    var sourceCloudBlob = container.GetBlockBlobReference(fileName);

    if (sourceCloudBlob.Exists())
        Console.WriteLine("Exists");
    else
        Console.WriteLine("Does Not Exist"); //alwayd doesn't exist
}

2 个答案:

答案 0 :(得分:0)

container.GetBlockBlobReference(fileName).Exists();的替代方案 将flatBlobListing标志设置为true,因为blob结构是平面层次结构。

// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, true))
{
    if (item.GetType() == typeof(CloudBlockBlob))
    {
        CloudBlockBlob blob = (CloudBlockBlob)item;

        if (blob != null)
        {
            blob.FetchAttributes();
            Console.WriteLine("Fetching Attributes");
            string blobFilePath = blob.Uri.AbsolutePath.ToString();

            if(String.compare(blobFilePath, filename, true) == 0) 
            {
                Console.WriteLine("Exists");
            }
            else
            {
                Console.WriteLine("Does Not Exist")
            }

            Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
        }
    }
}

答案 1 :(得分:0)

我是个白痴。当我已经在容器中时,将完整路径传递给文件的重点是什么:    var fileName =“47a49fb7-6f44-4f56-9695-37a4ddcd0f4a_56.mp4”;

Dev-One让我停下来思考它,但我无法将其标记为正确。

相关问题