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

This section provides an example on how to use ApsaraDB RDS together with OSS.

  1. Initialize the OSS API.
     from oss.oss_api import *
     endpoint="oss-cn-hangzhou.aliyuncs.com"  
     accessKeyId, accessKeySecret="your id","your secret"
     oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
  2. Create an OSS bucket.
     # Set the access control list (ACL) of the bucket to private.
     res = oss.create_bucket(bucket,"private")
     print "%s\n%s" % (res.status, res.read())
  3. Upload an object to the OSS bucket.
     res = oss.put_object_from_file(bucket, object, "test.txt")
     print "%s\n%s" % (res.status, res.getheaders())
  4. Retrieve the object.
     res = oss.get_object_to_file(bucket, object, "/filepath/test.txt")
     print "%s\n%s" % (res.status, res.getheaders())

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
from oss.oss_api import *
endpoint="oss-cn-hangzhou.aliyuncs.com"  
accessKeyId, accessKeySecret="your id","your secret"
oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
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.
oss.get_object_to_file(bucket, object, your_path/user_id+'.png')
#Process the uploaded profile picture of the user.
oss.put_object_from_file(bucket, object, your_path/user_id+'.png')