Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change mimeType of a file in Google Drive using the REST API

I have a file named config.json with the mimeType: application/octet-stream.

Google API Explorer - Get File Metatadatas

 GET https://www.googleapis.com/drive/v3/files/1b95F40yMjPYhdZjn8zZPr6P3wjUnxoM2

and the response:

{
 "kind": "drive#file",
 "id": "1b95F40yMjPYhdZjn8zZPr6P3wjUnxoM2",
 "name": "config.json",
 "mimeType": "application/octet-stream"
}

I want to change it to application/json.

Google API Explorer - Change File Metatadatas

PATCH https://www.googleapis.com/drive/v3/files/1b95F40yMjPYhdZjn8zZPr6P3wjUnxoM2

{
 "mimeType": "application/json"
}

But the mimeType didn't change...

{
 "kind": "drive#file",
 "id": "1b95F40yMjPYhdZjn8zZPr6P3wjUnxoM2",
 "name": "config.json",
 "mimeType": "application/octet-stream"
}

Any suggestions ?

Thank you.

like image 626
user8738918 Avatar asked Oct 14 '25 12:10

user8738918


1 Answers

Also in my environment, the mimeType cannot be changed by drive.files.update. So I always use this workaround. Although I'm not sure whether this is useful for you, how about this?

Use drive.files.copy

Endpoint is as follows.

POST https://www.googleapis.com/drive/v3/files/### file ID ###/copy

Request body is as follows.

{
 "mimeType": "application/json",
 "name": "samplename.json"
}

If this was not useful for you, I'm sorry.

Added:

Confirm:

At first, the mimeType of fileId1 is confirm. By the way, as a sample situation, this file is a PNG file. I uploaded this by changing the mimeType to application/octet-stream. This is used as the sample file.

Curl command:
curl \
  'https://www.googleapis.com/drive/v3/files/fileId1 \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed
Result:
{
 "kind": "drive#file",
 "id": "###",
 "name": "samplefile",
 "mimeType": "application/octet-stream"
}
  • From above curl command, it is found that the file of fileId1 has the mimeType of application/octet-stream.

Convert mimeType:

Curl command:
curl --request POST \
  'https://www.googleapis.com/drive/v3/files/fileId1/copy \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"mimeType":"application/json","name":"samplename.json"}' \
  --compressed
Result:
{
 "kind": "drive#file",
 "id": "fileId2",
 "name": "samplename.json",
 "mimeType": "application/json"
}
  • When above curl command is run for the file of fileId1, it was changed that the mimeType was changed from application/octet-stream to application/json.

Note:

  • There are 3 important points.

    1. OP wants to change the mimeType of application/octet-stream to application/json. This answer is for achieving this goal. Please be careful this.
    2. In this case, when the mimeType is changed, the data format is not changed. So please be also careful this.
    3. All mimeTypes cannot be changed. So please be also careful this.
  • For example, when the mimeType of excel file (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and application/vnd.ms-excel) can be converted to Google Spreadsheet (application/vnd.google-apps.spreadsheet). But the image file cannot be directly converted to Google Spreadsheet.

  • From it doesn't work for me. looks like the mimeType property is not working., I cannot understand about Mahdi Abdi's situation. So if you want to change the mimeType except for application/octet-stream to the mimeType except for application/json, this answer might not be able to be used. So please be careful this.

like image 110
Tanaike Avatar answered Oct 18 '25 02:10

Tanaike