1
votes

Im trying to grab a virtual directory in a container by iterating through a list of "folders" blobs.

folder(prefix) 
 |
 |-->somefile.ext

I noticed that it will only grab that folder(blob) if there is a file within it.

I need to be able to grab the virtual folder even if it has no files in it so I can upload to it.

folder(prefix) 


for (ListBlobItem c : container.listBlobs("prefix")) {
if (c.getUri().toString().endsWith("/")){
//print blob
}
}
1

1 Answers

3
votes

There is no such thing as a folder in azure blob storage. So there can only be empty containers, not empty folders.

Folders are virtual and only exists due to the blobs in them. The path of the blob defines the virtual folders so that is why you cannot get a virtual folder without blobs in it.

You can "create" a new folder by setting the path of a new blob. For example by uploading a blob named "my_not_yet_existing_folder/myimage.jpg"

For example (modified example from the docs):

try
{
    // Retrieve storage account from connection-string.
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

   // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.getContainerReference("mycontainer");

    final String filePath = "C:\\myimages\\myimage.jpg";

    // Create or overwrite the "myimage.jpg" blob with contents from a local file.
    CloudBlockBlob blob = container.getBlockBlobReference("my_not_yet_existing_folder/myimage.jpg");
    File source = new File(filePath);
    blob.upload(new FileInputStream(source), source.length());
}
catch (Exception e)
{
    // Output the stack trace.
    e.printStackTrace();
}