Google App Engine/Java(+ slim3)を使って、Webアプリケーションによくあるいろんな機能を実現するために色々と試行錯誤したことについてのお話。
「GAEだと、この機能はこうやればできるんだ!」ということを少しでも知ってもらって、一人でも多くの方がGAEで何かを作ってもらえるようになればな~、と思っています!
40. 9.「blobstore」って?
・blob=Binary Large OBject
The Blobstore API allows your application to serve data objects, called blobs, that are much
larger than the size allowed for objects in the Datastore service. Blobs are useful for
serving large files, such as video or image files, and for allowing users to upload large data
files. Blobs are created by uploading a file through an HTTP request. Typically, your
applications will do this by presenting a form with a file upload field to the user. When the
form is submitted, the Blobstore creates a blob from the file's contents and returns an opaque
reference to the blob, called a blob key, which you can later use to serve the blob. The
application can serve the complete blob value in response to a user request, or it can read
the value directly using a streaming file-like interface.
Google Developers (https://developers.google.com/appengine/docs/java/blobstore/overview?hl=en)
・要は「でっかいバイナリデータの記録専用のdatastore」と思えばおk
・アップロードするとBlobKeyが得られる。BlobKeyをキーにblobを取得することが可能
・アップロードしたblobを変更することはできない。削除は可能
58. 16.blobstoreにblobを登録する(1)
・使う要素としては、
「Writing Files to the Blobstore (Experimental)」を活用する。
(https://developers.google.com/appengine/docs/java/blobstore/overview?
hl=en#Writing_Files_to_the_Blobstore )
・書いてあるとおり、「Experimental」なので、
実装方法が変わる・いきなり使えなくなるなどのリスクはあるのでご注意
59. 16.blobstoreにblobを登録する(2)
・まずblobstoreへの登録部分。今までの名残でzip圧縮もしてます
public static BlobKey registBlob(Object o) throws IOException{
//データをバイト配列に変換(圧縮も実施)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zip_os = new ZipOutputStream(baos);
zip_os.putNextEntry(new ZipEntry("zipped_entry"));
ObjectOutputStream oos = new ObjectOutputStream(zip_os);
oos.reset();
oos.writeObject(o);
oos.flush();
zip_os.closeEntry();
zip_os.close();
baos.close();
byte[] bytes = baos.toByteArray();
・・・
60. 16.blobstoreにblobを登録する(3)
・・・
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "application/octet-stream"
AppEngineFile file = fileService.createNewBlobFile("application/octet-stream");
// Open a channel to write to it
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
// This time we write to the channel using standard Java
writeChannel.write(ByteBuffer.wrap(bytes));
// Now finalize
writeChannel.closeFinally();
return fileService.getBlobKey(file);
}
・mime-typeを"application/octet-stream"にしてやることにより、
渡されたオブジェクトをバイト配列にしたもの(+zip圧縮)を、
blobstoreにバイナリデータファイルとして書き出せています。と思います。
・書き込みが成功するとBlobKeyが得られるのは、画像のアップロードのときと同じ
61. 16.blobstoreにblobを登録する(4)
・続いて、blobstoreに書きだしたバイナリデータ(ファイル)を読み込む方法です
InputStream is = new BlobstoreInputStream(blobkey);
ZipInputStream zip_in = new ZipInputStream(is);
zip_in.getNextEntry();
ObjectInputStream ois = new ObjectInputStream(zip_in);
HashMap<String, Integer> readObject = (HashMap<String, Integer>) ois.readObject();
・これで、一手間はかかるけれど、巨大なデータを扱うWebアプリケーションの開発も、gae
で可能!
65. 18.課金体系について(2)
・一口に「チューニング」「設定」といっても、高度なものから非常に簡単なものからまで。
まずは「appengine ja night #18で分かった、Google App Engineの
課金の仕組み、節約術・自分用まとめ」(http://d.hatena.ne.jp/a-know/20111126)を見て、
「今すぐにでもできるチューニング・設定」を知りましょう!
・最新のガイドも出版されました!ぼくも買いました!
「今のGAE」を、いっしょに勉強しましょう!
Google API Expertが解説する
Google App Engine for Java実践ガイド
(インプレスジャパン・小川 信一(@shin1ogawa) 著)
・てか、課金に悩まされるくらいのHitアプリを作ってみたい
もんです!(魂の叫び)