English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# The BlobReader Class

A Python application can read Blobstore values through a file-like interface, implemented by the BlobReader class. The reader implementation streams data from the Blobstore as needed, so values larger than 1 megabyte can be read, using multiple service calls behind the scenes. This interface is read-only: Blobstore values cannot be modified directly by the application.

`BlobReader` is provided by the `google.appengine.ext.blobstore` module.

- Introduction
- `BlobReader()`
- Instance Methods
- Instance properties:
  - `blob_info`

## Introduction

An application can read data from Blobstore values using an interface similar to a Python `file` object. This interface can start reading a value at any byte position, and uses multiple service calls and buffering, so an application can access the full size of the value despite the limit on the size of a single service call response.

The BlobReader class can take one of three values as an argument to its constructor:

- A `BlobKey`
- The string form of a BlobKey
- A `BlobInfo` object

The object implements the familiar file methods for reading the value. The application cannot modify the Blobstore value; file methods for writing are not implemented.

    from google.appengine.ext import blobstore

    # blob_key = ...

    # Instantiate a BlobReader for a given Blobstore value.
    blob_reader = blobstore.BlobReader(blob_key)

    # Instantiate a BlobReader for a given Blobstore value, setting the
    # buffer size to 1 MB.
    blob_reader = blobstore.BlobReader(blob_key, buffer_size=1048576)

    # Instantiate a BlobReader for a given Blobstore value, setting the
    # initial read position.
    blob_reader = blobstore.BlobReader(blob_key, position=4194304)

    # Read the entire value into memory. This may take a while depending
    # on the size of the value and the size of the read buffer, and is not
    # recommended for large values.
    value = blob_reader.read()

    # Set the read position, then read 100 bytes.
    blob_reader.seek(2097152)
    data = blob_reader.read(100)

    # Read the value, one line (up to and including a '\n' character) at a time.
    for line in blob_reader:
        # ...

## Constructor

class BlobReader(blob_key, buffer_size=131072, position=0)

  A file-like interface for reading a Blobstore value.

  Arguments

  blob
    The `BlobInfo`, `BlobKey`, or string form of a blob key of the Blobstore value to read. Required.
  buffer_size
    The size of the input buffer, as a number of bytes. The default is 128 kilobytes (131,072 bytes). The buffer size cannot exceed 1 megabyte (1,048,576 bytes).
  position
    The byte position of the first byte to read. The default is to start reading at the beginning of the value (byte position 0).

## Instance Methods

A `BlobReader` instance has methods and properties similar to a Python `file` object. Methods that would modify a file (such as `write()`) are not implemented, and raise an `IOError`. You can pass a BlobReader object to code expecting a (read-only) file interface (such as the `zipfile` module).

Methods and interfaces include:

- iterator and iterable interfaces
- interfaces to support serialization of BlobReader state via `pickle`
- `close()`
- `read(size=-1)`
- `readline(size=-1)`
- `readlines(sizehint=None)`
- `seek(offset, whence=SEEK_SET)`
- `tell()`
- the `closed` property

## Instance Properties

In addition to the file-like interface, the `BlobReader` class provides read-only access to a Blobstore value's `BlobInfo` object through the `blob_info` property.

blob_info
  The `BlobInfo` object for the instance's Blobstore value.