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

前端 未结 9 1704
清酒与你
清酒与你 2020-11-27 16:01

How to create a sub directory in a blob container

for example,

in my blob container http://veda.blob.core.windows.net/document/

If I store some files it

相关标签:
9条回答
  • 2020-11-27 16:15

    In Azure Portal we have below option while uploading file :

    0 讨论(0)
  • 2020-11-27 16:18

    There is actually only a single layer of containers. You can virtually create a "file-system" like layered storage, but in reality everything will be in 1 layer, the container in which it is.

    For creating a virtual "file-system" like storage, you can have blob names that contain a '/' so that you can do whatever you like with the way you store. Also, the great thing is that you can search for a blob at a virtual level, by giving a partial string, up to a '/'.

    These 2 things, adding a '/' to a path and a partial string for search, together create a virtual "file-system" storage.

    0 讨论(0)
  • 2020-11-27 16:25

    To add on to what Egon said, simply create your blob called "folder/1.txt", and it will work. No need to create a directory.

    0 讨论(0)
  • 2020-11-27 16:25

    There is a comment by @afr0 asking how to filter on folders..

    There is two ways using the GetDirectoryReference or looping through a containers blobs and checking the type. The code below is in C#

    CloudBlobContainer container = blobClient.GetContainerReference("photos");
    
    //Method 1. grab a folder reference directly from the container
    CloudBlobDirectory folder = container.GetDirectoryReference("directoryName");
    
    //Method 2. Loop over container and grab folders.
    foreach (IListBlobItem item in container.ListBlobs(null, false))
    {
        if (item.GetType() == typeof(CloudBlobDirectory))
        {
            // we know this is a sub directory now
            CloudBlobDirectory subFolder = (CloudBlobDirectory)item;
    
            Console.WriteLine("Directory: {0}", subFolder.Uri);
        }
    }
    

    read this for more in depth coverage: http://www.codeproject.com/Articles/297052/Azure-Storage-Blobs-Service-Working-with-Directori

    0 讨论(0)
  • 2020-11-27 16:28

    As @Egon mentioned above, there is no real folder management in BLOB storage.

    You can achieve some features of a file system using '/' in the file name, but this has many limitations (for example, what happen if you need to rename a "folder"?).

    As a general rule, I would keep my files as flat as possible in a container, and have my application manage whatever structure I want to expose to the end users (for example manage a nested folder structure in my database, have a record for each file, referencing the BLOB using container-name and file-name).

    0 讨论(0)
  • 2020-11-27 16:31

    You do not need to create sub directory. Just create blob container and use file name like the variable filename as below code:

    string filename = "document/tech/user-guide.pdf";
    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);
    CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(filename);
    blob.StreamWriteSizeInBytes = 20 * 1024;
    blob.UploadFromStream(fileStream); // fileStream is System.IO.Stream
    
    0 讨论(0)
提交回复
热议问题