python 从AWS CLI凭据设置AWS环境(密钥ID,密钥和令牌)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从AWS CLI凭据设置AWS环境(密钥ID,密钥和令牌)相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
import os
import sys
import pickle
from datetime import datetime, timedelta, tzinfo
import boto3
import botocore
credentials_cache_file = ".aws_credentials.cache"
class UTC(tzinfo):
"""UTC"""
def utcoffset(self, dt):
return timedelta(0)
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return timedelta(0)
def get_credentials(profile):
try:
with open(credentials_cache_file) as f:
cache = pickle.load(f)
except IOError:
# No or corrupt file.
cache = {}
if profile in cache:
creds, expiry = cache[profile]
if expiry > datetime.now(UTC()):
return creds
try:
session = boto3.Session(profile_name=profile)
except botocore.exceptions.ProfileNotFound:
print "Environment \"%s\" does not exist in configuration. Aborting..." % (profile,)
sys.exit(1)
cred = session.get_credentials()
cache[profile] = (cred.get_frozen_credentials(), cred._expiry_time)
with open(credentials_cache_file, "w") as f:
pickle.dump(cache, f)
return cred
def args():
# If this script is invoked with a different name, say through a
# symlink, use that name as the profile. Otherwise use first arg.
invoked_name = os.path.basename(sys.argv.pop(0))
if invoked_name != "awsudo.py":
profile = invoked_name
else:
profile = sys.argv.pop(0)
return profile, sys.argv
def run_program(credentials, argv):
env = os.environ.copy()
env.update(AWS_ACCESS_KEY_ID=credentials.access_key,
AWS_SECRET_ACCESS_KEY=credentials.secret_key,
AWS_SESSION_TOKEN=credentials.token)
os.execvpe(argv[0], argv, env)
if __name__ == "__main__":
if len(sys.argv) == 1:
print "Usage: %s <profile> <command> [<argument> ...]" % tuple(sys.argv)
sys.exit(1)
profile, argv = args()
print "Using environment \"%s\":" % (profile,)
creds = get_credentials(profile)
run_program(creds, argv)
以上是关于python 从AWS CLI凭据设置AWS环境(密钥ID,密钥和令牌)的主要内容,如果未能解决你的问题,请参考以下文章
我正在尝试使用 AWS EB CLI (elastic beanstalk) 部署 python 应用程序
IntelliJ - 无法从链中的任何提供程序加载AWS凭据
从 Go 中的 ec2 元数据服务获取 aws 凭据
AWS CLI $PATH 设置
从 AWS Parameter Store 设置 Jenkins 参数
AWS Beanstalk - 无法在没有来自 CLI 的负载均衡器的情况下在 VPC 中创建环境