【问题标题】:Server side upload to Blobstore - Java Google Appengine服务器端上传到 Blob 存储 - Java Google App Engine
【发布时间】:2015-11-04 01:01:52
【问题描述】:

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

  1. 然后我想我应该使用 createUploadUrl(),而不是将此 URL 提供给客户端,而是从我的代码 HTTP 通过 URL Fetch 将我的数据发布到它。这看起来很奇怪,难道没有其他 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 谢谢

【问题讨论】:

    标签: java google-app-engine google-cloud-storage blobstore


    【解决方案1】:

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

    是的,您可以为存储在 GCS 中的文件创建 BlobKey,并且可以在某些 blobstore API 中使用该密钥(例如 fetchDataserve),但遗憾的是并非全部使用。 一些 blobstore API(例如BlobstoreInputStream)依赖于 BlobInfo,并且在使用 GCS 客户端时不会创建。

    【讨论】:

    • 人们想要的原因有很多。这就像问 Blobstore 有什么好处,对吧? :) 但是如果你说我可以为 GCS 中的任何文件创建一个下载 URL,那么这会减少一个好处。就我而言,这也是要求和遗留原因。我认为 fetchData 会给我我想要的,谢谢。关于我的问题 1,我想您确认我唯一能做的就是来自我的服务器应用程序的 HTTP Post。
    • 是的,没有办法直接写入blobstore。用于执行此操作的 Files api 现在已弃用 (cloud.google.com/appengine/docs/java/googlecloudstorageclient/…)。
    猜你喜欢
    • 2016-12-21
    • 2015-03-18
    • 2011-07-23
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多