函数Context中提供了访问云资源的临时密钥,包含AccessKey ID、AccessKey Secret、Security Token,请不要遗漏了Security Token。以下示例是在Python函数中访问OSS代码。

import json
import oss2
def my_handler(event, context):
    evt = json.loads(event)
    creds = context.credentials
    # 身份验证时,请不要遗漏了security_token!
    # Do not miss the "security_token" for the authentication!
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
    bucket = oss2.Bucket(auth, evt['endpoint'], evt['bucket'])
    bucket.put_object(evt['objectName'], evt['message'])
    return 'success'