0

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 2

0

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();
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work for a specific folder, unfortunately
0

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

7 Comments

How come you can select the hamburger menu on a folder in a container and 'generate SAS token' on it?
I have used Gen 2 storage account only. In gen 2 you can generate token by selecting the Hamburger menu. In Blob, it doesn't support. For Gen 2, you can also use Blob SDK like I have shown above. Is your storage Gen2 Or Blob?
This is throwing http 401 when I try to use your sample code; this is a 'StorageV2' storage account (general purpose v2)
Is the hierarchical namespace enabled on your storage account? If you are able to get SAS of a specific folder, then Is my second code working for you?
No, your second code is saying HTTP 401 error, I have tried it exactly. I have verified the SAS token is correct as via browser I am able to download blobs in that folder. The a.listBlobsByHierarchy({folder}) throws the 401, in your list_all method
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.