skip to Main Content

I have a SAS token for a specific folder, which in itself of course has more folders and blobs. I am looking to create a Service or Container client to allow me to iterate on the folders/blobs in this folder.

I’ve tried using a serviceclient, but of course it’s not authorized.

I’ve tried using a containerclient, but this fails on the URI with an invalid connection string

new BlobContainerClientBuilder().connectionString("https://{blobacct}.blob.core.windows.net/{container}/{folder}?sp=...

The above is the attempt which fails on invalid connection string.

2

Answers


  1. Looking at the sdk github documentation, you can do someting like that:

    BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
        .endpoint("<your-storage-account-url>")
        .sasToken("<your-sasToken>")
        .containerName("mycontainer")
        .buildClient();
    
    Login or Signup to reply.
  2. As per this ask,

    The Blob storage folders are virtual and generating SAS at folder level is not supported.

    So, you might be using ADLS gen2 storage account. You can use ADLS gen2 Java SDK for this. First, create DataLakeDirectoryClient like below.

    DataLakeDirectoryClient directory_Client = new DataLakePathClientBuilder() .endpoint("<storage-account-url>" + "/" + "<container_name>" + "/" + "<folder_name>" + "?" + "<SAS_of_folder>").buildDirectoryClient();
    

    The above code is referred from this doc.

    Then use listpaths() on this object to get the list of files/directories from your folder. You can go through this documentation to know more about this method.

    Coming to your error, the SAS of yours is at the folder level but to list the blobs it needs container level SAS. That is why it is giving the authorization error.

    If you want to list the files/directories using Blob storage SDK, you can achieve your requirement using the below code where it uses a recursive function and some Array lists.

    import com.azure.core.http.rest.PagedIterable;
    import com.azure.storage.blob.BlobContainerClient;
    import com.azure.storage.blob.BlobServiceClient;
    import com.azure.storage.blob.BlobServiceClientBuilder;
    import com.azure.storage.blob.models.BlobItem;
    import java.util.ArrayList;
    import java.util.List;
    
    public class App 
    {
        public static void main( String[] args )
        {
            String sasToken = "<folder_SAS>";
            String folderUrl = "https://<storage_account_name>.blob.core.windows.net/<container_name>/<folder_name>";
        
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                    .endpoint(folderUrl)
                    .sasToken(sasToken)
                    .buildClient();
    
            String containerName = "<container_name>"; 
            String folderPath = "<folder_name>/";      
    
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
            
            List<String> file_paths = new ArrayList<>();
            List<String> directory_paths = new ArrayList<>();
            
            list_all(folderPath, file_paths, directory_paths,containerClient);
    
            // Print the results
            System.out.println("File paths:");
            for (String filePath : file_paths) {
                System.out.println(filePath);
            }
    
            System.out.println("nDirectory paths:");
            for (String dirPath : directory_paths) {
                System.out.println(dirPath);
            }
            
        }
        
        public static void list_all(String name, List<String> file_paths, List<String> directory_paths,BlobContainerClient a) {
            if (name.endsWith("/")) {
                directory_paths.add(name);
                PagedIterable<BlobItem> b = a.listBlobsByHierarchy(name);
                if (b != null) {
                    for (BlobItem i : b) {
                        if (i.getName().endsWith("/")) {
                            list_all(i.getName(), file_paths, directory_paths,a);
                        } else {
                            file_paths.add(i.getName());
                        }
                    }
                }
            }
        }
    }
    

    Here, when I have used listBlobs() method or listBlobsByHierarchy("/"), I got same error as it is accessing container level information.

    Result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search