google.resumable_media.requests.download

Support for downloading media from Google APIs.

class google.resumable_media.requests.download.ChunkedDownload(media_url, chunk_size, stream, start=0, end=None, headers=None)

Bases: google.resumable_media.requests._helpers.RequestsMixin, google.resumable_media._download.ChunkedDownload

Download a resource in chunks from a Google API.

Parameters:
  • media_url (str) – The URL containing the media to be downloaded.
  • chunk_size (int) – The number of bytes to be retrieved in each request.
  • stream (IO[bytes]) – A write-able stream (i.e. file-like object) that will be used to concatenate chunks of the resource as they are downloaded.
  • start (int) – The first byte in a range to be downloaded. If not provided, defaults to 0.
  • end (int) – The last byte in a range to be downloaded. If not provided, will download to the end of the media.
  • headers (Optional [ Mapping [ str, str ] ]) – Extra headers that should be sent with each request, e.g. headers for data encryption key headers.
media_url

The URL containing the media to be downloaded.

Type:str
start

The first byte in a range to be downloaded.

Type:Optional [ int ]
end

The last byte in a range to be downloaded.

Type:Optional [ int ]
chunk_size

The number of bytes to be retrieved in each request.

Type:int
Raises:ValueError – If start is negative.
property bytes_downloaded

Number of bytes that have been downloaded.

Type:int
consume_next_chunk(transport)

Consume the next chunk of the resource to be downloaded.

Parameters:transport (Session) – A requests object which can make authenticated requests.
Returns:The HTTP response returned by transport.
Return type:Response
Raises:ValueError – If the current download has finished.
property finished

Flag indicating if the download has completed.

Type:bool
property invalid

Indicates if the download is in an invalid state.

This will occur if a call to consume_next_chunk() fails.

Type:bool
property total_bytes

The total number of bytes to be downloaded.

Type:Optional [ int ]
class google.resumable_media.requests.download.Download(media_url, stream=None, start=None, end=None, headers=None)

Bases: google.resumable_media.requests._helpers.RequestsMixin, google.resumable_media._download.Download

Helper to manage downloading a resource from a Google API.

“Slices” of the resource can be retrieved by specifying a range with start and / or end. However, in typical usage, neither start nor end is expected to be provided.

Parameters:
  • media_url (str) – The URL containing the media to be downloaded.
  • stream (IO[bytes]) – A write-able stream (i.e. file-like object) that the downloaded resource can be written to.
  • start (int) – The first byte in a range to be downloaded. If not provided, but end is provided, will download from the beginning to end of the media.
  • end (int) – The last byte in a range to be downloaded. If not provided, but start is provided, will download from the start to the end of the media.
  • headers (Optional [ Mapping [ str, str ] ]) – Extra headers that should be sent with the request, e.g. headers for encrypted data.
media_url

The URL containing the media to be downloaded.

Type:str
start

The first byte in a range to be downloaded.

Type:Optional [ int ]
end

The last byte in a range to be downloaded.

Type:Optional [ int ]
consume(transport)

Consume the resource to be downloaded.

If a stream is attached to this download, then the downloaded resource will be written to the stream.

Parameters:

transport (Session) – A requests object which can make authenticated requests.

Returns:

The HTTP response returned by transport.

Return type:

Response

Raises:
property finished

Flag indicating if the download has completed.

Type:bool