0

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

1 Answer 1

2

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.

3
  • Thanks. But use Copy+Delete may take a lot of time. One 'rename' request may need hundreds of 'Copy+Delete' request. It looks strange that Azure blob system not support directory concept as window file system。
    – Kevin
    Jun 3, 2015 at 9:52
  • I would agree that it may take a lot of time if you have lots of blobs. But unfortunately as of today, it is the only option. I would have suggested Azure File Service which closely mimics directory concept but that one is not as feature rich as blob service as of today. Jun 3, 2015 at 9:59
  • Hi Gaurav, we want to use Azure storage like window file system. e.g. list file/directory, rename file/directory, get directory size... What Azure service we need to use? Use Azure blob is a headache. It need to manually use table to record file tree, list all blob to calculate "directory" size, "lock system" to forbid uploading same file at the same time ... Thanks.
    – Kevin
    Jun 8, 2015 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.