Object Storage Service (OSS) uses a flat structure instead of a hierarchical structure used by traditional file systems to store objects. All data in OSS is stored as objects in buckets. The OSS console displays objects whose names end with a forward slash (/) as directories, similar to folders in file systems. You can use directories to organize objects hierarchically and simplify access control.
Notes
-
This topic uses the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, see Regions and endpoints.
-
This topic obtains access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.
-
This topic creates an OSSClient instance by using an OSS endpoint. To create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Create directories
The following sample code shows how to create 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
The following sample code shows how to list 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
Deleting a directory synchronously deletes all subdirectories and objects within it. Proceed with caution.
The following sample code shows how to delete a directory named log and all objects within it 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 the API operation used to create a directory, see PutObject.
-
Delete directories
-
For the complete sample code that deletes a directory and all objects within it, visit the aliyun-oss-python-sdk repository.
-
For the API operation used to delete a directory and all objects within it, see DeleteObject.
-