This topic was translated by AI and is currently in queue for revision by our editors. Alibaba Cloud does not guarantee the accuracy of AI-translated content. Request expedited revision

Use ApsaraDB RDS with OSS to store data in various structures

Updated at: 2025-03-26 18:04

You can use ApsaraDB RDS together with OSS to store data in different structures.

OSS is a secure, cost-effective, and highly reliable cloud storage service provided by Alibaba Cloud for storing large amounts of data. You can combine ApsaraDB RDS with OSS for your applications. 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 approach reduces the amount of data that needs to be stored in your RDS instance.

Sample code

The sample code is written in Python 3.6.

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

  1. Configure environment variables.

    Linux/macOS
    Windows
    export OSS_ACCESS_KEY_ID=STS.xxxxxx
    export OSS_ACCESS_KEY_SECRET=xxxxxx
    export OSS_SESSION_TOKEN=xxxxxx
    set OSS_ACCESS_KEY_ID=STS.xxxxxx
    set OSS_ACCESS_KEY_SECRET=xxxxxx
    set OSS_SESSION_TOKEN=xxxxxx
    Note

    Temporary credentials must be obtained through the Security Token Service (STS). The validity period is typically 1 hour. We recommend that you use a RAM role to automatically rotate credentials.

  2. Initialize oss2.Bucket.

    #!/usr/bin/env python
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    
    # Use V4 signature and environment variable credentials
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Enter the actual endpoint (must include the HTTPS protocol)
    endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'  # China (Hangzhou) example
    
    # You must specify the region and create a bucket instance
    bucket = oss2.Bucket(
        auth,
        endpoint,
        'examplebucket',
        region='cn-hangzhou'  # The region that corresponds to the endpoint
    )
  3. Upload an object.

    # Enter the full path of the object and the full path of the local file. Do not include the bucket name in the full path.
    # If the path of the local file is not specified, the 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.

    # Enter the full path of the object. Do not include the bucket name in the full path, 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 retrieving user ID from users table
    # Use V4 signature and standard credential provider
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"  # Corrected protocol header
    bucket = oss2.Bucket(auth, endpoint, 'oss-wwjtest', region='cn-hangzhou')

    # Execute database query
    user_id = mysql_client.fetch_one(sql)  # Ensure fetch_one returns a single value
    object_path = f"{user_id}.png"  # Need to properly define object name
    local_path = f"your_path/{object_path}"  # Need to replace with actual path

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

if __name__ == '__main__':
    conn_client()

  • On this page (1)
  • Sample code
Feedback