本文整理汇总了Python中botocore.session.Session方法的典型用法代码示例。如果您正苦于以下问题:Python session.Session方法的具体用法?Python session.Session怎么用?Python session.Session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类botocore.session
的用法示例。
在下文中一共展示了session.Session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def __init__(self, session, aws_region, domain, task_list):
if not isinstance(session, Session):
raise TypeError("session must be an instance "
"of botocore.session.Session")
self._identity = None
self._session = session
self._aws_region = aws_region
self._domain = domain
self._task_list = task_list
# set user agent to botoflow
# import here to avoid import cycles
from .. import __version__
session.user_agent_name = 'botoflow'
session.user_agent_version = __version__
self._fix_endpoint()
示例2: main
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def main(self):
parser = self._create_parser()
self._parsed_args = parser.parse_args(self.args)
if self._parsed_args.profile:
self._session = Session(profile=self._parsed_args.profile)
else:
self._session = get_session()
if self._parsed_args.debug:
self._debug = True
if self._parsed_args.no_headless:
self._headless = False
if self._parsed_args.role:
self._role = self._parsed_args.role
if self._parsed_args.account:
self._account = self._parsed_args.account
return self.__getattribute__('_{}'.format(self._parsed_args.command))()
示例3: get_aws_credentials
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def get_aws_credentials(
service: str = DEFAULT_SPARK_SERVICE,
no_aws_credentials: bool = False,
aws_credentials_yaml: Optional[str] = None,
profile_name: Optional[str] = None,
) -> Tuple[Optional[str], Optional[str]]:
if no_aws_credentials:
return None, None
elif aws_credentials_yaml:
return _load_aws_credentials_from_yaml(aws_credentials_yaml)
elif service != DEFAULT_SPARK_SERVICE:
service_credentials_path = os.path.join(AWS_CREDENTIALS_DIR, f"{service}.yaml")
if os.path.exists(service_credentials_path):
return _load_aws_credentials_from_yaml(service_credentials_path)
else:
print(
PaastaColors.yellow(
"Did not find service AWS credentials at %s. Falling back to "
"user credentials." % (service_credentials_path)
)
)
creds = Session(profile=profile_name).get_credentials()
return creds.access_key, creds.secret_key
示例4: __init__
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def __init__(self, name, access_key=None, secret_key=None,
security_token=None, profile_name=None, **kwargs):
"""
Create a new BotoCredentialAdapter.
"""
# TODO: We take kwargs because new boto2 versions have an 'anon'
# argument and we want to be future proof
if (name == 'aws' or name is None) and access_key is None and not kwargs.get('anon', False):
# We are on AWS and we don't have credentials passed along and we aren't anonymous.
# We will backend into a boto3 resolver for getting credentials.
# Make sure to enable boto3's own caching, so we can share that
# cash with pure boto3 code elsewhere in Toil.
self._boto3_resolver = create_credential_resolver(Session(profile=profile_name), cache=JSONFileCache())
else:
# We will use the normal flow
self._boto3_resolver = None
# Pass along all the arguments
super(BotoCredentialAdapter, self).__init__(name, access_key=access_key,
secret_key=secret_key, security_token=security_token,
profile_name=profile_name, **kwargs)
示例5: _get_v4_signed_headers
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def _get_v4_signed_headers(self):
"""Returns V4 signed get-caller-identity request headers"""
if self.aws_session is None:
boto_session = session.Session()
creds = boto_session.get_credentials()
else:
creds = self.aws_session.get_credentials()
if creds is None:
raise CerberusClientException("Unable to locate AWS credentials")
readonly_credentials = creds.get_frozen_credentials()
# hardcode get-caller-identity request
data = OrderedDict((('Action', 'GetCallerIdentity'), ('Version', '2011-06-15')))
url = 'https://sts.{}.amazonaws.com/'.format(self.region)
request_object = awsrequest.AWSRequest(method='POST', url=url, data=data)
signer = auth.SigV4Auth(readonly_credentials, 'sts', self.region)
signer.add_auth(request_object)
return request_object.headers
示例6: build_region_list
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def build_region_list(service, chosen_regions = [], partition_name = 'aws'):
"""
Build the list of target region names
:param service:
:param chosen_regions:
:param partition_name:
:return:
"""
service = 'ec2containerservice' if service == 'ecs' else service # Of course things aren't that easy...
# Get list of regions from botocore
regions = Session().get_available_regions(service, partition_name = partition_name)
if len(chosen_regions):
return list((Counter(regions) & Counter(chosen_regions)).elements())
else:
return regions
示例7: setup_aws_client
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def setup_aws_client(config):
role_arn = "arn:aws:iam::{}:role/{}".format(config['account_id'].replace('-', ''),
config['role_name'])
session = Session()
fetcher = AssumeRoleCredentialFetcher(
session.create_client,
session.get_credentials(),
role_arn,
extra_args={
'DurationSeconds': 3600,
'RoleSessionName': 'TapS3CSV',
'ExternalId': config['external_id']
},
cache=JSONFileCache()
)
refreshable_session = Session()
refreshable_session.register_component(
'credential_provider',
CredentialResolver([AssumeRoleProvider(fetcher)])
)
LOGGER.info("Attempting to assume_role on RoleArn: %s", role_arn)
boto3.setup_default_session(botocore_session=refreshable_session)
示例8: create_botocore_session
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def create_botocore_session(profile=None, debug=False,
connection_timeout=None,
read_timeout=None,
max_retries=None):
# type: (OptStr, bool, OptInt, OptInt, OptInt) -> Session
s = Session(profile=profile)
_add_chalice_user_agent(s)
if debug:
_inject_large_request_body_filter()
config_args = {} # type: Dict[str, Any]
if connection_timeout is not None:
config_args['connect_timeout'] = connection_timeout
if read_timeout is not None:
config_args['read_timeout'] = read_timeout
if max_retries is not None:
config_args['retries'] = {'max_attempts': max_retries}
if config_args:
config = BotocoreConfig(**config_args)
s.set_default_client_config(config)
return s
示例9: getEnvironment
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def getEnvironment(self, profile=None):
"""Return environment variables that should be set for the profile."""
eventHooks = HierarchicalEmitter()
session = Session(event_hooks=eventHooks)
if profile:
session.set_config_variable('profile', profile)
eventHooks.register('session-initialized',
inject_assume_role_provider_cache,
unique_id='inject_assume_role_cred_provider_cache')
session.emit('session-initialized', session=session)
creds = session.get_credentials()
env = {}
def set(key, value):
if value:
env[key] = value
set('AWS_ACCESS_KEY_ID', creds.access_key)
set('AWS_SECRET_ACCESS_KEY', creds.secret_key)
# AWS_SESSION_TOKEN is the ostensibly the standard:
# http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environment
set('AWS_SESSION_TOKEN', creds.token)
# ...but boto expects AWS_SECURITY_TOKEN. Set both for compatibility.
# https://github.com/boto/boto/blob/b016c07d834df5bce75141c4b9d2f3d30352e1b8/boto/connection.py#L438
set('AWS_SECURITY_TOKEN', creds.token)
set('AWS_DEFAULT_REGION', session.get_config_variable('region'))
return env
示例10: connect_service
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def connect_service(service, credentials, region_name = None, config = None, silent = False):
"""
Instantiates an AWS API client
:param service:
:param credentials:
:param region_name:
:param config:
:param silent:
:return:
"""
api_client = None
try:
client_params = {}
client_params['service_name'] = service.lower()
session_params = {}
session_params['aws_access_key_id'] = credentials['AccessKeyId']
session_params['aws_secret_access_key'] = credentials['SecretAccessKey']
session_params['aws_session_token'] = credentials['SessionToken']
if region_name:
client_params['region_name'] = region_name
session_params['region_name'] = region_name
if config:
client_params['config'] = config
aws_session = boto3.session.Session(**session_params)
if not silent:
infoMessage = 'Connecting to AWS %s' % service
if region_name:
infoMessage = infoMessage + ' in %s' % region_name
printInfo('%s...' % infoMessage)
api_client = aws_session.client(**client_params)
except Exception as e:
printException(e)
return api_client
示例11: _add_chalice_user_agent
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def _add_chalice_user_agent(session):
# type: (Session) -> None
suffix = '%s/%s' % (session.user_agent_name, session.user_agent_version)
session.user_agent_name = 'aws-chalice'
session.user_agent_version = chalice_version
session.user_agent_extra = suffix
示例12: create_default_deployer
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def create_default_deployer(self, session, config, ui):
# type: (Session, Config, UI) -> deployer.Deployer
return deployer.create_default_deployer(session, config, ui)
示例13: create_plan_only_deployer
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def create_plan_only_deployer(self, session, config, ui):
# type: (Session, Config, UI) -> deployer.Deployer
return deployer.create_plan_only_deployer(session, config, ui)
示例14: create_log_retriever
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def create_log_retriever(self, session, lambda_arn, follow_logs):
# type: (Session, str, bool) -> LogRetriever
client = TypedAWSClient(session)
if follow_logs:
event_generator = cast(BaseLogEventGenerator,
FollowLogEventGenerator(client))
else:
event_generator = cast(BaseLogEventGenerator,
LogEventGenerator(client))
retriever = LogRetriever.create_from_lambda_arn(event_generator,
lambda_arn)
return retriever
示例15: register_parser
# 需要导入模块: from botocore import session [as 别名]
# 或者: from botocore.session import Session [as 别名]
def register_parser(function, parent=None, name=None, **add_parser_args):
def get_aws_profiles(**kwargs):
from botocore.session import Session
return list(Session().full_config["profiles"])
def set_aws_profile(profile_name):
os.environ["AWS_PROFILE"] = profile_name
del os.environ["AWS_DEFAULT_PROFILE"]
def get_region_names(**kwargs):
from botocore.loaders import create_loader
for partition_data in create_loader().load_data("endpoints")["partitions"]:
if partition_data["partition"] == config.partition:
return partition_data["regions"].keys()
def set_aws_region(region_name):
os.environ["AWS_DEFAULT_REGION"] = region_name
def set_endpoint_url(endpoint_url):
from .util.aws._boto3_loader import Loader
Loader.client_kwargs["default"].update(endpoint_url=endpoint_url)
def set_client_kwargs(client_kwargs):
from .util.aws._boto3_loader import Loader
Loader.client_kwargs.update(json.loads(client_kwargs))
if config is None:
initialize()
if parent is None:
parent = parser
parser_name = name or function.__name__
if parent.prog not in _subparsers:
_subparsers[parent.prog] = parent.add_subparsers()
if "description" not in add_parser_args:
func_module = sys.modules[function.__module__]
add_parser_args["description"] = add_parser_args.get("help", function.__doc__ or func_module.__doc__)
if add_parser_args["description"] and "help" not in add_parser_args:
add_parser_args["help"] = add_parser_args["description"].strip().splitlines()[0].rstrip(".")
add_parser_args.setdefault("formatter_class", AegeaHelpFormatter)
subparser = _subparsers[parent.prog].add_parser(parser_name.replace("_", "-"), **add_parser_args)
if "_" in parser_name and USING_PYTHON2:
_subparsers[parent.prog]._name_parser_map[parser_name] = subparser
subparser.add_argument("--max-col-width", "-w", type=int, default=32,
help="When printing tables, truncate column contents to this width. Set to 0 for auto fit.")
subparser.add_argument("--json", action="store_true",
help="Output tabular data as a JSON-formatted list of objects")
subparser.add_argument("--log-level", default=config.get("log_level"),
help=str([logging.getLevelName(i) for i in range(10, 60, 10)]),
choices={logging.getLevelName(i) for i in range(10, 60, 10)})
subparser.add_argument("--profile", help="Profile to use from the AWS CLI credential file",
type=set_aws_profile).completer = get_aws_profiles
subparser.add_argument("--region", help="Region to use (overrides environment variable)",
type=set_aws_region).completer = get_region_names
subparser.add_argument("--endpoint-url", metavar="URL", help="Service endpoint URL to use", type=set_endpoint_url)
subparser.add_argument("--client-kwargs", help=argparse.SUPPRESS, type=set_client_kwargs)
subparser.set_defaults(entry_point=function)
if parent and sys.version_info < (2, 7, 9): # See https://bugs.python.org/issue9351
parent._defaults.pop("entry_point", None)
subparser.set_defaults(**_get_config_for_prog(subparser.prog))
return subparser