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. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or STS, see Create an OSSClient instance.
  • The oss:GetBucketInfo permission is required to check whether the hierarchical namespace feature is enabled. For more information, see Attach a custom policy to a RAM user.

Sample code

The following code provides an example on how to check whether thehierarchical 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";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 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();