0
votes

I was trying to upload a resumable file upload on google api drive as per the documentation from https://developers.google.com/drive/api/v3/manage-uploads#python_1 . A sample code is given below, I am stuck with the current situation.

SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.appdata']
credentials = ServiceAccountCredentials.from_json_keyfile_name('json-file', SCOPES)
http=Http()
http.redirect_codes = http.redirect_codes - {308}
http_auth = credentials.authorize(http)
drive_service = build('drive', 'v3', http=http_auth,cache_discovery=False)

parent_id="folder-id"
source='/root/test.tar'
target='test.tar'
file_metadata = {'name': target, 'parents': [parent_id], 'mimeType': 'application/vnd.google-apps.file'}
media = MediaFileUpload(source,mimetype='application/vnd.google-apps.file',chunksize=1024*1024,resumable=True)
putfile=drive_service.files().create(body=file_metadata,media_body=media,fields='id').execute()
dest_id=putfile.get('id')

These lines are trowing the following errors. I have no idea about it.

raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=resumable returned "Bad Request">

I wonder what is making this issue ?

1

1 Answers

0
votes

You are missing chunk size.

parent_id = "folder-id"
source = '/root/test.tar'
target = 'test.tar'
file_metadata = {'name': target, 'parents': [parent_id], 
                 'mimeType': 'application/vnd.google-apps.file'}
media = MediaFileUpload(source, mimetype='application/vnd.google-apps.file', 
             chunksize=1024*1024, resumable=True)
putfile = drive_service.files().create(body=file_metadata,
                         media_body=media,fields='id').execute()
dest_id = putfile.get('id')