Multi-structure data storage

Last Updated: Dec 07, 2017

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

Example on the combined use of OSS and RDS.

  1. Initialize OssAPI.

    1. from oss.oss_api import *
    2. endpoint=”
    3. accessKeyId, accessKeySecret=”your id”,”your secret
    4. oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
  2. Create a bucket.

    1. #Set the bucket to private-read-write
    2. res = oss.create_bucket(bucket,"private")
    3. print "%s\n%s" % (res.status,
  3. Upload an object.

    1. res = oss.put_object_from_file(bucket, object, "test.txt")
    2. print "%s\n%s" % (res.status, res.getheaders())
  4. Obtain the corresponding object.

    1. res = oss.get_object_to_file(bucket, object, "/filepath/test.txt")
    2. 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:

  1. ```
  2. #!/usr/bin/env python
  3. from oss.oss_api import *
  4. endpoint=””
  5. accessKeyId, accessKeySecret=”your id”,”your secret”
  6. oss = OssAPI(endpoint, accessKeyId, accessKeySecret)
  7. user_id = mysql_client.fetch_one(sql)#Search for user_id in RDS
  8. #Obtain and download the user avatar to the corresponding path
  9. oss.get_object_to_file(bucket, object, your_path/user_id+’.png’)
  10. #Process the uploaded user avatar
  11. oss.put_object_from_file(bucket, object, your_path/user_id+’.png’)
  12. ```
Thank you! We've received your feedback.