当前位置: 首页>>代码示例>>Python>>正文


Python boto3.Session方法代码示例

本文整理汇总了Python中boto3.Session方法的典型用法代码示例。如果您正苦于以下问题:Python boto3.Session方法的具体用法?Python boto3.Session怎么用?Python boto3.Session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在boto3的用法示例。


在下文中一共展示了boto3.Session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: describe_bucket_worker

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def describe_bucket_worker(bucket):
    bucket.LocationConstraint = clients.s3.get_bucket_location(Bucket=bucket.name)["LocationConstraint"]
    cloudwatch = resources.cloudwatch
    bucket_region = bucket.LocationConstraint or "us-east-1"
    if bucket_region != cloudwatch.meta.client.meta.region_name:
        cloudwatch = boto3.Session(region_name=bucket_region).resource("cloudwatch")
    data = get_cloudwatch_metric_stats("AWS/S3", "NumberOfObjects",
                                       start_time=datetime.utcnow() - timedelta(days=2),
                                       end_time=datetime.utcnow(), period=3600, BucketName=bucket.name,
                                       StorageType="AllStorageTypes", resource=cloudwatch)
    bucket.NumberOfObjects = int(data["Datapoints"][-1]["Average"]) if data["Datapoints"] else None
    data = get_cloudwatch_metric_stats("AWS/S3", "BucketSizeBytes",
                                       start_time=datetime.utcnow() - timedelta(days=2),
                                       end_time=datetime.utcnow(), period=3600, BucketName=bucket.name,
                                       StorageType="StandardStorage", resource=cloudwatch)
    bucket.BucketSizeBytes = format_number(data["Datapoints"][-1]["Average"]) if data["Datapoints"] else None
    return bucket 
开发者ID:kislyuk,项目名称:aegea,代码行数:19,代码来源:s3.py

示例2: audit_2_3

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def audit_2_3(self):
        """2.3 Ensure the S3 bucket CloudTrail logs to is not publicly accessible (Scored)"""
        raise NotImplementedError()
        import boto3
        s3 = boto3.session.Session(region_name="us-east-1").resource("s3")
        # s3 = boto3.resource("s3")
        # for trail in self.trails:
        #    for grant in s3.Bucket(trail["S3BucketName"]).Acl().grants:
        #    print(s3.Bucket(trail["S3BucketName"]).Policy().policy)
        for bucket in s3.buckets.all():
            print(bucket)
            try:
                print("    Policy:", bucket.Policy().policy)
            except Exception:
                pass
            for grant in bucket.Acl().grants:
                try:
                    print("    Grant:", grant)
                except Exception:
                    pass 
开发者ID:kislyuk,项目名称:aegea,代码行数:22,代码来源:audit.py

示例3: get_iam_username

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_iam_username(cls):
        if cls._default_iam_username is None:
            try:
                user = resources.iam.CurrentUser().user
                cls._default_iam_username = getattr(user, "name", ARN(user.arn).resource.split("/")[-1])
            except Exception as e:
                try:
                    if "Must specify userName" in str(e) or ("assumed-role" in str(e) and "botocore-session" in str(e)):
                        cur_session = boto3.Session()._session
                        src_profile = cur_session.full_config["profiles"][cur_session.profile]["source_profile"]
                        src_session = boto3.Session(profile_name=src_profile)
                        cls._default_iam_username = src_session.resource("iam").CurrentUser().user.name
                    else:
                        caller_arn = ARN(clients.sts.get_caller_identity()["Arn"])
                        cls._default_iam_username = caller_arn.resource.split("/")[-1]
                except Exception:
                    cls._default_iam_username = "unknown"
        return cls._default_iam_username 
开发者ID:kislyuk,项目名称:aegea,代码行数:20,代码来源:__init__.py

示例4: get_session

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_session(role_arn=None, sts_client=None, logger=None):
    if role_arn not in [None, ""]:
        sts = sts_client if sts_client is not None else boto3.client("sts")
        account = account_from_role_arn(role_arn)
        try:
            token = sts.assume_role(RoleArn=role_arn, RoleSessionName="{}-{}".format(account, str(uuid.uuid4())))
        except botocore.exceptions.ClientError as ex:
            if logger is not None:
                logger.error(ERR_ASSUME_ROLE_FOR_ARN, role_arn, ex)
            raise ex
        credentials = token["Credentials"]
        return boto3.Session(aws_access_key_id=credentials["AccessKeyId"],
                             aws_secret_access_key=credentials["SecretAccessKey"],
                             aws_session_token=credentials["SessionToken"])
    else:
        role = os.getenv(ENV_ROLE_ARN)
        if role is not None:
            return get_session(role, sts_client)
        return boto3.Session() 
开发者ID:awslabs,项目名称:aws-ops-automator,代码行数:21,代码来源:__init__.py

示例5: _get_watchtower_handler

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def _get_watchtower_handler():
    session = boto3.Session(
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
        region_name=AWS_REGION,
    )

    handler = watchtower.CloudWatchLogHandler(
        log_group=CLOUDWATCH_LOG_GROUP,
        stream_name=CLOUDWATCH_LOG_STREAM,
        send_interval=5,  # every 5 sec
        boto3_session=session,
    )

    handler.setFormatter(_log_formatter)

    return handler 
开发者ID:simple-login,项目名称:app,代码行数:19,代码来源:log.py

示例6: get_attached_local_policies

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_attached_local_policies(self):
        client = boto3.Session(
            profile_name=self.args.profile,
            aws_access_key_id=self.args.keys[0],
            aws_secret_access_key=self.args.keys[1],
            region_name=self.args.region
        ).client('iam')
        pages = paginate(
            client,
            'list_policies',
            Scope='Local',
            OnlyAttached=True
        )
        for page in pages:
            for policy in page['Policies']:
                version = client.get_policy_version(
                    PolicyArn=policy['Arn'],
                    VersionId=policy['DefaultVersionId']
                )['PolicyVersion']
                yield policy['Arn'], version 
开发者ID:Skyscanner,项目名称:LambdaGuard,代码行数:22,代码来源:LambdaWrite.py

示例7: get_regions

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_regions(args):
    '''
    Valid region specification:
        Single:     eu-west-1
        Multiple:   eu-west-1,ap-south-1,us-east-2
        All:        all
    Returns regions as a Python list
    '''
    if not isinstance(args.region, str):
        raise ValueError(f'No region specified')
    if args.function:
        return [arnparse(args.function).region]
    available = boto3.Session().get_available_regions('lambda')
    if args.region == 'all':
        return available
    regions = args.region.split(',')
    if not regions:
        raise ValueError(f'No region specified')
    for region in regions:
        if region not in available:
            raise ValueError(f'Invalid region "{region}"')
    return regions 
开发者ID:Skyscanner,项目名称:LambdaGuard,代码行数:24,代码来源:__init__.py

示例8: __init__

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def __init__(self, arn, profile=None, access_key_id=None, secret_access_key=None):
        # AWS ARN
        self.arn = arnparse(arn)

        # AWS Profile and Keys
        self.profile = profile
        self.access_key_id = access_key_id
        self.secret_access_key = secret_access_key

        # AWS Resource-based policy
        self.policy = {}

        # Additional service information
        self.info = ''

        # AWS connection
        session = boto3.Session(profile_name=self.profile)
        self.client = session.client(
            self.arn.service,
            region_name=self.arn.region,
            aws_access_key_id=access_key_id,
            aws_secret_access_key=secret_access_key
        ) 
开发者ID:Skyscanner,项目名称:LambdaGuard,代码行数:25,代码来源:AWS.py

示例9: handle

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def handle(self, *args, **options):
        print('Hello World Import Files')
        start_time = time.time()
        print('Download Files')
        command = 'mkdir -p {}/data/files/'.format(settings.BASE_DIR)
        run(command, shell=True)
        file_list = open('%s/data/files/all_files.txt' % (settings.BASE_DIR), 'w')
        s3credentials = S3Credential.objects.all()
        for s3credential in s3credentials:
            print(s3credential.name)
            for bucket_name in s3credential.buckets.splitlines():
                session = boto3.Session(
                    aws_access_key_id=s3credential.access_key,
                    aws_secret_access_key=s3credential.secret_key
                )
                s3 = session.resource('s3')
                bucket = s3.Bucket(bucket_name)
                print(bucket)
                for key in bucket.objects.all():
                    if key.size != 0:
                        file = [str(key.last_modified), str(key.size), bucket.name, key.key]
                        file_list.writelines('%s\n' % ('\t'.join(file)))
        self.stdout.write(self.style.SUCCESS('Successfully downloaded files!'))
        elapsed_time = time.time() - start_time
        print('Importing Files Took {}'.format(elapsed_time)) 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:27,代码来源:download_files.py

示例10: download_files

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def download_files(self):
        print('Download Files')
        file_list = open('%s/data/files/all_files.txt' % (settings.BASE_DIR), 'w')
        s3credentials = S3Credential.objects.all()
        for s3credential in s3credentials:
            print(s3credential.name)
            for bucket_name in s3credential.buckets.splitlines():
                session = boto3.Session(
                    aws_access_key_id=s3credential.access_key,
                    aws_secret_access_key=s3credential.secret_key
                )
                s3 = session.resource('s3')
                bucket = s3.Bucket(bucket_name)
                print(bucket)
                for key in bucket.objects.all():
                    if key.size != 0:
                        file = [str(key.last_modified), str(key.size), bucket.name, key.key]
                        file_list.writelines('%s\n' % ('\t'.join(file)))
        self.stdout.write(self.style.SUCCESS('Successfully downloaded files!')) 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:21,代码来源:update_files.py

示例11: _get_boto3_session

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def _get_boto3_session(self):
        import boto3

        if self.use_opinel:
            from opinel.utils.credentials import read_creds

            # Refresh access token, and attach credentials to current object for debugging
            self._credentials = read_creds(self.aws_profile)

            return boto3.Session(
                aws_access_key_id=self._credentials['AccessKeyId'],
                aws_secret_access_key=self._credentials['SecretAccessKey'],
                aws_session_token=self._credentials['SessionToken'],
                profile_name=self.aws_profile,
            )

        return boto3.Session(profile_name=self.aws_profile) 
开发者ID:airbnb,项目名称:omniduct,代码行数:19,代码来源:s3.py

示例12: aws_client

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def aws_client(region, service='ec2', profile=None):
    """ Set the boto3 client with the correct service and AWS profile.

    Args:
        region (str): The AWS region you want this client to connect to.
            example us-west-2
    Kwargs:
        service (str): The service this client will connect to.
        profile (str): The aws profile name that is set in ~/.aws/credentials

    Basic Usage:
        >>> client = aws_client('us-west-2', 'kinesis', profile='prod')

    Returns:
        botocore.client.EC2
    """
    try:
        session = boto3.Session(region_name=region, profile_name=profile)
        return session.client(service)
    except botocore.exceptions.ClientError as e:
        raise e 
开发者ID:PacktPublishing,项目名称:-Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools,代码行数:23,代码来源:aws.py

示例13: get_session

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_session(aws_access_key_id=None, aws_secret_access_key=None,
                aws_session_token=None, profile_name=None):
    if aws_access_key_id is not None:
        if aws_access_key_id not in get_session._cached_sessions:
            get_session._cached_sessions[aws_access_key_id] = boto3.Session(
                aws_access_key_id=aws_access_key_id,
                aws_secret_access_key=aws_secret_access_key,
                aws_session_token=aws_session_token,
                profile_name=profile_name
            )
        get_session._last_session = get_session._cached_sessions[aws_access_key_id]
        return get_session._cached_sessions[aws_access_key_id]
    else:
        if get_session._last_session is None:
            get_session._last_session = boto3.Session(profile_name=profile_name)
        return get_session._last_session 
开发者ID:fugue,项目名称:credstash,代码行数:18,代码来源:credstash.py

示例14: find_elb_dns_zone_id

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def find_elb_dns_zone_id(name='', env='dev', region='us-east-1'):
    """Get an application's AWS elb dns zone id.

    Args:
        name (str): ELB name
        env (str): Environment/account of ELB
        region (str): AWS Region

    Returns:
        str: elb DNS zone ID

    """
    LOG.info('Find %s ELB DNS Zone ID in %s [%s].', name, env, region)
    client = boto3.Session(profile_name=env).client('elb', region_name=region)
    elbs = client.describe_load_balancers(LoadBalancerNames=[name])
    return elbs['LoadBalancerDescriptions'][0]['CanonicalHostedZoneNameID'] 
开发者ID:foremast,项目名称:foremast,代码行数:18,代码来源:elb.py

示例15: get_sns_subscriptions

# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import Session [as 别名]
def get_sns_subscriptions(app_name, env, region):
    """List SNS lambda subscriptions.

    Returns:
        list: List of Lambda subscribed SNS ARNs.

    """
    session = boto3.Session(profile_name=env, region_name=region)
    sns_client = session.client('sns')

    lambda_alias_arn = get_lambda_alias_arn(app=app_name, account=env, region=region)

    lambda_subscriptions = []
    subscriptions = sns_client.list_subscriptions()

    for subscription in subscriptions['Subscriptions']:
        if subscription['Protocol'] == "lambda" and subscription['Endpoint'] == lambda_alias_arn:
            lambda_subscriptions.append(subscription['SubscriptionArn'])

    if not lambda_subscriptions:
        LOG.debug('SNS subscription for function %s not found', lambda_alias_arn)

    return lambda_subscriptions 
开发者ID:foremast,项目名称:foremast,代码行数:25,代码来源:get_sns_subscriptions.py


注:本文中的boto3.Session方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。