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:
