1
1
"""
2
2
Google Cloud Storage pythonic interface
3
3
"""
4
+
4
5
import asyncio
5
6
import io
6
7
import json
7
8
import logging
9
+ import mimetypes
8
10
import os
9
11
import posixpath
10
12
import re
@@ -1391,7 +1393,7 @@ async def _put_file(
1391
1393
rpath ,
1392
1394
metadata = None ,
1393
1395
consistency = None ,
1394
- content_type = "application/octet-stream" ,
1396
+ content_type = None ,
1395
1397
chunksize = 50 * 2 ** 20 ,
1396
1398
callback = None ,
1397
1399
fixed_key_metadata = None ,
@@ -1401,6 +1403,10 @@ async def _put_file(
1401
1403
# enforce blocksize should be a multiple of 2**18
1402
1404
if os .path .isdir (lpath ):
1403
1405
return
1406
+ if content_type is None :
1407
+ content_type , _ = mimetypes .guess_type (lpath )
1408
+ if content_type is None :
1409
+ content_type = "application/octet-stream"
1404
1410
callback = callback or NoOpCallback ()
1405
1411
consistency = consistency or self .consistency
1406
1412
checker = get_consistency_checker (consistency )
@@ -1755,7 +1761,8 @@ def __init__(
1755
1761
the number we wrote; 'md5' does a full checksum. Any value other
1756
1762
than 'size' or 'md5' or 'crc32c' is assumed to mean no checking.
1757
1763
content_type: str
1758
- default is `application/octet-stream`. See the list of available
1764
+ default when unspecified is provided by mimetypes.guess_type or
1765
+ otherwise `application/octet-stream`. See the list of available
1759
1766
content types at https://www.iana.org/assignments/media-types/media-types.txt
1760
1767
metadata: dict
1761
1768
Custom metadata, in key/value pairs, added at file creation
@@ -1798,7 +1805,8 @@ def __init__(
1798
1805
else :
1799
1806
det = {}
1800
1807
self .content_type = content_type or det .get (
1801
- "contentType" , "application/octet-stream"
1808
+ "contentType" ,
1809
+ mimetypes .guess_type (self .path )[0 ] or "application/octet-stream" ,
1802
1810
)
1803
1811
self .metadata = metadata or det .get ("metadata" , {})
1804
1812
self .fixed_key_metadata = _convert_fixed_key_metadata (det , from_google = True )
0 commit comments