Skip to content

Commit 73107c3

Browse files
feat: rename 'Blob.download_as_{string,bytes}', add 'Blob.download_as_text' (#182)
Leave 'Blob.download_as_string' as a deprecated alias for 'download_as_bytes'. Co-authored-by: Tres Seaver <[email protected]>
1 parent e8a8638 commit 73107c3

File tree

3 files changed

+340
-55
lines changed

3 files changed

+340
-55
lines changed

google/cloud/storage/blob.py

+175-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ def download_to_filename(
11001100
mtime = updated.timestamp()
11011101
os.utime(file_obj.name, (mtime, mtime))
11021102

1103-
def download_as_string(
1103+
def download_as_bytes(
11041104
self,
11051105
client=None,
11061106
start=None,
@@ -1180,6 +1180,180 @@ def download_as_string(
11801180
)
11811181
return string_buffer.getvalue()
11821182

1183+
def download_as_string(
1184+
self,
1185+
client=None,
1186+
start=None,
1187+
end=None,
1188+
raw_download=False,
1189+
if_generation_match=None,
1190+
if_generation_not_match=None,
1191+
if_metageneration_match=None,
1192+
if_metageneration_not_match=None,
1193+
timeout=_DEFAULT_TIMEOUT,
1194+
):
1195+
"""(Deprecated) Download the contents of this blob as a bytes object.
1196+
1197+
If :attr:`user_project` is set on the bucket, bills the API request
1198+
to that project.
1199+
1200+
.. note::
1201+
Deprecated alias for :meth:`download_as_bytes`.
1202+
1203+
:type client: :class:`~google.cloud.storage.client.Client` or
1204+
``NoneType``
1205+
:param client: (Optional) The client to use. If not passed, falls back
1206+
to the ``client`` stored on the blob's bucket.
1207+
1208+
:type start: int
1209+
:param start: (Optional) The first byte in a range to be downloaded.
1210+
1211+
:type end: int
1212+
:param end: (Optional) The last byte in a range to be downloaded.
1213+
1214+
:type raw_download: bool
1215+
:param raw_download:
1216+
(Optional) If true, download the object without any expansion.
1217+
1218+
:type if_generation_match: long
1219+
:param if_generation_match: (Optional) Make the operation conditional on whether
1220+
the blob's current generation matches the given value.
1221+
Setting to 0 makes the operation succeed only if there
1222+
are no live versions of the blob.
1223+
1224+
:type if_generation_not_match: long
1225+
:param if_generation_not_match: (Optional) Make the operation conditional on whether
1226+
the blob's current generation does not match the given
1227+
value. If no live blob exists, the precondition fails.
1228+
Setting to 0 makes the operation succeed only if there
1229+
is a live version of the blob.
1230+
1231+
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
1232+
blob's current metageneration matches the given value.
1233+
1234+
:type if_metageneration_not_match: long
1235+
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
1236+
blob's current metageneration does not match the given value.
1237+
1238+
:type timeout: float or tuple
1239+
:param timeout:
1240+
(Optional) The number of seconds the transport should wait for the
1241+
server response. Depending on the retry strategy, a request may be
1242+
repeated several times using the same timeout each time.
1243+
Can also be passed as a tuple (connect_timeout, read_timeout).
1244+
See :meth:`requests.Session.request` documentation for details.
1245+
1246+
:rtype: bytes
1247+
:returns: The data stored in this blob.
1248+
1249+
:raises: :class:`google.cloud.exceptions.NotFound`
1250+
"""
1251+
warnings.warn(
1252+
"Blob.download_as_string() is deprecated and will be removed in future."
1253+
"Use Blob.download_as_bytes() instead.",
1254+
PendingDeprecationWarning,
1255+
stacklevel=1,
1256+
)
1257+
return self.download_as_bytes(
1258+
client=client,
1259+
start=start,
1260+
end=end,
1261+
raw_download=raw_download,
1262+
if_generation_match=if_generation_match,
1263+
if_generation_not_match=if_generation_not_match,
1264+
if_metageneration_match=if_metageneration_match,
1265+
if_metageneration_not_match=if_metageneration_not_match,
1266+
timeout=timeout,
1267+
)
1268+
1269+
def download_as_text(
1270+
self,
1271+
client=None,
1272+
start=None,
1273+
end=None,
1274+
raw_download=False,
1275+
encoding="utf-8",
1276+
if_generation_match=None,
1277+
if_generation_not_match=None,
1278+
if_metageneration_match=None,
1279+
if_metageneration_not_match=None,
1280+
timeout=_DEFAULT_TIMEOUT,
1281+
):
1282+
"""Download the contents of this blob as a string.
1283+
1284+
If :attr:`user_project` is set on the bucket, bills the API request
1285+
to that project.
1286+
1287+
:type client: :class:`~google.cloud.storage.client.Client` or
1288+
``NoneType``
1289+
:param client: (Optional) The client to use. If not passed, falls back
1290+
to the ``client`` stored on the blob's bucket.
1291+
1292+
:type start: int
1293+
:param start: (Optional) The first byte in a range to be downloaded.
1294+
1295+
:type end: int
1296+
:param end: (Optional) The last byte in a range to be downloaded.
1297+
1298+
:type raw_download: bool
1299+
:param raw_download:
1300+
(Optional) If true, download the object without any expansion.
1301+
1302+
:type encoding: str
1303+
:param encoding: (Optional) The data of the blob will be decoded by
1304+
encoding method. Defaults to UTF-8. Apply only
1305+
if the value of ``blob.content_encoding`` is None.
1306+
1307+
:type if_generation_match: long
1308+
:param if_generation_match: (Optional) Make the operation conditional on whether
1309+
the blob's current generation matches the given value.
1310+
Setting to 0 makes the operation succeed only if there
1311+
are no live versions of the blob.
1312+
1313+
:type if_generation_not_match: long
1314+
:param if_generation_not_match: (Optional) Make the operation conditional on whether
1315+
the blob's current generation does not match the given
1316+
value. If no live blob exists, the precondition fails.
1317+
Setting to 0 makes the operation succeed only if there
1318+
is a live version of the blob.
1319+
1320+
:param if_metageneration_match: (Optional) Make the operation conditional on whether the
1321+
blob's current metageneration matches the given value.
1322+
1323+
:type if_metageneration_not_match: long
1324+
:param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
1325+
blob's current metageneration does not match the given value.
1326+
1327+
:type timeout: float or tuple
1328+
:param timeout:
1329+
(Optional) The number of seconds the transport should wait for the
1330+
server response. Depending on the retry strategy, a request may be
1331+
repeated several times using the same timeout each time.
1332+
Can also be passed as a tuple (connect_timeout, read_timeout).
1333+
See :meth:`requests.Session.request` documentation for details.
1334+
1335+
:rtype: text
1336+
:returns: The data stored in this blob.
1337+
1338+
:raises: :class:`google.cloud.exceptions.NotFound`
1339+
"""
1340+
data = self.download_as_bytes(
1341+
client=client,
1342+
start=start,
1343+
end=end,
1344+
raw_download=raw_download,
1345+
if_generation_match=if_generation_match,
1346+
if_generation_not_match=if_generation_not_match,
1347+
if_metageneration_match=if_metageneration_match,
1348+
if_metageneration_not_match=if_metageneration_not_match,
1349+
timeout=timeout,
1350+
)
1351+
1352+
if self.content_encoding:
1353+
return data.decode(self.content_encoding)
1354+
else:
1355+
return data.decode(encoding)
1356+
11831357
def _get_content_type(self, content_type, filename=None):
11841358
"""Determine the content type from the current object.
11851359

0 commit comments

Comments
 (0)