Esempio n. 1
0
    def __init__(self,
                 nconcurrent=24,
                 region_name=None,
                 addressing_style='path'):
        from aiobotocore.config import AioConfig

        if region_name is None:
            region_name = auto_find_region()

        s3_cfg = AioConfig(max_pool_connections=nconcurrent,
                           s3=dict(addressing_style=addressing_style))

        self._nconcurrent = nconcurrent
        self._async = AsyncThread()
        self._s3 = None
        self._session = None
        self._closed = False

        async def setup(s3_cfg):
            session = aiobotocore.get_session()
            s3 = session.create_client('s3',
                                       region_name=region_name,
                                       config=s3_cfg)
            return (session, s3)

        session, s3 = self._async.submit(setup, s3_cfg).result()
        self._session = session
        self._s3 = s3
Esempio n. 2
0
    def __init__(
        self,
        nconcurrent=24,
        region_name=None,
        addressing_style="path",
        aws_unsigned=None,
    ):

        self._closed = True
        if region_name is None:
            region_name = auto_find_region()

        opts = {}
        if aws_unsigned is None:
            aws_unsigned = _aws_unsigned_check_env()

        if aws_unsigned:
            opts["signature_version"] = botocore.UNSIGNED

        s3_cfg = AioConfig(
            max_pool_connections=nconcurrent,
            **opts,
            s3=dict(addressing_style=addressing_style),
        )

        self._nconcurrent = nconcurrent
        self._async = AsyncThread()
        self._s3 = None
        self._s3_ctx = None
        self._session = None

        async def setup(s3_cfg):
            session = get_session()
            s3_ctx = session.create_client("s3",
                                           region_name=region_name,
                                           config=s3_cfg)
            s3 = await s3_ctx.__aenter__()
            return (session, s3, s3_ctx)

        session, s3, s3_ctx = self._async.submit(setup, s3_cfg).result()
        self._closed = False
        self._session = session
        self._s3 = s3
        self._s3_ctx = s3_ctx
Esempio n. 3
0
def activate_rio_env(aws=None, defaults=True, **kwargs):
    """Inject activated rasterio.Env into current thread.

    This de-activates previously setup environment.

    :param aws: Dictionary of options for rasterio.session.AWSSession
                OR False in which case session won't be setup
                OR None -- session = rasterio.session.AWSSession()

    :param defaults: Supply False to not inject COG defaults
    :param **kwargs: Passed on to rasterio.Env(..) constructor
    """
    env_old = getattr(_local, "env", None)

    if env_old is not None:
        env_old.__exit__(None, None, None)
        _local.env = None

    if aws is False:
        session = None
    else:
        aws = {} if aws is None else dict(**aws)
        region_name = aws.get("region_name", "auto")

        if region_name == "auto":
            from odc.aws import auto_find_region

            try:
                aws["region_name"] = auto_find_region()
            except Exception as e:
                # only treat it as error if it was requested by user
                if "region_name" in aws:
                    raise e

        session = AWSSession(**aws)

    opts = dict(GDAL_DISABLE_READDIR_ON_OPEN="EMPTY_DIR") if defaults else {}

    opts.update(**kwargs)

    env = rasterio.Env(session=session, **opts)
    env.__enter__()
    _local.env = env
    return get_rio_env()