1
1
from datetime import datetime , timezone
2
2
import json
3
3
import logging
4
+ import mimetypes
4
5
import os
5
6
6
7
import requests
@@ -171,6 +172,24 @@ def check_errors(keywords, *path):
171
172
172
173
return response ["data" ]
173
174
175
+ def upload_file (self , path ):
176
+ """Uploads given path to local file.
177
+
178
+ Also includes best guess at the content type of the file.
179
+
180
+ Args:
181
+ path (str): path to local file to be uploaded.
182
+ Returns:
183
+ str, the URL of uploaded data.
184
+ Raises:
185
+ labelbox.exceptions.LabelboxError: If upload failed.
186
+
187
+ """
188
+ content_type , _ = mimetypes .guess_type (path )
189
+ basename = os .path .basename (path )
190
+ with open (path , "rb" ) as f :
191
+ return self .upload_data (data = (basename , f .read (), content_type ))
192
+
174
193
def upload_data (self , data ):
175
194
""" Uploads the given data (bytes) to Labelbox.
176
195
@@ -183,8 +202,8 @@ def upload_data(self, data):
183
202
"""
184
203
request_data = {
185
204
"operations" : json .dumps ({
186
- "variables" : {"file" : None , "contentLength" : len (data ), "sign" : False },
187
- "query" : """mutation UploadFile($file: Upload!, $contentLength: Int!,
205
+ "variables" : {"file" : None , "contentLength" : len (data ), "sign" : False },
206
+ "query" : """mutation UploadFile($file: Upload!, $contentLength: Int!,
188
207
$sign: Boolean) {
189
208
uploadFile(file: $file, contentLength: $contentLength,
190
209
sign: $sign) {url filename} } """ ,}),
0 commit comments