【发布时间】:2016-06-17 02:34:32
【问题描述】:
之前,我问了一个问题https://stackoverflow.com/questions/35581090/can-i-use-resumable-upload-for-gae-blobstore-api 关于使用 Blobstire API 进行可恢复上传。 就我自己而言,我认为使用 Blobstire API 实现可恢复上传是不可能的。 在这种情况下,我正在尝试使用 Java 客户端库实现 Google Cloud Storage。目前,我将我的视频文件下载到存储桶并提供视频。我的 servlet 看起来像 google 示例中的样子
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
GcsOutputChannel outputChannel =
gcsService.createOrReplace(getFileName(req), GcsFileOptions.getDefaultInstance());
copy(req.getInputStream(), Channels.newOutputStream(outputChannel));
}
private GcsFilename getFileName(HttpServletRequest req) {
String[] splits = req.getRequestURI().split("/", 4);
if (!splits[0].equals("") || !splits[1].equals("gcs")) {
throw new IllegalArgumentException("The URL is not formed as expected. " +
"Expecting /gcs/<bucket>/<object>");
}
return new GcsFilename(splits[2], splits[3]);
}
private void copy(InputStream input, OutputStream output) throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
} finally {
input.close();
output.close();
}
}
现在我需要实现
- 可恢复上传(由于移动设备上的网络不畅)
- 由chunck上传(由于一个请求的大小限制为32mb)
我意识到,应该手动组织可恢复上传的服务器端,并且我的后端应该能够为我提供上传块的范围并允许继续引导到 OutputChannel。
GcsOutputChannel 的文档说:
这个类是可序列化的,这允许写入文件的一部分, 序列化 GcsOutputChannel 反序列化它,并继续 写入同一个文件。序列化实例的时间 有效由 Google Cloud Storage 服务限制和确定
我没有足够的经验,所以这个问题可能很愚蠢: 请有人告诉我如何序列化我的 GcsOutputChannel?我不明白我可以将包含序列化对象的文件保存在哪里。
顺便问一下,谁能知道谷歌云存储服务存储这个序列化对象多久?
【问题讨论】:
标签: java google-app-engine google-api google-cloud-storage