All Products
Search
Document Center

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

Last Updated:Aug 28, 2023

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, 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 Create an OSSClient instance.

  • 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";

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

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