All Products
Search
Document Center

Object Storage Service:Manage directories

Last Updated:Apr 24, 2025

Object Storage Service (OSS) uses a flat structure instead of a hierarchical structure that is used by traditional file systems to store objects. All data in OSS are stored as objects in buckets. To facilitate object management, the OSS console displays objects whose names end with a forward slash (/) as directories. Directories are similar to folders in file systems. You can use directories to hierarchically organize and group objects and facilitate access control.

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, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure the 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 Initialization.

Create directories

Below is the sample code for creating a directory:

# -*- coding: utf-8 -*-

import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the directory. The directory name must end with a forward slash. 
bucket.put_object('exampledir/', '')

List directories

Below is the sample code for obtaining all subdirectories in the root directory.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# List directories
def list_directories(bucket, prefix='', delimiter='/'):
    result = bucket.list_objects_v2(prefix=prefix, delimiter=delimiter)
    directories = []

    # Obtain the directory at the current level.
    for common_prefix in result.prefix_list:
        directories.append(common_prefix)

    return directories

# Invoke the function to obtain all subdirectories in the root directory.
directories = list_directories(bucket)
print("Directory list:", directories)

Delete directories

Warning

If you delete a directory, the subdirectories and all objects in the directory are synchronously deleted. Exercise caution when you delete a directory.

Below is the sample code for deleting a directory named log and all objects within from examplebucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
prefix = "exampledir/"

# Delete the directory and all objects stored within. 
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
    bucket.delete_object(obj.key)

Related topics

  • Create directories

    For more information about the API operation that you can call to create a directory, see PutObject.

  • Delete directories

    • For the complete sample code for deleting a directory and all objects stored within, visit GitHub.

    • For more information about the API operation that you can call to delete a directory and all objects stored within, see DeleteObject.