The OSS is a cloud storage service provided by Alibaba Cloud, featuring massive capacity, security, low cost, and high reliability. The RDS can work with the OSS to form multiple types of data storage solutions.

For example, when the business application is a forum and the RDS works with the OSS, resources such as registered users' images and post content images can be stored in the OSS to reduce the storage pressure of the RDS.

Sample code

The OSS works with the RDS example.

  1. Initialize OssAPI.
     from oss.oss_api import *
     endpoint=”oss-cn-hangzhou.aliyuncs.com”  
     accessKeyId, accessKeySecret=”your id”,”your secret”
     oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
  2. Create a bucket.
     #Set the bucket to private-read-write
     res = oss.create_bucket(bucket,"private")
     print "%s\n%s" % (res.status, res.read())
  3. Upload  an object.
     res = oss.put_object_from_file(bucket, object, "test.txt")
     print "%s\n%s" % (res.status, res.getheaders())
  4. Obtain the corresponding Object.
     res = oss.get_object_to_file(bucket, object, "/filepath/test.txt")
     print "%s\n%s" % (res.status, res.getheaders())

In the ECS application code, RDS stores the ID of each user, and OSS stores the avatar resource of the user. The Python code is as follows:


/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 user_id in RDS
#Obtain and download the user avatar to the corresponding path
oss.get_object_to_file(bucket, object, your_path/user_id+’.png’)
#Process the uploaded user avatar
oss.put_object_from_file(bucket, object, your_path/user_id+’.png’)