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 based on Python 3.6.

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

  1. Initialize oss2.Bucket.
     # !/usr/bin/env python
    import oss2
    # An Alibaba Cloud account has permissions to call all API operations. If the AccessKey pair of your Alibaba Cloud account is leaked, your data may be exposed to high security risks. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
    # 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 yourEndpoint parameter to 
    endpoint = 'yourEndpoint'
    # Specify the name of the bucket. 
    bucket = oss2.Bucket(auth, endpoint, 'examplebucket')                    
  2. Upload an object.
    # Specify the full paths of the local file and the object. 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")
    # Upload the picture_old.png object.
    res2 = bucket.put_object_from_file("picture_key.png", "picture_old.png")
  3. 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")
    res4 = bucket.get_object_to_file("picture_key.png", "D:\\picture_new.png")

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
import mysql_client

def conn_client():
    auth = oss2.Auth('LTAI5tLNak1sYu8g9UT*****', 'Lyn9YLbmfQYQBgdNJAupawSHT*****')
    endpoint = ""
    bucket = oss2.Bucket(auth, endpoint, 'oss-wwjtest')

    user_id = mysql_client.fetch_one(sql)# Search for the ID of a user in your RDS instance.
    # Retrieve and download the profile picture of the user to the specified path.
    bucket.get_object_to_file(object, your_path/user_id + '.png')
    # Process the uploaded profile picture of the user.
    bucket.put_object_from_file(object, your_path/user_id + '.png')

if __name__ == '__main__':