Object Storage Service (OSS) uses a flat structure to store objects. There are no directories or file system hierarchies. All data exists as objects in buckets. However, OSS simulates a folder structure by treating object names that end with a forward slash (/) as folders. These folders let you organize objects hierarchically and simplify access control.
You can use Python SDK V1 to create, list, and delete folders.
Prerequisites
Before you begin, make sure that you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables configured. For details, see Configure access credentials using OSS SDK for Python 1.0
Usage notes
The examples use the public endpoint of the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For more information, see Regions and endpoints.
The examples create an
oss2.Bucketinstance using an OSS endpoint. To use custom domain names or Security Token Service (STS), see Initialization.
Create a folder
A folder in OSS is a 0-byte object whose name ends with a forward slash (/). Call put_object with an empty string as the content to create a folder.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables.
# Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region of your bucket.
# Example: China (Hangzhou) region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region ID. Required for signature algorithm V4.
region = "cn-hangzhou"
# Specify the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Create a folder. The name must end with a forward slash (/).
bucket.put_object('exampledir/', '')List folders
Use list_objects_v2 with the delimiter parameter set to '/' to retrieve folders at a specific level. The prefix parameter controls which level to list. Folders appear in result.prefix_list.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables.
# Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region of your bucket.
# Example: China (Hangzhou) region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region ID. Required for signature algorithm V4.
region = "cn-hangzhou"
# Specify the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# List folders at the specified level.
# - prefix: filters results to a specific path. '' means root level.
# - delimiter: '/' groups results by folder, returning only the current level.
def list_folders(bucket, prefix='', delimiter='/'):
result = bucket.list_objects_v2(prefix=prefix, delimiter=delimiter)
folders = []
# prefix_list contains folder paths at the current level.
for common_prefix in result.prefix_list:
folders.append(common_prefix)
return folders
# List all subfolders in the root folder.
folders = list_folders(bucket)
print("Folder list:", folders)Delete a folder
Deleting a folder also deletes all subfolders and objects within it. This operation cannot be undone. Proceed with caution.
The following example deletes the exampledir/ folder and all objects within it from examplebucket. The code iterates through all objects under the prefix and deletes them one by one.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables.
# Make sure OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint for the region of your bucket.
# Example: China (Hangzhou) region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region ID. Required for signature algorithm V4.
region = "cn-hangzhou"
# Specify the bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
prefix = "exampledir/"
# Delete the folder and all objects within it.
# ObjectIterator lists every object under the prefix, including nested objects.
for obj in oss2.ObjectIterator(bucket, prefix=prefix):
bucket.delete_object(obj.key)References
Create a folder: For API details, see PutObject.
Delete a folder: For API details, see DeleteObject. For the complete sample code, visit GitHub.