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

时间:2022-06-20 15:24:29

How can I upload a local CSV file to Google Drive using the Drive API v2 so that the uploaded file is in the native Google Spreadsheet format. Preferably in Python, but a raw HTTP request will suffice.

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

What I tried:

我尝试了什么:

  1. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'text/csv'. --> 401 Bad Request

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

  2. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'application/vnd.google-apps.spreadsheet'. --> 400 Bad Request

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

  3. ... (a couple of others such as leaving a property out and similar, usually got 400 or Drive didn't recognise it as a native spreadsheet)

    ...(其他一些例如留下房产和类似房产,通常有400或驱动器没有将其识别为原生电子表格)

5 个解决方案

#1


23  

Your insert request should specify text/csv as the content-type. The trick to get the file converted is to add the ?convert=true query parameter to the request url:

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

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

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

#2


9  

(Mar 2017) Note, while the question specifically asks about Drive API v2, developers should know that the Google Drive API team released v3 at the end of 2015, and in that release, insert() changed names to create() so as to better reflect the file operation. There's also no more convert flag -- you just specify MIMEtypes... imagine that!

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

The documentation has also been improved: there's now a special guide devoted to uploads (simple, multipart, and resumable) that comes with sample code in Java, Python, PHP, C#/.NET, Ruby, JavaScript/Node.js, and iOS/Obj-C to upload a file and another that imports a CSV file as a Google Sheet.

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

Just to show up straightforward it is, below is one alternate Python solution (to the sample in the docs) for short files ("simple upload") where you don't need the apiclient.http.MediaFileUpload class. This snippet assumes your auth code works where your service endpoint is DRIVE with a minimum auth scope of https://www.googleapis.com/auth/drive.file.

只是为了直截了当,下面是一个替代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']))

#3


7  

Claudio Cherubino's answer is correct -- you have to add the parameter manually. Since you asked in Python though, here's a concrete example:

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'

#4


7  

Java :

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());

#5


0  

The best way to get started is using the web form at https://developers.google.com/drive/v2/reference/files/insert#try-it

入门的最佳方式是使用https://developers.google.com/drive/v2/reference/files/insert#try-it上的网络表单

#1


23  

Your insert request should specify text/csv as the content-type. The trick to get the file converted is to add the ?convert=true query parameter to the request url:

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

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

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

#2


9  

(Mar 2017) Note, while the question specifically asks about Drive API v2, developers should know that the Google Drive API team released v3 at the end of 2015, and in that release, insert() changed names to create() so as to better reflect the file operation. There's also no more convert flag -- you just specify MIMEtypes... imagine that!

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

The documentation has also been improved: there's now a special guide devoted to uploads (simple, multipart, and resumable) that comes with sample code in Java, Python, PHP, C#/.NET, Ruby, JavaScript/Node.js, and iOS/Obj-C to upload a file and another that imports a CSV file as a Google Sheet.

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

Just to show up straightforward it is, below is one alternate Python solution (to the sample in the docs) for short files ("simple upload") where you don't need the apiclient.http.MediaFileUpload class. This snippet assumes your auth code works where your service endpoint is DRIVE with a minimum auth scope of https://www.googleapis.com/auth/drive.file.

只是为了直截了当,下面是一个替代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']))

#3


7  

Claudio Cherubino's answer is correct -- you have to add the parameter manually. Since you asked in Python though, here's a concrete example:

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'

#4


7  

Java :

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());

#5


0  

The best way to get started is using the web form at https://developers.google.com/drive/v2/reference/files/insert#try-it

入门的最佳方式是使用https://developers.google.com/drive/v2/reference/files/insert#try-it上的网络表单