服务器端上传到 Blob 存储 - Java Google App Engine

Server side upload to Blobstore - Java Google Appengine

我在服务器 (gae) 中创建数据,我想将其存储在 Blobstore 中。我看到很多关于如何向客户端提供 BlobStore URL 的答案,但是没有客户端或 HTTP 请求:它只是一个异步任务。

  1. 然后我想我应该使用 createUploadUrl(),而不是将此 URL 提供给客户端,从我的代码 HTTP Post 我的数据通过 URL 获取。这看起来很奇怪,难道没有另一个 API 吗?

  2. 假设我在 Blobstore 中需要的文件已经存储在我的 GCS 默认存储桶中。我可以使用 gcs 位置“/gs/bucketname/file”将它们告知 Blobstore 吗?我试过


GcsFilename filename = new GcsFilename(bucketName, fileId);
String gcsKey = "/gs/" + bucketName + "/" + filename.getObjectName();
BlobKey blobKey = blobstoreService.createGsBlobKey(gcsKey);
GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, GcsFileOptions.getDefaultInstance());
ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(outputChannel));
oout.writeObject(myDataObjectToPersist);
oout.close();

// ...at some other point I have checked the file is correctly stored in
// GCS and I can fetch it using /gs/bucket/fileId
// but it doesn't seem to be in Blobstore, so when
InputStream stream = new BlobstoreInputStream(new BlobKey(blobKey.keyString))
// ... this gives a BlobstoreInputStream$BlobstoreIOException: BlobstoreInputStream received an invalid blob key...

这在概念上是错误的吗?例如,如果我使用 GcsOutputChannel 来保存它,即使我创建了 BlobKey,我也不会从 Blobstore 获取它,或者它是否可以工作但我只是做错了什么?

1K 谢谢

为什么要将文件存储在 blobstore 中,而不是直接从 GCS 写入和读取文件?

是的,您可以为存储在 GCS 中的文件创建一个 BlobKey,并且可以在某些 blobstore API(例如 fetchData and serve)中使用该密钥,但不幸的是,不能在所有的中使用。 某些 blobstore API(例如 BlobstoreInputStream)依赖于 BlobInfo,并且在使用 GCS 客户端时不会创建。