Skip to content
Advertisement

Azure Blob Storage Java SDK 12 extract file name from blob name

let’s say I have blob structure like this:

dir0
├── dir1
│ ├── file11
│ └── file12
├── dir2
└── dir3

After listing the dir1 that contains two files file11, file12 with listBlobsByHierarchy("dir0/dir1/") method I’m getting two blobs with blobName property set to dir0/dir1/file11 and dir0/dir1/file12, so <virtual_path>/<file_name>. That’s OK, but in some point I need to get the <file_name> part.

My question is: Does Azure Storage SDK 12 for Java provides a mechanism for obtaining <file_name> part from blob name? I could do it myself but it’s hard to believe that SDK doesn’t provide this functionality… Maybe you guys know different elegant way to do so? How do you usually handle such things?

It’s a bit weird to me that SDK seems not to support building prefixes or getting file names from blob names 😛 I expected sth like fileName field in BlobItem class or buildPrefix(List<String> segments) method for joining path segments with ‘/’.

Advertisement

Answer

You just do:

blobName.replaceFirst("^/dir0/dir1/", "")

Directories in blob storage are not real, the structure is flat and there are only some helper methods that take prefixes into account. See this explanation for more details.

You can always enable hierarchical namespace (aka. Datalake Storage) in your blob account. This makes blob containers more like traditional filesystem with real directories. You are using a different SDK then (->docs). And its DirectoryEntry object differentiates between name (without path) and fullName (with path).

Advertisement