smart_open Documentation

repository·develop·Indexed 25 days ago

https://github.com/piskvorky/smart_open

A Python 3 library for the efficient streaming of very large files to and from various storage backends, including S3, GCS, Azure Blob Storage, HDFS, HTTP, and SFTP. It provides a unified API as a drop-in replacement for the built-in open() function, supporting transparent (de)compression for formats such as gzip, bz2, xz, lz4, and zstd.

Tokens
15.7K
Snippets
30
Records
105
Agent score
85%

What's inside smart_open

  1. Access a Specific Version of an S3 Object

    develop

    When working with versioned S3 buckets, you can retrieve a specific version of an object by passing version_id inside the transport_params dictionary. If version_id is not specified, smart_open reads the most recent version.

    >>> import boto3
    >>> from smart_open import open
    >>> versions = ['KiQpZPsKI5Dm2oJZy_RzskTOtl2snjBg', 'N0GJcE3TQCKtkaS.gF.MUBZS85Gs3hzn']
    >>> for v in versions:
    ...     with open('s3://smart-open-versioned/demo.txt', transport_params={'version_id': v}) as fin:
    ...         print(v, repr(fin.read()))
    KiQpZPsKI5Dm2oJZy_RzskTOtl2snjBg 'second version\n'
    N0GJcE3TQCKtkaS.gF.MUBZS85Gs3hzn 'first version\n'
    
    >>> # If you don't specify a version, smart_open will read the most recent one
    >>> with open('s3://smart-open-versioned/demo.txt') as fin:
    ...     print(repr(fin.read()))
    'second version\n'
  2. Read and Write Zip Files using smart_open and zipfile

    develop

    smart_open does not support zip files natively, but you can integrate it with Python's zipfile module. smart_open handles the I/O (streaming from/to local or remote storage) while zipfile handles the compression and member lookup.

    >>> from smart_open import open
    >>> import zipfile
    >>> with open('sampledata/hello.zip', 'rb') as fin:
    ...     with zipfile.ZipFile(fin) as zip:
    ...         for info in zip.infolist():
    ...             file_bytes = zip.read(info.filename)
    ...             print('%r: %r' % (info.filename, file_bytes.decode('utf-8')))
    'hello/': ''
    'hello/en.txt': 'hello world!\n'
    'hello/ru.txt': 'здравствуй, мир!\n'
  3. Install smart_open with optional dependencies

    develop

    By default, smart_open does not install any dependencies to keep the installation size small. You can install specific storage solutions using extras defined in pyproject.toml.

    To install specific transports:

    pip install 'smart_open[s3,gcs,azure,http,webhdfs,ssh,zst,lz4]'

    To install all supported dependencies:

    pip install 'smart_open[all]'

    Note on external dependencies:

    • HTTP Kerberos auth (kerberos=True) requires requests-kerberos: pip install requests-kerberos
    • SSH GSSAPI/Kerberos auth (gss_* options) requires paramiko's GSSAPI support: pip install 'paramiko[gssapi]'
    • hdfs:// and viewfs:// require the Hadoop hdfs command-line client to be installed and available on $PATH.
    pip install 'smart_open[s3,gcs,azure,http,webhdfs,ssh,zst,lz4]'
  4. Write to S3 Efficiently (Memory Management)

    develop

    By default, smart_open buffers the most recent 50MB part of a multipart upload in memory. To reduce memory footprint, you can:

    1. Use smaller part sizes: Set min_part_size in transport_params (minimum 5MB).
    2. Use a temporary file as a buffer: Pass a tempfile.NamedTemporaryFile object via the writebuffer key in transport_params. This reduces memory usage at the cost of additional disk I/O.
    # Option 1: Smaller part sizes
    import boto3
    from smart_open import open
    
    tp = {"min_part_size": 5 * 1024**2}
    with open("s3://bucket/key", "w", transport_params=tp) as fout:
        fout.write(lots_of_data)
    
    # Option 2: Using a temporary file buffer
    import boto3
    from smart_open import open
    import tempfile
    
    with tempfile.NamedTemporaryFile() as tmp:
        transport_params = {"writebuffer": tmp}
        with open("s3://bucket/key", "w", transport_params=transport_params) as fout:
            fout.write(lots_of_data)
  5. Install smart_open with specific storage dependencies

    develop

    Since version 3.0.0, smart_open does not install storage-specific dependencies (like boto3 for S3) by default to keep installations lean. You must install the extras for the protocols you intend to use.

    • For S3 support: pip install smart_open[s3]
    • For HTTP/HTTPS/WebHDFS support: pip install smart_open[http]
    • To install everything (legacy behavior): pip install smart_open[all]
    pip install smart_open[s3]
    pip install smart_open[http]
    pip install smart_open[all]
  6. Access S3 Anonymously

    develop

    To access public S3 buckets without using your own AWS credentials, pass a botocore.client.Config with signature_version=botocore.UNSIGNED via the transport_params dictionary.

    >>> import boto3
    >>> import botocore
    >>> import botocore.client
    >>> from smart_open import open
    >>> config = botocore.client.Config(signature_version=botocore.UNSIGNED)
    >>> params = {'client': boto3.client('s3', config=config)}
    >>> with open('s3://commoncrawl/robots.txt', transport_params=params) as fin:
    ...    fin.readline()
    'User-Agent: *\n'