简体   繁体   中英

How to get response and access the AWS Api with python?

I need to get response from sending request to the AWS. I have the secretkey/AccessKey of AWS. What is the method/syntax to access the aws APi, for example GetCredentialReport is an API of AWS, How to access this api?

Check boto3 , which is the aws SDK for Python.

To install it, run in your terminal:

pip install boto3

In order to get the credentials report, try:

import boto3
client = boto3.client(service_name='iam', aws_access_key_id="your_access_key",
                              aws_secret_access_key="your_secret_key")
print(client.get_credential_report())

If you haven't created the report before, generate it first:

client.generate_credential_report()

You can use get_credential_report() API in boto3 for credential repot

get_credential_report() Retrieves a credential report for the AWS account. For more information about the credential report, see Getting Credential Reports in the IAM User Guide.

See also: AWS API Documentation

Request Syntax

response = client.get_credential_report()

Response Structure

(dict) -- Contains the response to a successful GetCredentialReport request.

Content (bytes) -- Contains the credential report. The report is Base64-encoded.

ReportFormat (string) -- The format (MIME type) of the credential report.

GeneratedTime (datetime) -- The date and time when the credential report was created, in ISO 8601 date-time format.

Try this code

import os
import boto3
from dotenv import load_dotenv

load_dotenv()


AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')

client = boto3.client( 'iam', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY )

response = client.generate_credential_report()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM