使用云端硬盘v2 API将CSV上传到Google云端硬盘电子表格

Zuz*_*uza 23 csv google-sheets google-drive-api

如何使用Drive API v2将本地CSV文件上传到Google云端硬盘,以便上传的文件采用原生Google电子表格格式.最好是在Python中,但原始的HTTP请求就足够了.

我尝试了什么:

  1. 请求正文内容类型:'application/vnd.google-apps.spreadsheet',media_body content-type:'text/csv'. - > 401 Bad Request

  2. 请求正文内容类型:'application/vnd.google-apps.spreadsheet',media_body content-type:'application/vnd.google-apps.spreadsheet'. - > 400 Bad Request

  3. ...(其他几个例如留下一个属性和类似的,通常有400或驱动器不认识它作为本机电子表格)

Cla*_*ino 24

您的插入请求应指定text/csv为内容类型.获取文件转换的技巧是将?convert=true查询参数添加到请求URL:

https://developers.google.com/drive/v2/reference/files/insert

  • 对于API`convert`的v3不再有效.而是将上传的MIME类型指定为"text/csv",并将所需文件的MIME类型指定为"application/vnd.google-apps.spreadsheet". (3认同)

wes*_*cpy 10

(2017年3月)请注意,虽然问题专门询问Drive API v2,但开发人员应该知道Google Drive API团队在2015年底发布了v3,并在该版本中insert()更改了名称,create()以便更好地反映文件操作.也没有更多转换标志 - 你只需指定MIME类型......想象一下!

文档也得到了改进:现在有一个专门用于上传(简单,多部分和可恢复)的特别指南,其中包含Java,Python,PHP,C#/ .NET,Ruby,JavaScript/Node.js和iOS中的示例代码./Obj-C上传文件,另一个文件导入CSV文件作为Google表格.

只是为了直截了当,下面是一个备用Python解决方案(对于文档中的示例),用于短文件("简单上载"),您不需要apiclient.http.MediaFileUpload该类.此代码段假设您的身份验证代码适用于服务端点DRIVE的最小身份验证范围https://www.googleapis.com/auth/drive.file.

# filenames & MIMEtypes
DST_FILENAME = 'inventory'
SRC_FILENAME = DST_FILENAME + '.csv'
SHT_MIMETYPE = 'application/vnd.google-apps.spreadsheet'
CSV_MIMETYPE = 'text/csv'

# Import CSV file to Google Drive as a Google Sheets file
METADATA = {'name': DST_FILENAME, 'mimeType': SHT_MIMETYPE}
rsp = DRIVE.files().create(body=METADATA, media_body=SRC_FILENAME).execute()
if rsp:
    print('Imported %r to %r (as %s)' % (SRC_FILENAME, DST_FILENAME, rsp['mimeType']))
Run Code Online (Sandbox Code Playgroud)


小智 7

Claudio Cherubino的回答是正确的 - 您必须手动添加参数.既然你用Python问过,这是一个具体的例子:

body = {
    'mimeType':'text/csv',
    'title': 'title'
}

# service: your authenticated service
# media: your apiclient.http.MediaFileUpload object, with 'text/csv' mimeType
req = service.files().insert(media_body=media, body=body)

# patch the uri to ensure conversion, as the documented kwarg seems to be borked.
# you may need to use '?convert=true' depending on the uri, not taking that into
# account here for sake of simplicity.
req.uri = req.uri + '&convert=true'

# now we can execute the response.
resp = req.execute()

# should be OK
assert resp['mimeType'] == u'application/vnd.google-apps.spreadsheet'
Run Code Online (Sandbox Code Playgroud)

  • 你不必手动添加参数,只需将其用作常规参数:`insert(media_body = media,body = body,convert = True)` (2认同)

小智 7

Java:

    //Insert a file  
    File body = new File();
    body.setTitle("CSV");
    body.setDescription("A test document");
    body.setMimeType("text/csv");

    java.io.File fileContent = new java.io.File("document.csv");
    FileContent mediaContent = new FileContent("text/csv", fileContent);

    Insert insert = service.files().insert(body, mediaContent);
    insert.setConvert(true);
    File file = insert.execute();
    System.out.println("File ID: " + file.getId());
Run Code Online (Sandbox Code Playgroud)