如何阅读上传的blob的内容?

时间:2021-07-12 23:13:32

I'm using Blobstore to upload a simple text file using this doc: https://cloud.google.com/appengine/docs/java/blobstore/#Java_Uploading_a_blob . I understand from the docs how to save and serve the blob to users, but I don't understand how can my servlet that handle the file upload actually read the contents of the text file?

我正在使用Blobstore使用此文档上传简单的文本文件:https://cloud.google.com/appengine/docs/java/blobstore/#Java_Uploading_a_blob。我从文档中了解如何为用户保存和提供blob,但我不明白我的servlet如何处理文件上传实际上读取文本文件的内容?

2 个解决方案

#1


I found the answers. This is the code:

我找到了答案。这是代码:

Map<String, List<FileInfo>> infos = blobstoreService.getFileInfos(request);
Long fileSize = infos.get("myFile").get(0).getSize();
Map<String, List<BlobKey>> blobKeys = blobstoreService.getUploads(request);
byte[] fileBytes = 
        blobstoreService.fetchData(blobKeys.get("myFile").get(0), 0, fileSize);
String input = new String(fileBytes);

#2


In python there is the BlobReader class to help you do this. (https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass)

在python中有一个BlobReader类来帮助你做到这一点。 (https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass)

It seems like you are using Java though. There does not seem to be an equivalent class in Java. What I would do is to use GCS as the backing for you blobstore (https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage). This way the files uploaded to the blobstore will be accessibly in GCS.

看起来你似乎在使用Java。在Java中似乎没有相应的类。我要做的是使用GCS作为blobstore的支持(https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage)。这样,上传到blobstore的文件将在GCS中可访问。

You can then read the file using the GCS client library for Java.

然后,您可以使用Java的GCS客户端库来读取该文件。

#1


I found the answers. This is the code:

我找到了答案。这是代码:

Map<String, List<FileInfo>> infos = blobstoreService.getFileInfos(request);
Long fileSize = infos.get("myFile").get(0).getSize();
Map<String, List<BlobKey>> blobKeys = blobstoreService.getUploads(request);
byte[] fileBytes = 
        blobstoreService.fetchData(blobKeys.get("myFile").get(0), 0, fileSize);
String input = new String(fileBytes);

#2


In python there is the BlobReader class to help you do this. (https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass)

在python中有一个BlobReader类来帮助你做到这一点。 (https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass)

It seems like you are using Java though. There does not seem to be an equivalent class in Java. What I would do is to use GCS as the backing for you blobstore (https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage). This way the files uploaded to the blobstore will be accessibly in GCS.

看起来你似乎在使用Java。在Java中似乎没有相应的类。我要做的是使用GCS作为blobstore的支持(https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage)。这样,上传到blobstore的文件将在GCS中可访问。

You can then read the file using the GCS client library for Java.

然后,您可以使用Java的GCS客户端库来读取该文件。