Read data as bytes.
(self)
| 147 | raise ValueError(f"Unable to get string for blob {self}") |
| 148 | |
| 149 | def as_bytes(self) -> bytes: |
| 150 | """Read data as bytes.""" |
| 151 | if isinstance(self.data, bytes): |
| 152 | return self.data |
| 153 | elif isinstance(self.data, str): |
| 154 | return self.data.encode(self.encoding) |
| 155 | elif self.data is None and self.path: |
| 156 | with open(str(self.path), "rb") as f: |
| 157 | return f.read() |
| 158 | else: |
| 159 | raise ValueError(f"Unable to get bytes for blob {self}") |
| 160 | |
| 161 | @contextlib.contextmanager |
| 162 | def as_bytes_io(self) -> Generator[Union[BytesIO, BufferedReader], None, None]: |