File tree Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -105,7 +105,7 @@ def create_file(
105
105
file ["name" ] = name
106
106
if display_name is not None :
107
107
file ["displayName" ] = display_name
108
-
108
+
109
109
if isinstance (path , IOBase ):
110
110
media = googleapiclient .http .MediaIoBaseUpload (
111
111
fd = path , mimetype = mime_type , resumable = resumable
@@ -114,7 +114,7 @@ def create_file(
114
114
media = googleapiclient .http .MediaFileUpload (
115
115
filename = path , mimetype = mime_type , resumable = resumable
116
116
)
117
-
117
+
118
118
request = self ._discovery_api .media ().upload (body = {"file" : file }, media_body = media )
119
119
for key , value in metadata :
120
120
request .headers [key ] = value
Original file line number Diff line number Diff line change @@ -58,7 +58,13 @@ def upload_file(
58
58
"""
59
59
client = get_default_file_client ()
60
60
61
- if not isinstance (path , IOBase ):
61
+ if isinstance (path , IOBase ):
62
+ if mime_type is None :
63
+ raise ValueError (
64
+ "Unknown mime type: When passing a file like object to `path` (instead of a\n "
65
+ " path-like object) you must set the `mime_type` argument"
66
+ )
67
+ else :
62
68
path = pathlib .Path (os .fspath (path ))
63
69
64
70
if display_name is None :
@@ -67,9 +73,12 @@ def upload_file(
67
73
if mime_type is None :
68
74
mime_type , _ = mimetypes .guess_type (path )
69
75
70
- if mime_type is None :
71
- # Guess failed or IOBase, use octet-stream.
72
- mime_type = 'application/octet-stream'
76
+ if mime_type is None :
77
+ if mime_type is None :
78
+ raise ValueError (
79
+ "Unknown mime type: Could not determine the mimetype for your file\n "
80
+ " please set the `mime_type` argument"
81
+ )
73
82
74
83
if name is not None and "/" not in name :
75
84
name = f"files/{ name } "
Original file line number Diff line number Diff line change @@ -102,12 +102,13 @@ def test_video_metadata(self):
102
102
protos .File (
103
103
uri = "https://test" ,
104
104
state = "ACTIVE" ,
105
+ mime_type = "video/quicktime" ,
105
106
video_metadata = dict (video_duration = datetime .timedelta (seconds = 30 )),
106
107
error = dict (code = 7 , message = "ok?" ),
107
108
)
108
109
)
109
110
110
- f = genai .upload_file (path = "dummy" )
111
+ f = genai .upload_file (path = "dummy.mov " )
111
112
self .assertEqual (google .rpc .status_pb2 .Status (code = 7 , message = "ok?" ), f .error )
112
113
self .assertEqual (
113
114
protos .VideoMetadata (dict (video_duration = datetime .timedelta (seconds = 30 ))),
You can’t perform that action at this time.
0 commit comments