在AWS Glue作业中进行API调用的方法如下所示:
import boto3
s3_client = boto3.client('s3') bucket_list = s3_client.list_buckets()
for bucket in bucket_list['Buckets']: print(bucket['Name'])
from awsglue.job import Job from awsglue.context import GlueContext
glueContext = GlueContext(SparkContext.getOrCreate()) s3_client = boto3.client('s3') bucket_list = s3_client.list_buckets()
for bucket in bucket_list['Buckets']: print(bucket['Name'])
import boto3 import json
def create_glue_role(role_name, policy_arn): iam_client = boto3.client('iam') iam_role = iam_client.create_role(RoleName=role_name, AssumeRolePolicyDocument=json.dumps({ 'Version': '2012-10-17', 'Statement': [{ 'Effect': 'Allow', 'Principal': {'Service': ['glue.amazonaws.com']}, 'Action': ['sts:AssumeRole'] }] })) iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn) return iam_role['Role']['Arn']
role_name = 'glue_exec_role' policy_arn = 'arn:aws:iam::aws:policy/service