从其BlobKey获取Google云端存储文件 [英] Get a Google Cloud Storage file from its BlobKey

查看:130
本文介绍了从其BlobKey获取Google云端存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Google App Engine应用程序,它利用Blobstore来保存程序化生成的数据。为此,我使用了文件API ,但不幸的是,该文件已弃用于Google云储存。所以我正在重写我的帮助类以使用GCS。



我想尽可能保持接口的类似性,因为我坚持使用BlobKeys在数据存储中保留对文件的引用(并且更改生产应用程序的模型总是很痛苦)。当我将某些东西保存到GCS中时,我检索了一个BlobKey,其中包含

$ p $ BlobKey blobKey = blobstoreService.createGsBlobKey(/ gs /+ fileName .getBucketName()+/+ fileName.getObjectName());

按规定这里,我坚持它在数据存储。



所以这里的问题是:文档告诉我如何服务在servlet响应中使用 blobstoreService.serve(blobKey,resp); 的GCS文件,但是如何检索文件内容(如InputStream,byte array或其他)以使用它在我的代码进一步处理?在我目前的实现中,我使用从 AppEngineFile (不赞成使用)读取的 FileReadChannel 来读取。

$给定一个 blobKey ,使用 BlobstoreInputStream class to请按照文档中的说明从Blobstore中读取值:

  BlobstoreInputStream in = new BlobstoreInputStream(blobKey); 


I wrote a Google App Engine application that makes use of Blobstore to save programmatically-generated data. To do so, I used the Files API, which unfortunately has been deprecated in favor to Google Cloud Storage. So I'm rewriting my helper class to work with GCS.

I'd like to keep the interface as similar as possible as it was before, also because I persist BlobKeys in the Datastore to keep references to the files (and changing the model of a production application is always painful). When i save something to GCS, i retrieve a BlobKey with

BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName());

as prescribed here, and I persist it in the Datastore.

So here's the question: the documentation tells me how to serve a GCS file with blobstoreService.serve(blobKey, resp); in a servlet response, BUT how can I retrieve the file content (as InputStream, byte array or whatever) to use it in my code for further processing? In my current implementation I do that with a FileReadChannel reading from an AppEngineFile (both deprecated).

解决方案

Given a blobKey, use the BlobstoreInputStream class to read the value from Blobstore, as described in the documentation:

BlobstoreInputStream in = new BlobstoreInputStream(blobKey); 

这篇关于从其BlobKey获取Google云端存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆