File.stream
File.stream(*, start_byte=0, end_byte=None) → io.BytesIO
Read the file as a BytesIO stream. To convert to a TextIO stream, wrap with io.TextIOWrapper
. Note that by default the underlying stream won't be closed until fully consumed – it is strongly recommended to make the request within a with
statement to ensure it’s always closed.
Parameters:
start_byte
: int, default 0
The offset byte at which to start reading the file. Allows for reading part of a file. If not specified, the file will be read from the start.
end_byte
: int, default None
The byte at which to stop reading the file, inclusive. Allows for streaming part of a file. If not specified, the file will be streamed to the end.
Returns:
Examples
from io import TextIOWrapper
f = redivis.file("rnwk-acs3famee.pVr4Gzq54L3S9pblMZTs5Q")
with f.stream() as f_stream:
f_stream.read(100) # read 100 bytes
with TextIOWrapper(f.stream()) as f_stream:
f_stream.readline() # read first line
# We can use streams to work with files similarly
# to if they were on disk, e.g.:
from PIL import Image
# References the file bogota.tiff at https://redivis.com/datasets/yz1s-d09009dbb/files/4c10-d8rqhswh8.zz7AB9NJB2ctNpDGKbRD7w
file = redivis.file("4c10-d8rqhswh8.zz7AB9NJB2ctNpDGKbRD7w")
# Pass the file as a bytesIO stream to PIL to load as an image
print(Image.open(file.stream()))
Last updated
Was this helpful?