Skip to content
Advertisement

Google Drive API adding some string in file

I have been trying to replace files using google API only with the Python HTTPS module. But when I replace it. It adds some string to the file with text. Code:

headers = {
        "Authorization": "Bearer " + str(Acesstoken),
    }

files = {  
    'file': open("./Test.txt", "rb"),
    "mimeType": "text/plain"
    }


r2= requests.patch(
    "https://www.googleapis.com/upload/drive/v3/files/" + updateFileId,
    headers= headers,
    files = files,
)

Text On Google Drive Before Replacing File:

Test

Text On Google Drive After Replacing File:

--a164a6e367e3577590ab9eb85b487e21
Content-Disposition: form-data; name="file"; filename="Test.txt"

Test 2
--a164a6e367e3577590ab9eb85b487e21
Content-Disposition: form-data; name="mimeType"; filename="mimeType"

text/plain
--a164a6e367e3577590ab9eb85b487e21--

Advance Thanks to @DaImTo

I’ve just unlocked talk in chat. I don’t mind talking.

Advertisement

Answer

I thought that in your script, the endpoint is required to be modified. In your situation, please add the query parameter of uploadType=media as follows.

By the way, in this script, it supposes that the file of Google Drive is the text file. From your comment, it seems that you are trying to overwrite the ZIP file as text data. In this case, the mimeType is not changed. So I would like to recommend to be the same mimeType between a file on Google Drive and the uploading file.

Pattern 1

If you want to update the text file on Google Drive by ./Test.txt, how about the following modification?

Modified script:

In this script, the text file on Google Drive is overwritten by ./Test.txt.

import requests

accessToken = '###' # Please set your access token.
updateFileId = '###' # Please set the file ID fot the text file on Google Drive.

headers = {"Authorization": "Bearer " + accessToken}
file = open("./Test.txt", "rb")
r2 = requests.patch(
    "https://www.googleapis.com/upload/drive/v3/files/" + updateFileId + "?uploadType=media",
    headers=headers,
    data=file,
)

Pattern 2

If you want to append the text file on Google Drive by ./Test.txt, how about the following modification?

Modified script:

import io
import requests

accessToken = '###' # Please set your access token.
updateFileId = '###' # Please set the file ID fot the text file on Google Drive.

headers = {"Authorization": "Bearer " + accessToken}

r1 = requests.get("https://www.googleapis.com/drive/v3/files/" + updateFileId + "?alt=media", headers=headers)

file = open("./Test.txt", "rt")
merged = r1.text + file.read()
r2 = requests.patch(
    "https://www.googleapis.com/upload/drive/v3/files/" + updateFileId + "?uploadType=media",
    headers=headers,
    data=io.BytesIO(bytes(merged, 'utf-8')),
)
  • In this case, first, the text data is retrieved. And, by merging the texts of downloaded text and the text of ./Test.txt, the text file is updated.

Note:

  • If you want to overwrite a ZIP file on Google Drive, please use a sample ZIP file as the uploading file.

References:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement