How to get Access keys from Roles Assigned to Function Compute in Alibaba Cloud?

108 Views Asked by At

I am trying to access the keys from fc.context and not sure where I can get keys from when I assign a role to the function Compute. Instead of hardcoding or putting them in ENV I want to pute inthe bucket by applying roles to function compute. Ive applied s3 all access role to fc but not sure how to put after that?

-- coding: utf-8 --

# License: MIT
#

import logging
import oss2
import requests, io
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import urllib, base64
from dotenv import load_dotenv, find_dotenv
import os
from pathlib import Path  # Python 3.6+ only
env_path = Path('.') / '.env'
load_dotenv(find_dotenv())
# load_dotenv()

 
def handler(environ, start_response):
 # Enable logging
    logger = logging.getLogger()
    context = environ['fc.context']
    request_uri = environ['fc.request_uri']
    fig = plt.figure(figsize=(9, 6))
    x = np.linspace(0., 5., 100)
    y = np.sin(x)

    rows = 3
    columns = 2

    x = np.linspace(0., 5., 100)
    y = np.sin(x)

    grid = plt.GridSpec(rows, columns, wspace = .25, hspace = .25)

    plt.subplot(grid[:, 0])
    plt.annotate('sub1', xy = (0.5, -0.5), va = 'center', ha = 'center',  weight='bold', fontsize = 15)
    plt.plot(x, y)

    plt.subplot(grid[0, 1])
    plt.annotate('sub2', xy = (0.5, -0.5), va = 'center', ha = 'center',  weight='bold', fontsize = 15)
    plt.plot(x, y)

    plt.subplot(grid[1, 1])
    plt.annotate('sub3', xy = (0.5, -0.5), va = 'center', ha = 'center',  weight='bold', fontsize = 15)
    plt.plot(x, y)

    plt.subplot(grid[2, 1])
    plt.annotate('sub4', xy = (0.5, -0.5), va = 'center', ha = 'center',  weight='bold', fontsize = 15)
    fig = plt.gcf()

    plt.plot(x, y)
    buf = io.BytesIO()
    fig.savefig(buf, format='png')

    buf.seek(0)
    string = base64.b64encode(buf.read())

    uri = 'data:image/png;base64,' + urllib.parse.quote(string)
    BUCKET_NAME = 'Sample123'
    auth = oss2.Auth(Access_key, SecretKey)
    bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', BUCKET_NAME)
    IMAGE_NAME = 'SampleImage.png'
    bucket.put_object(IMAGE_NAME,buf.getvalue())
    # Last, return the final result
 
    status = '200 OK'
    response_headers = [('Content-type', 'image/png'), ('Content-Disposition', 'inline; filename="meme.jpeg"')]
    start_response(status, response_headers)
    return [ buf.getvalue() ]
   
1

There are 1 best solutions below

3
On

You can get the credentials from the context and create an oss client as below:

def get_oss_client(context, endpoint, bucket):
  creds = context.credentials
  if creds.security_token != None:
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
  else:
    # for local testing, use the public endpoint
    endpoint = str.replace(endpoint, "-internal", "")
    auth = oss2.Auth(creds.access_key_id, creds.access_key_secret)
  return oss2.Bucket(auth, endpoint, bucket)