11from datetime import datetime , timezone
22import json
33import logging
4+ import mimetypes
45import os
56
67import requests
@@ -171,6 +172,24 @@ def check_errors(keywords, *path):
171172
172173 return response ["data" ]
173174
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+
174193 def upload_data (self , data ):
175194 """ Uploads the given data (bytes) to Labelbox.
176195
@@ -183,8 +202,8 @@ def upload_data(self, data):
183202 """
184203 request_data = {
185204 "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!,
188207 $sign: Boolean) {
189208 uploadFile(file: $file, contentLength: $contentLength,
190209 sign: $sign) {url filename} } """ ,}),
0 commit comments