简体   繁体   中英

Accessing Blob Name(s) for a Azure Container without Regex

I am trying to access just the Azure Blob's file name. For example if I am currently returning the string privatecontainer/Text-pc.txt and I am trying to just access the Text-pc.txt . The catch is that each Azure Container has different files within it. I have listed the code that I am using the generate a List of Blobs from a user specified Azure Container.

MainRun()

 public static void MainRun()
    {
        var userInput = GetContainerNameFromUser();
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("STORAGE_CONNECTION_STRING"));

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = blobClient.GetContainerReference(userInput);

        foreach (var blobItem in container.ListBlobs(useFlatBlobListing: true))
        {
          Console.WriteLine(blobItem.Parent.Uri.MakeRelativeUri(blobItem.Uri));
         // The line above for-loop will access all the Blobs within a Container
        }
    }

Is there a way to just access the Text-pc.txt without having to use regex? The reason I want to stay away from regex is that the for loop above successfully prints out the file name when there are Virtual Folders/Directories in place. However, on a Container that has Blobs without Virtual Folders/Directories it will print publiccontainer/Text-pc.txt ie (<container_name>/File_name) .

You can use substring as I think it's safe to assume that the blob name won't have a / character in it. This way you can just take the substring of everything after the final slash. LastIndexOf is perfect for this. This is pretty similar to a regex method though.

If that doesn't work you can use the Segments property of the Uri. The last one should be your filename

Two ways of doing this added into your loop below:

    foreach (var blobItem in container.ListBlobs(useFlatBlobListing: true))
    {
      Console.WriteLine(blobItem.Parent.Uri.MakeRelativeUri(blobItem.Uri));

      //Segments Method
      Console.WriteLine(blobItem.Uri.Segments.Last());

      //Substring Method
      string filename = blobItem.AbsolutUri.Substring(det.Filename.LastIndexOf("/") + 1)
      Console.WriteLine(fileName);
    }

Another solution could be to cast blobItem as CloudBlob and use its Name property. Something like:

        foreach (var blobItem in container.ListBlobs(useFlatBlobListing: true))
        {
                            Console.WriteLine((blobItem as CloudBlob).Name);
        }

Please note that for blobs in virtual folders will print the full path ( virtual-folder-name/file-name ) as that's the name of the blob. If you just want the file name, you may want to apply the solution proposed by Stephen and split the string using / delimiter.

Add a reference to System.Linq and

var str = "privatecontainer/Text-pc.txt";
var splittedStr = str.Split('/');
var name = splittedStr.Last();

You'd probably want to make it a bit more robust.

如果您的文件名没有斜线,因为没有办法区分文件夹或文件名,那么您只需查找最后一个斜线并获取字符串的其余部分:

var cleanFileName = fileName.Substring(fileName.LastIndexOf("/") + 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM