All Products
Search
Document Center

Object Storage Service:Check whether the hierarchical namespace feature is enabled

Last Updated:Jun 26, 2025

If the hierarchical namespace feature is enabled for a bucket, you can perform directory-related operations in the bucket, such as creating directories. This topic describes how to check whether the hierarchical namespace feature is enabled for a bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.

  • To check whether the hierarchical namespace feature is enabled, you must have the oss:GetBucketInfo permission. For more information, see Common examples of RAM policies.

Sample code

The following sample code provides an example on how to check whether the hierarchical namespace feature is enabled for the examplebucket bucket.

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
nvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. 
String bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
String region = "cn-hangzhou";

// Create an OSSClient instance. 
// Call the shutdown method to release associated resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)               
.build();

// Query the bucket information. 
BucketInfo info =  ossClient.getBucketInfo(bucketName);
// Check whether the value of HnsStatus is Enabled. If the value of HnsStatus is Enabled, the hierarchical namespace feature is enabled for the bucket. 
System.out.println("Hnstatus:" + info.getBucket().getHnsStatus());

// Shut down the OSSClient instance. 
ossClient.shutdown();