1
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

1
votes

I believe your goal as follows.

  • You want to upload a TAR file of /root/test.tar to Drive API using Drive API with googleapis for python.
  • drive_service in your script can be used for uploaded the file.
  • You have already been able to use Drive API.

Modification points:

  • In your script, you are trying to upload the TAR file. In this case, it seems that the mimeType of uploaded file is application/x-tar. Ref But 'mimeType': 'application/vnd.google-apps.file' is set to the metadata (file_metadata). And also, mimetype='application/vnd.google-apps.file' is used for MediaFileUpload. I think that those might be the reason of your issue.
    • In my environment, when I tested your script, I could confirm that the same error of Bad Request.
    • In this case, how about giving the mimeType of application/x-tar to file_metadata and/or MediaFileUpload?

When above points are reflected to your script, it becomes as follows.

Modified script:

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)
file_metadata = {'name': target, 'parents': [parent_id]}
media = MediaFileUpload(source, mimetype='application/x-tar', chunksize=1024*1024, resumable=True)

or

file_metadata = {'name': target, 'parents': [parent_id], 'mimeType': 'application/x-tar'}
media = MediaFileUpload(source, chunksize=1024*1024, resumable=True)
  • Or, it's both as follows.

      file_metadata = {'name': target, 'parents': [parent_id], 'mimeType': 'application/x-tar'}
      media = MediaFileUpload(source, mimetype='application/x-tar', chunksize=1024*1024, resumable=True)
    

Note:

  • In my environment, I confirmed that above patterns worked. But I'm not sure about your actual environment. So please test above.

  • At Drive API, it seems that the mimeType can be added from the file extension of the uploaded file. So, in this case, even when the mimeType is not used, the script works and the file can be uploaded. But when I tested the TAR file, the mimeType is set as application/x-tar-compressed. By this, although the file is completely uploaded, the file cannot be opened. So I proposed to add the mimeType of application/x-tar. For example, when the zip file is uploaded, the following script can be also used. In this case, the uploaded file has the mimeType of application/x-zip-compressed and it can be opened.

      file_metadata = {'name': target, 'parents': [parent_id]}
      media = MediaFileUpload(source, chunksize=1024*1024, resumable=True)
    

Reference: