函數Context參數中提供了訪問云資源的臨時密鑰,包含AccessKey ID、AccessKey Secret及Security Token,如果遺漏了Security Token,會收到The Access Key ID does not exist的報錯。
以下是在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'
文檔內容是否對您有幫助?