Advertisement
kopyl

Untitled

Feb 22nd, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. @handle_upload_errors
  2. def upload_file(
  3.     local_file_path,
  4.     google_location="",  # folder id on Google Drive or name from google_folders dict
  5.     file_name=""
  6. ):
  7.     if not file_name:
  8.         file_name = local_file_path
  9.  
  10.     p(f"File {local_file_path} uploading to folder {google_location}")
  11.  
  12.     if google_location in google_folders:
  13.         google_location = google_folders[google_location]
  14.  
  15.     file_metadata = {
  16.         'name': file_name,
  17.         'parents': [google_location]
  18.     }
  19.     media = googleapiclient.http.MediaFileUpload(local_file_path, resumable=True)
  20.     """
  21.    resumable allows uploading files over 5MB.
  22.    """
  23.     uploaded_file_properties = service.files().create(
  24.         body=file_metadata,
  25.         media_body=media,
  26.         fields='id, parents',  # to retrun an id of uploaded file
  27.     ).execute()
  28.     p(f"File {file_name} uploaded")
  29.     return uploaded_file_properties
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement