如何在Javascript(Google Drive API)中保存HTTP请求中的URI

How to save URI from HTTP request in Javascript (Google Drive API)

本文关键字:保存 HTTP 请求 URI API Javascript Drive Google      更新时间:2023-09-26

我拼命地试图遵循这个:https://developers.google.com/drive/v3/web/manage-uploads#save-session-uri

我已经能够使用 javascript 将文件上传到我的 Google 云端硬盘,但是我现在正在尝试上传大文件 (>1GB),这会使我当前脚本中的浏览器崩溃。所以我的新脚本使用可恢复上传选项。我已发送可恢复会话启动请求,并收到带有位置标头 URI 的 200 OK 标头。

然后,本教程说"复制并保存会话 URI,以便您可以将其用于后续请求。我一辈子都无法弄清楚如何在 javascript 中做到这一点?如何从标头保存 URI?我这样做的方式是错误的吗?我更习惯于python(我已经设法让可恢复的上传工作),但遗憾的是我们需要在javascript中完成这项工作。我的代码(显然删除了我的客户端ID):

  // Your Client ID can be retrieved from your project in the Google
  // Developer Console, https://console.developers.google.com
  var CLIENT_ID = '<YOURCLIENTID>';
  var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.file'];
  /**
   * Check if current user has authorized this application.
   */
  function checkAuth() {
    gapi.auth.authorize(
      {
        'client_id': CLIENT_ID,
        'scope': SCOPES.join(' '),
        'immediate': true
      }, handleAuthResult);
  }
  /**
   * Handle response from authorization server.
   * @param {Object} authResult Authorization result.
   */
  function handleAuthResult(authResult) {
    var authorizeDiv = document.getElementById('authorize-div');
    if (authResult && !authResult.error) {
      // Hide auth UI, then load client library.
      authorizeDiv.style.display = 'none';
      loadDriveApi();
    } else {
      // Show auth UI, allowing the user to initiate authorization by
      // clicking authorize button.
      authorizeDiv.style.display = 'inline';
    }
  }
  /**
   * Initiate auth flow in response to user clicking authorize button.
   * @param {Event} event Button click event.
   */
  function handleAuthClick(event) {
    gapi.auth.authorize(
      {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
      handleAuthResult);
    return false;
  }
  /**
   * Load Drive API client library.
   * Use last parameter (empty function) to do something on load!
   */
  function loadDriveApi() {
    gapi.client.load('drive', 'v3', function(){});
  }

  function createFile(fileData) {
    var contentType = fileData.type || 'application/octet-stream';
    alert(fileData.name + " " + contentType);
    // Metadata
    var metadata = {
      'name' : fileData.name,
      'mimeType': contentType,
      'parents': [{'id':'0B1c3-viP2d_8QWMyenczTzdzSkk'}]
    };

    var request = gapi.client.request({
        'path' : 'upload/drive/v3/files',
        'method' : 'POST',
        'params' : {'uploadType':'resumable'},
        'headers' : {
          'X-Upload-Content-Type' : contentType,
          'X-Upload-Content-Length' : 1024*256
        },
        'body' : metadata
    });
    request.execute();
  }
  /**
   * Append a pre element to the body containing the given message as its text node.
   * @param {string} message Text to be placed in pre element.
   */
  function appendPre(message) {
    var pre = document.getElementById('output');
    var textContent = document.createTextNode(message + ''n');
    pre.appendChild(textContent);
  }

  //files is a filelist
  function fileselected(files) 
  {
      for(var i = 0; i < files.length; i++) // for file in list of files
      {
          var f = files[i]; // Pick the current file (i) from the file list
          appendPre("Uploading " + f.name);
          createFile(f);
          appendPre(f.name + " upload complete"); // Upload the current file
      }
  }

正如 OP 最初在编辑问题时所说的那样,您需要像这样使用 request.execute() 的回调:

request.execute(function(resp, raw_resp) {
    appendPre(raw_resp);
    // GETS THE LOCATION! WOO
    appendPre(JSON.parse(raw_resp).gapiRequest.data.headers.location);
});