简体   繁体   English

使用 Google Drive REST API v3 和 Angular 2 上传和命名文件

[英]Upload and Name a File Using Google Drive REST API v3 and Angular 2

I'm creating a Google Drive service using the Drive REST Api v3 in Angular 2. Most of the functionality is in place: view file, download, create etc.. but I cannot find how to name a file (either when creating a file or updating).我正在使用 Angular 2 中的 Drive REST Api v3 创建 Google Drive 服务。大部分功能都已到位:查看文件、下载、创建等。但我找不到如何命名文件(无论是在创建文件时或更新)。

I'm using the following docs pages: create and update .我正在使用以下文档页面: createupdate They say the file name should be part of the request body.他们说文件名应该是请求正文的一部分。 The relevant code from my Google Drive service is bellow.我的 Google Drive 服务的相关代码如下。

createFile(name :string, content :string) :Promise<Object> {
    let headers = new Headers({
      'Content-Type': 'text/markdown',
      'Authorization': 'Bearer ' + this.token,
      'name': name //TODO name not working!
    });
    let options = new RequestOptions({ headers: headers }); // Create a request option

    return this.http
      .post('https://www.googleapis.com/upload/drive/v3/files' + '?uploadType=multipart', content, options)
      .toPromise();
  }

updateFile(id :string, content :string, name :string) :Promise<Object> {
    let headers = new Headers({
      'Content-Type': 'text/markdown',
      'Authorization': 'Bearer ' + this.token,
      'id': id,
      'name': name //TODO name not working!
    }); //generate headers
    let options = new RequestOptions({ headers: headers }); // Create a request option

    return this.http
      .patch('https://www.googleapis.com/upload/drive/v3/files/' + id + '?uploadType=multipart', content, options)
      .toPromise();
  }

To summarise files are being created and updated fine (including content) but naming and renaming a file doesn't work at all.总结正在创建和更新的文件(包括内容),但命名和重命名文件根本不起作用。

Thanks for any help.谢谢你的帮助。

I also faced that problem.我也遇到过这个问题。 I think there is 3 solutions:我认为有3个解决方案:

  1. Use multipart upload https://developers.google.com/drive/v3/web/multipart-upload with different headers for file metadata and actual file.使用multipart upload https://developers.google.com/drive/v3/web/multipart-upload为文件元数据和实际文件提供不同的标头。 Me myself stuck there, didn't found how to add boundaries to separate request headers in Angular 2+我自己被困在那里,没有找到如何在 Angular 2+ 中为单独的请求标头添加边界

  2. Upload file in two requests.在两个请求中上传文件。 First to create empty file with metadata (response will provide id of the file) and second to actually "update" the file.首先创建带有元数据的空文件(响应将提供文件的 id),然后实际“更新”文件。

  3. Use resumable upload .使用resumable upload First request to "setup metadata" (will not even create empty file) and get "special link" where to send request to upload actual file.第一个请求“设置元数据”(甚至不会创建空文件)并获取“特殊链接”发送请求以上传实际文件。 And this approach have some other features, like uploading in chunks.这种方法还有一些其他功能,比如分块上传。 https://developers.google.com/drive/v3/web/resumable-upload https://developers.google.com/drive/v3/web/resumable-upload

Here is the link to another Question with implementation of resumable upload in Angular 2 and DRIVE REST API V3这是在 Angular 2 和 DRIVE REST API V3 中实现可恢复上传的另一个问题的链接

Angular 2+ HTTP POST and GDrive API. Angular 2+ HTTP POST 和 GDrive API。 Resumable file upload with name 带名称的可恢复文件上传

I hope it might be useful.我希望它可能有用。

You are trying to set the name using an http header.您正在尝试使用 http 标头设置名称。 This is wrong.这是错误的。 I can't begin to understand how you thought that was the way to do it, so you need to go back and reread the Drive API documentation.我无法开始理解您认为这是如何做到的,因此您需要返回并重新阅读 Drive API 文档。

In short, the name: "name" should be a JSON object passed in the body of the request, not in an http header.简而言之, name: "name"应该是在请求正文中传递的 JSON 对象,而不是在 http 标头中。

Try placing name in the request body and not in the request header as described in the Files: create :尝试将名称放在请求正文中,而不是放在文件中所述的请求标头中 :创建

Request body请求正文

  • In the request body, supply a Files resource with the following properties as the metadata.在请求正文中,提供具有以下属性的 Files 资源作为元数据。 For more information, see the document on media upload .有关更多信息,请参阅有关媒体上传的文档。

To test it, try using API Explorer to help you explore various Google APIs interactively.要对其进行测试,请尝试使用API Explorer来帮助您以交互方式探索各种 Google API。

Sample Request:样品请求:

POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}

{
"name": "My File"
}

Response:回复:

200

{

"kind": "drive#file",
"id": "fileID",
"name": "My File"
}

There is also a related SO post that explain how to insert file to Google Drive through API.还有一个相关的SO 帖子解释了如何通过 API 将文件插入到 Google Drive。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM