Kevin
Kevin

Reputation: 11

How to create sub directory in Windows Azure blob container and rename directory

How to create sub directory in Windows Azure blob container? I know we can use blob name like "content/2015/images/mypicture.jpg".

But we need to change parent directory name. Sub directory may contain many files. Change every blob name in sub directory will take time. How to do this? I have read below links. But they don't mention how to handle rename parent directory. Thanks

Windows Azure: How to create sub directory in a blob container

How to create a sub container in azure storage location

Upvotes: 0

Views: 6032

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136366

But we need to change parent directory name. Sub directory may contain many files. Change every blob name in sub directory will take time. How to do this?

So, there are two things in blob storage:

  1. As you mentioned, there is no concept of folders there. To create an illusion of folders, you prefix the blob name with the folder you want.
  2. Blob storage does not support rename functionality natively. In order to accomplish rename, you would need to perform 2 operations on a blob: Copy+Delete.

Here's how you can rename a folder in blob storage:

  1. First, you would need to list all blobs in that folder. You can use ListBlobs method and pass in the name of the folder as prefix parameter. See my answer here: How to load list of Azure blob files recursively?.
  2. Next, you need to copy these blobs since you're just renaming the folder, you would basically create a new name of the blob by replacing the old folder name with new folder name.
  3. Once the blobs are copied, you can again iterate over your blobs and delete them one by one.

You can also combine step 2 and 3 and do copy + delete operation on each blob. So you iterate over your list of blobs, first copy it with new name, delete it and then move on to the next blobs.

The bottom line is that you would need to do Copy+Delete on each blob in the folder to rename that folder.

Upvotes: 2

Related Questions