如何使用REST API更改Google云端硬盘中文件的mimeType

问题描述 投票:0回答:1

我有一个名为config.json的文件,其mimeType:application/octet-stream

Google API Explorer - Get File Metatadatas

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

和回复:

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

我想将其更改为application/json

Google API Explorer - Change File Metatadatas

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

{
 "mimeType": "application/json"
}

但是mimeType没有改变...

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

有什么建议吗?

谢谢。

google-drive-api
1个回答
0
投票

同样在我的环境中,无法通过drive.files.update更改mimeType。所以我总是使用这种解决方法。尽管我不确定这对您是否有用,但是如何呢?

使用drive.files.copy

端点如下。

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

请求正文如下。

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

如果这对您没有用,对不起。

添加:

确认:

首先,确认fileId1的mimeType。顺便说一下,作为一个示例情况,该文件是PNG文件。我通过将mimeType更改为application/octet-stream来上传此文件。这用作示例文件。

卷曲命令:
curl \
  'https://www.googleapis.com/drive/v3/files/fileId1 \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed
结果:
{
 "kind": "drive#file",
 "id": "###",
 "name": "samplefile",
 "mimeType": "application/octet-stream"
}
  • 从上面的curl命令发现fileId1的文件具有application/octet-stream的mimeType。

转换mimeType:

卷曲命令:
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
结果:
{
 "kind": "drive#file",
 "id": "fileId2",
 "name": "samplename.json",
 "mimeType": "application/json"
}
  • fileId1的文件运行上述curl命令时,已将mimeType从application/octet-stream更改为application/json

注意:

  • 有3个要点。

    1. OP希望将application/octet-stream的mimeType更改为application/json。这个答案是为了实现这一目标。请注意这一点。
    2. 在这种情况下,当mimeType更改时,数据格式不会更改。因此,请注意这一点。
    3. 所有mimeTypes无法更改。因此,请注意这一点。
  • 例如,当excel文件的mimeType(application/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.ms-excel)可以转换为Google Spreadsheet(application/vnd.google-apps.spreadsheet)时。但是图像文件不能直接转换为Google Spreadsheet。

  • 来自it doesn't work for me. looks like the mimeType property is not working.,我不了解it doesn't work for me. looks like the mimeType property is not working.的情况。因此,如果要将Mahdi Abdi以外的mimeType更改为application/octet-stream以外的mimeType,则可能无法使用此答案。因此,请注意这一点。

© www.soinside.com 2019 - 2024. All rights reserved.