java - Drive Rest API V3 中的可恢复上传

标签 java android google-drive-api

我正在尝试在 Android 中使用 Drive Rest API 创建一个可恢复的上传 session 。

根据文档,需要遵循的 3 个步骤是

  1. 开始可续会
  2. 保存可恢复 session URI
  3. 上传文件

第 1 步:我使用以下代码启动可恢复 session 。

File body = new File();
body.setName(fileName);
body.setMimeType(mimeType);
body.setCreatedTime(modifiedDate);
body.setModifiedTime(modifiedDate);
body.setParents(Collections.singletonList(parentId));

HttpHeaders header = new HttpHeaders();
header.setContentLength(0L);
header.setContentType("application/json; charset=UTF-8");
header.set("X-Upload-Content-Type","image/jpeg");

HttpResponse response= driveObject
                     .files()
                     .create(body)
                     .setRequestHeaders(header)
                     .set("uploadType","resumable")
                     .buildHttpRequest()
                     .execute();

第 2 步:执行完成后,我将打印请求的响应 header 以查看 Location URI

System.out.println(response.getHeader().toString());

输出如下

{
    cache-control=[no-cache, no-store, max-age=0, must-revalidate], 
    content-encoding=[gzip], 
    content-type=[application/json; charset=UTF-8], 
    date=[Thu, 06 Oct 2016 02:20:18 GMT], 
    expires=[Mon, 01 Jan 1990 00:00:00 GMT], 
    alt-svc=[quic=":443"; ma=2592000; v="36,35,34,33,32"], 
    pragma=[no-cache], 
    server=[GSE], 
    transfer-encoding=[chunked], 
    vary=[Origin, X-Origin], 
    x-android-received-millis=[1475720421761], 
    x-android-response-source=[NETWORK 200], 
    x-android-sent-millis=[1475720420804], 
    x-content-type-options=[nosniff], 
    x-frame-options=[SAMEORIGIN], 
    x-xss-protection=[1; mode=block]
}

我没有在响应 header 中找到 Location URI 来开始上传文档中指定的文件数据,也没有找到任何 Java 示例来执行可恢复上传。

如何检索文档中指定的位置 URI?

最佳答案

我已经尝试了一周的大部分时间,终于可以运行可续传上传。它没有像我预期的那样工作,但它确实有效。

不要对一切都使用 Drive REST API

据我所知,据我所知,Google Drive REST API 并不能真正进行分块上传。这可能是一个错误,也可能是设计使然。也可能是我太笨了。

但让我想到的是,我在任何地方都看不到代码示例。每个人一直都在谈论 Http header 。这就是我们下面要做的。我们将仅使用 header 。

下面是如何使用 Google Drive REST API 和 Android 进行可恢复的分块上传:

0) 初始化

String accountName = "account_name";
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Arrays.asList(SCOPES)).setBackOff(new ExponentialBackOff()).setSelectedAccountName(accountName);

1) 启动可恢复 session

遵循 Google 在 this document 中概述的规则:

POST /upload/drive/v3/files?uploadType=resumable HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
  "name": "My File"
}

像 Google 的示例一样设置所有标题字段。将其作为 POST 请求发送。使用您的 credential 变量获取授权 token 。 X-Upload-Content-Type 的 mime 类型并不是那么重要,没有它它也能工作(this SO answer 提供了一个很好的函数来从路径中检索它)。将 X-Upload-Content-Length 设置为文件的总长度。将 Content-Type 设置为 JSON 格式,因为我们的正文将以 JSON 格式为 Google 提供元数据。

现在创建您的元数据主体。我输入了文件名和父级。将 Content-Length 设置为 body 的字节长度。然后将您的主体写入 request.getOutputStream() 输出流。

URL url = new URL("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestMethod("POST");
request.setDoInput(true);
request.setDoOutput(true);
request.setRequestProperty("Authorization", "Bearer " + credential.getToken());
request.setRequestProperty("X-Upload-Content-Type", getMimeType(file.getPath()));
request.setRequestProperty("X-Upload-Content-Length", String.format(Locale.ENGLISH, "%d", file.length()));
request.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
String body = "{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + parentId + "\"]}";
request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body.getBytes().length));
OutputStream outputStream = request.getOutputStream();
outputStream.write(body.getBytes());
outputStream.close();
request.connect();

2) 保存可恢复 session URI

最后,connect() 并等待响应。如果响应代码为 200,则您已成功启动分块可续传上传。现在将 location header URI 保存在某处(数据库、文本文件等)。您稍后会需要它。

if (request.getResponseCode() == HttpURLConnection.HTTP_OK) {
    String sessionUri = request.getHeaderField("location");
}

3) 上传文件

PUT {session_uri} HTTP/1.1
Host: www.googleapis.com
Content-Length: 524288
Content-Type: image/jpeg
Content-Range: bytes 0-524287/2000000

bytes 0-524288

将以下代码循环,直到上传整个文件。在每个 block 之后,您将收到代码为 308range header 的响应。从这个 range header ,您可以读取下一个 block 开始(请参阅 (4))。

Content-Type 将再次成为 mime 类型。 Content-Length 是您在此 block 中上传的字节数。 Content-Range 需要采用 bytes startByte-EndByte/BytesTotal 形式。您将其放入 PUT 请求中。

然后您创建一个 FileInputStream 并将位置设置为您的起始字节(您从上次响应 range header 中获得)并将另一个 block 读入您的缓冲区。然后将该缓冲区写入连接输出流。最后,connect()

URL url = new URL(sessionUri);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestMethod("PUT");
request.setDoOutput(true);
request.setConnectTimeout(10000);
request.setRequestProperty("Content-Type", getMimeType(file.getPath()));
long uploadedBytes = chunkSizeInMb * 1024 * 1024;
if (chunkStart + uploadedBytes > file.length()) {
    uploadedBytes = (int) file.length() - chunkStart;
}
request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", uploadedBytes));
request.setRequestProperty("Content-Range", "bytes " + chunkStart + "-" + (chunkStart + uploadedBytes - 1) + "/" + file.length());
byte[] buffer = new byte[(int) uploadedBytes];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.getChannel().position(chunkStart);
if (fileInputStream.read(buffer, 0, (int) uploadedBytes) == -1) { /* break, return, exit*/ }
fileInputStream.close();
OutputStream outputStream = request.getOutputStream();
outputStream.write(buffer);
outputStream.close();
request.connect();

4) 处理响应

在此之后,您将收到代码为 308 的响应(如果成功)。此响应包含一个 range header (已提及)。

HTTP/1.1 308 Resume Incomplete
Content-Length: 0
Range: bytes=0-524287

您将其拆分并获得新的 block 起始字节。

 String range = chunkUploadConnection.getHeaderField("range");
    int chunkPosition = Long.parseLong(range.substring(range.lastIndexOf("-") + 1, range.length())) + 1;

5)响应码不是308?!

您可能会收到 5xx 响应。您的互联网连接可能会失败,文件可能会在上传过程中被删除/重命名等。 不用担心。只要保存 session URI 和 block 起始字节,就可以随时恢复上传。

为此,请发送以下形式的 header :

PUT {session_uri} HTTP/1.1
Content-Length: 0
Content-Range: bytes */TotalFileLength


URL url = new URL(sessionUri);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestMethod("PUT");
request.setDoOutput(true);
request.setConnectTimeout(10000);
request.setRequestProperty("Content-Length", "0");
request.setRequestProperty("Content-Range", "bytes */" + file.length());
request.connect();

然后您将收到带有 range header 的 308,您可以从中读取最后上传的字节(就像我们在上面所做的那样)。取这个数字,重新开始循环。

我希望我能帮助到你们中的一些人。如果您还有其他问题,请在评论中提问,我会编辑答案。

关于java - Drive Rest API V3 中的可恢复上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39887303/

相关文章:

php - 如何在 PHP 中阅读 Google Drive 电子表格?

java - 禁止从 Nashorn 导入 Java

java - 比较java中的顶点对

android - 从另一个 Activity 移回上一个 Activity,不知道 Activity 在堆栈中的位置

javascript - 我可以使用服务帐户对我网站上的用户进行身份验证,使他们能够查看我的 Google 云端硬盘中的选定文件吗?

python - unauthorized_client : Client is unauthorized to retrieve access tokens using this method

java - MacOS Mojave 无法启动 jar 文件

java - 将自定义对象传递给 Arrays.asList

android - 下载我的应用程序时出现错误 [DF-AA-33]

android - AltBeacon:Android 信标库。如何定义Eddystone的区域?