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.
Configure environment variables.
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.
Initialize oss2.Bucket.
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
bucket = oss2.Bucket(
auth,
endpoint,
'examplebucket',
region='cn-hangzhou'
)
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))
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:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import mysql_client
def conn_client():
sql = "SELECT id FROM users LIMIT 1"
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
bucket = oss2.Bucket(auth, endpoint, 'oss-wwjtest', region='cn-hangzhou')
user_id = mysql_client.fetch_one(sql)
object_path = f"{user_id}.png"
local_path = f"your_path/{object_path}"
bucket.get_object_to_file(object_path, local_path)
bucket.put_object_from_file(object_path, local_path)
if __name__ == '__main__':
conn_client()