0

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 ?

0

1 Answer 1

0

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')
Sign up to request clarification or add additional context in comments.

2 Comments

If you are getting a different error you may want to open a new question.
I am back to the initial issue. googleapiclient.errors.HttpError: <HttpError 400 when requesting googleapis.com/upload/drive/v3/… returned "Bad Request"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.