All Products
Search
Document Center

ApsaraDB RDS:Use ApsaraDB RDS with OSS to store data in various structures

Last Updated:Apr 09, 2025

This topic describes how to use ApsaraDB RDS together with Object Storage Service (OSS) to store data in various structures.

OSS is a secure, cost-effective, and highly reliable cloud storage service that is provided by Alibaba Cloud to store large amounts of data. For example, if your application is forum software, you can store resources such as user profile pictures and forum posts in your OSS bucket. This way, you can reduce the amount of data that needs to be stored in your RDS instance.

Sample code

The sample code is written by using Python 3.6.

For more information about how to install OSS SDK for Python, see Installation.

  1. Configure environment variables.

    Linux or macOS

    export OSS_ACCESS_KEY_ID=STS.xxxxxx
    export OSS_ACCESS_KEY_SECRET=xxxxxx
    export OSS_SESSION_TOKEN=xxxxxx

    Windows

    set OSS_ACCESS_KEY_ID=STS.xxxxxx
    set OSS_ACCESS_KEY_SECRET=xxxxxx
    set OSS_SESSION_TOKEN=xxxxxx
    Note

    Temporary access credentials are obtained from Security Token Service (STS) and valid for one hour. We recommend that you use Resource Access Management (RAM) roles to automatically rotate the credentials.

  2. Initialize the oss2.Bucket class.

    #!/usr/bin/env python
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    
    # Use V4 signatures and credentials obtained from environment variables.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Enter the endpoint. The HTTPS protocol must be included.
    endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'  # In this example, the endpoint of the China (Hangzhou) region is used.
    
    # Specify a region and create a bucket.
    bucket = oss2.Bucket(
        auth,
        endpoint,
        'examplebucket',
        region='cn-hangzhou' # The region corresponding to the endpoint.
    )
  3. Upload an object.

    # Specify the full path of the object and the full path of the local file. The full path of the object cannot contain the bucket name. 
    # By default, if you do not specify the full path of a local file, the local file is uploaded from the path of the project to which the sample program belongs. 
    # Upload the info_old.txt object.
    res1 = bucket.put_object_from_file("info_key.txt", "info_old.txt")
    print("res1-{0}".format(res1.status))
        
    # Upload the picture_old.png object.
    res2 = bucket.put_object_from_file("picture_key.png", "picture_old.png")
    print("res2-{0}".format(res2.status))
  4. Retrieve the object.

    # Specify the full path of the object. The full path cannot contain the bucket name, such as info_key.txt. 
    # Download the object and save it as a file in the specified path, such as D:\\info_new.txt. If a file that has the same name already exists, the downloaded object overwrites the file. If no file that has the same name exists, a file is created. 
    res3 = bucket.get_object_to_file("info_key.txt", "D:\\info_new.txt")
    print("res2-{0}".format(res3.status))
    
    res4 = bucket.get_object_to_file("picture_key.png", "D:\\picture_new.png")
    print("res4-{0}".format(res4.status))

The Elastic Compute Service (ECS) application code specifies that user IDs are stored in your RDS instance and that user profile pictures are stored in your OSS bucket. The following example shows a Python code snippet:

#!/usr/bin/env python
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import mysql_client

def conn_client():
    sql = "SELECT id FROM users LIMIT 1" # Assume that the user ID is obtained from the users table.
    # Use V4 signatures and the standard credential provider.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com" # Modify the protocol header.
    bucket = oss2.Bucket(auth, endpoint, 'oss-wwjtest', region='cn-hangzhou')

    # Run a database query.
    user_id = mysql_client.fetch_one(sql)  # Make sure that fetch_one returns a single value.
    object_path = f"{user_id}.png"  # You must specify a valid object name.
    local_path = f"your_path/{object_path}"  # You must use the actual path.

    # Download the object.
    bucket.get_object_to_file(object_path, local_path)
    # Upload the object.
    bucket.put_object_from_file(object_path, local_path)

if __name__ == '__main__':
    conn_client()