@@ -249,19 +249,25 @@ class MultipartUpload(UploadBase):
249
249
upload_url (str): The URL where the content will be uploaded.
250
250
headers (Optional[Mapping[str, str]]): Extra headers that should
251
251
be sent with the request, e.g. headers for encrypted data.
252
- checksum ( Optional([str]) ): The type of checksum to compute to verify
252
+ checksum Optional([str]): The type of checksum to compute to verify
253
253
the integrity of the object. The request metadata will be amended
254
254
to include the computed value. Using this option will override a
255
- manually-set checksum value. Supported values are "md5", "crc32c"
256
- and None. The default is None.
255
+ manually-set checksum value. Supported values are "md5",
256
+ "crc32c", "auto", and None. The default is "auto", which will try
257
+ to detect if the C extension for crc32c is installed and fall back
258
+ to md5 otherwise.
257
259
258
260
Attributes:
259
261
upload_url (str): The URL where the content will be uploaded.
260
262
"""
261
263
262
- def __init__ (self , upload_url , headers = None , checksum = None ):
264
+ def __init__ (self , upload_url , headers = None , checksum = "auto" ):
263
265
super (MultipartUpload , self ).__init__ (upload_url , headers = headers )
264
266
self ._checksum_type = checksum
267
+ if self ._checksum_type == "auto" :
268
+ self ._checksum_type = (
269
+ "crc32c" if _helpers ._is_crc32c_available_and_fast () else "md5"
270
+ )
265
271
266
272
def _prepare_request (self , data , metadata , content_type ):
267
273
"""Prepare the contents of an HTTP request.
@@ -355,13 +361,15 @@ class ResumableUpload(UploadBase):
355
361
chunk_size (int): The size of each chunk used to upload the resource.
356
362
headers (Optional[Mapping[str, str]]): Extra headers that should
357
363
be sent with every request.
358
- checksum ( Optional([str]) ): The type of checksum to compute to verify
364
+ checksum Optional([str]): The type of checksum to compute to verify
359
365
the integrity of the object. After the upload is complete, the
360
- server-computed checksum of the resulting object will be read
366
+ server-computed checksum of the resulting object will be checked
361
367
and google.cloud.storage.exceptions.DataCorruption will be raised on
362
368
a mismatch. The corrupted file will not be deleted from the remote
363
- host automatically. Supported values are "md5", "crc32c" and None.
364
- The default is None.
369
+ host automatically. Supported values are "md5", "crc32c", "auto",
370
+ and None. The default is "auto", which will try to detect if the C
371
+ extension for crc32c is installed and fall back to md5 otherwise.
372
+
365
373
366
374
Attributes:
367
375
upload_url (str): The URL where the content will be uploaded.
@@ -371,7 +379,7 @@ class ResumableUpload(UploadBase):
371
379
:data:`.UPLOAD_CHUNK_SIZE`.
372
380
"""
373
381
374
- def __init__ (self , upload_url , chunk_size , checksum = None , headers = None ):
382
+ def __init__ (self , upload_url , chunk_size , checksum = "auto" , headers = None ):
375
383
super (ResumableUpload , self ).__init__ (upload_url , headers = headers )
376
384
if chunk_size % UPLOAD_CHUNK_SIZE != 0 :
377
385
raise ValueError (
@@ -383,6 +391,10 @@ def __init__(self, upload_url, chunk_size, checksum=None, headers=None):
383
391
self ._bytes_uploaded = 0
384
392
self ._bytes_checksummed = 0
385
393
self ._checksum_type = checksum
394
+ if self ._checksum_type == "auto" :
395
+ self ._checksum_type = (
396
+ "crc32c" if _helpers ._is_crc32c_available_and_fast () else "md5"
397
+ )
386
398
self ._checksum_object = None
387
399
self ._total_bytes = None
388
400
self ._resumable_url = None
@@ -1185,9 +1197,10 @@ class XMLMPUPart(UploadBase):
1185
1197
be sent with every request.
1186
1198
checksum (Optional([str])): The type of checksum to compute to verify
1187
1199
the integrity of the object. The request headers will be amended
1188
- to include the computed value. Supported values are "md5", "crc32c"
1189
- and None. The default is None.
1190
-
1200
+ to include the computed value. Supported values are "md5", "crc32c",
1201
+ "auto" and None. The default is "auto", which will try to detect if
1202
+ the C extension for crc32c is installed and fall back to md5
1203
+ otherwise.
1191
1204
Attributes:
1192
1205
upload_url (str): The URL of the object (without query parameters).
1193
1206
upload_id (str): The ID of the upload from the initialization response.
@@ -1208,7 +1221,7 @@ def __init__(
1208
1221
end ,
1209
1222
part_number ,
1210
1223
headers = None ,
1211
- checksum = None ,
1224
+ checksum = "auto" ,
1212
1225
):
1213
1226
super ().__init__ (upload_url , headers = headers )
1214
1227
self ._filename = filename
@@ -1218,6 +1231,10 @@ def __init__(
1218
1231
self ._part_number = part_number
1219
1232
self ._etag = None
1220
1233
self ._checksum_type = checksum
1234
+ if self ._checksum_type == "auto" :
1235
+ self ._checksum_type = (
1236
+ "crc32c" if _helpers ._is_crc32c_available_and_fast () else "md5"
1237
+ )
1221
1238
self ._checksum_object = None
1222
1239
1223
1240
@property
0 commit comments