Exemple #1
0
def _state(purge=False):
    """
    .env   None| rasterio.Env
    .epoch -1  | +Int
    """
    return thread_local_cache("__rio_state__",
                              SimpleNamespace(env=None, epoch=-1),
                              purge=purge)
Exemple #2
0
def s3_client(profile: Optional[str] = None,
              creds: Optional[ReadOnlyCredentials] = None,
              region_name: Optional[str] = None,
              session: Optional[Session] = None,
              use_ssl: bool = True,
              cache: Union[bool, str] = False,
              **cfg) -> botocore.client.BaseClient:
    """ Construct s3 client with configured region_name.

    :param profile    : profile name to lookup (only used if session is not supplied)
    :param creds      : Override credentials with supplied data
    :param region_name: region_name to use, overrides session setting
    :param session    : botocore session to use
    :param use_ssl    : Whether to connect via http or https
    :param cache      : True -- Store/lookup s3 client in thread local cache
                        "purge" -- delete from cache and return what was there to begin with

    **cfg: passed on to botocore.client.Config(..)
       max_pool_connections
       connect_timeout
       read_timeout
       parameter_validation
       ...
    """
    if not cache:
        return _mk_s3_client(profile,
                             creds=creds,
                             region_name=region_name,
                             session=session,
                             use_ssl=use_ssl,
                             **cfg)

    _cache = thread_local_cache("__aws_s3_cache", {})

    key = _s3_cache_key(profile=profile, region_name=region_name, creds=creds)

    if cache == "purge":
        return _cache.pop(key, None)

    s3 = _cache.get(key, None)

    if s3 is None:
        s3 = _mk_s3_client(profile,
                           creds=creds,
                           region_name=region_name,
                           session=session,
                           use_ssl=use_ssl,
                           **cfg)
        _cache[key] = s3

    return s3
Exemple #3
0
def test_thread_local_cache():
    name = "test_0123394"
    v = {}

    assert thread_local_cache(name, v) is v
    assert thread_local_cache(name) is v
    assert thread_local_cache(name, purge=True) is v
    assert thread_local_cache(name, 33) == 33
    assert thread_local_cache(name, purge=True) == 33

    assert thread_local_cache("no_such_key", purge=True) is None
    assert thread_local_cache("no_such_key", 111, purge=True) == 111
Exemple #4
0
def s3_client(profile: Optional[str] = None,
              creds: Optional[ReadOnlyCredentials] = None,
              region_name: Optional[str] = None,
              session: Optional[Session] = None,
              aws_unsigned: Optional[bool] = None,
              use_ssl: bool = True,
              cache: Union[bool, str] = False,
              **cfg) -> botocore.client.BaseClient:
    """ Construct s3 client with configured region_name.

    :param profile: profile name to lookup (only used if session is not supplied)
    :param creds: Override credentials with supplied data
    :param region_name: region_name to use, overrides session setting
    :param aws_unsigned: Do not use any credentials when accessing S3 resources
    :param session: botocore session to use
    :param use_ssl: Whether to connect via http or https
    :param cache: ``True`` - store/lookup s3 client in thread local cache.
                  ``"purge"`` - delete from cache and return what was there to begin with

    :param cfg: passed on to ``botocore.client.Config(..)``

    """
    if aws_unsigned is None:
        if creds is None:
            aws_unsigned = _aws_unsigned_check_env()
        else:
            aws_unsigned = False

    if aws_unsigned:
        cfg.update(signature_version=botocore.UNSIGNED)

    if not cache:
        return _mk_s3_client(profile,
                             creds=creds,
                             region_name=region_name,
                             session=session,
                             use_ssl=use_ssl,
                             **cfg)

    _cache = thread_local_cache("__aws_s3_cache", {})

    key = _s3_cache_key(profile=profile,
                        region_name=region_name,
                        creds=creds,
                        aws_unsigned=aws_unsigned)

    if cache == "purge":
        return _cache.pop(key, None)

    s3 = _cache.get(key, None)

    if s3 is None:
        s3 = _mk_s3_client(profile,
                           creds=creds,
                           region_name=region_name,
                           session=session,
                           use_ssl=use_ssl,
                           **cfg)
        _cache[key] = s3

    return s3