2013年1月27日日曜日

Google Cloud Storageのドキュメント - Java API概要 その11

Google Cloud StorageのJava APIの概要についての
ドキュメントを翻訳しています。

翻訳元はこちらです。

このドキュメントは、Google App Engineアプリケーションから
Google Cloud Storageの機能を呼び出す方法について
説明しています。

現在は試験的運用中なので、今後変更があるかもしれません。

第1回はこちら、第2回はこちら、第3回はこちら
第4回はこちら、第5回はこちら、第6回はこちら
第7回はこちら、第8回はこちら、第9回はこちら
第10回はこちらです。

Complete Sample App - サンプルアプリケーション

以下は、App EngineアプリケーションでCloud Storage APIを使用するデモンストレーションであるサンプルアプリケーションです。アプリケーションを実行するには、バケットに対してWRITEアクセスを持つか、オーナーでなければなりません。 このアプリケーションをセットアップするには:
  • Google Plugin for EclipseページのCreating a projectセクション以下の指示に従って、プラグインのインストールとセットアップを行います。
  • あなたのGuestbookServlet.javaファイルの先頭に以下のインポートをコピーします。
    import com.google.appengine.api.files.AppEngineFile;
    import com.google.appengine.api.files.FileReadChannel;
    import com.google.appengine.api.files.FileService;
    import com.google.appengine.api.files.FileServiceFactory;
    import com.google.appengine.api.files.FileWriteChannel;
    import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.ByteBuffer;
    import java.nio.channels.Channels;
    
    import javax.servlet.http.*;
    

インストール不要・無料のKaede翻訳ツール:
http://kaedetrans-hrd.appspot.com/

2013年1月18日金曜日

Google Cloud Storageのドキュメント - Java API概要 その10

Google Cloud StorageのJava APIの概要についての
ドキュメントを翻訳しています。

翻訳元はこちらです。

このドキュメントは、Google App Engineアプリケーションから
Google Cloud Storageの機能を呼び出す方法について
説明しています。

現在は試験的運用中なので、今後変更があるかもしれません。

第1回はこちら、第2回はこちら、第3回はこちら
第4回はこちら、第5回はこちら、第6回はこちら
第7回はこちら、第8回はこちら、第9回はこちらです。

Reading the Object - オブジェクトの読み出し

オブジェクトを読み出す前に、オブジェクトのファイナライズを行わなければなりません。 オブジェクトを読み出すには:
boolean lockForRead = false;
String filename = "/gs/my_bucket/my_object";
AppEngineFile readableFile = new AppEngineFile(filename);
FileReadChannel readChannel = fileService.openReadChannel(readableFile, lockForRead);

// Read the file in whichever way you'd like
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
String line = reader.readLine();
resp.getWriter().println("READ:" + line);

readChannel.close();
一つのAPIコールでアプリケーションによって読み出されるオブジェクトの最大サイズは、32メガバイトです。

インストール不要・無料のKaede翻訳ツール:
http://kaedetrans-hrd.appspot.com/

2013年1月10日木曜日

Google Cloud Storageのドキュメント - Java API概要 その9

Google Cloud StorageのJava APIの概要についての
ドキュメントを翻訳しています。

翻訳元はこちらです。

このドキュメントは、Google App Engineアプリケーションから
Google Cloud Storageの機能を呼び出す方法について
説明しています。

現在は試験的運用中なので、今後変更があるかもしれません。

第1回はこちら、第2回はこちら、第3回はこちら
第4回はこちら、第5回はこちら、第6回はこちら
第7回はこちら、第8回はこちらです。

Finalizing the Object - オブジェクトのファイナライズ

オブジェクトへの書き込みが終了した後、読み出しを行う前にファイナライズを行う必要があります:
// Finalize the object
writeChannel.closeFinally();
ここまでで、/gs/my_bucket/my_objectのファイル名でApp Engineからオブジェクトが可視状態となります。パブリックアクセス可でオブジェクトを設定している場合、http://storage.googleapis.com/my_bucket/my_objectのURLを使ってアクセスすることができます。 一度オブジェクトをファイナライズすると、後から書き込みを行うことはできなくなります。ファイルの内容の変更を行いたい場合は、上書きするために、同じ名前で新しいオブジェクトの生成を行う必要があるでしょう。

インストール不要・無料のKaede翻訳ツール:
http://kaedetrans-hrd.appspot.com/

2013年1月4日金曜日

Google Cloud Storageのドキュメント - Java API概要 その8

Google Cloud StorageのJava APIの概要についての
ドキュメントを翻訳しています。

翻訳元はこちらです。

このドキュメントは、Google App Engineアプリケーションから
Google Cloud Storageの機能を呼び出す方法について
説明しています。

現在は試験的運用中なので、今後変更があるかもしれません。

第1回はこちら、第2回はこちら、第3回はこちら
第4回はこちら、第5回はこちら、第6回はこちら
第7回はこちらです。

Opening and Writing to the Object - オブジェクトのオープン及び書き込み

オブジェクトに書き込むためには、書き込みチャネルをオープンしなければなりません:
// Open a channel for writing
boolean lockForWrite = false; // Do you want to exclusively lock this object?
FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile, lockForWrite);

次に、標準的なJavaの方法を使ってオブジェクトに書き込むことができます:
// For this example, we write to the object using the PrintWriter
PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
out.println("The woods are lovely and deep.");
out.println("But I have promises too keep.");

// Close the object without finalizing.
out.close();

/**
 * You can write to this file again, later, by saving the file path.
 * It is not possible to read the file until you finalize it.
 * Once you finalize a file, it is not possible to write to it.
 */

// Save the file path
String path = writableFile.getFullPath();

// Lock the file so no one else can access it at the same time
lockForWrite = true;

// Write to the unfinalized file again in a separate request
writableFile = new AppEngineFile(path);
writeChannel = fileService.openWriteChannel(writableFile, lockForWrite);
writeChannel.write(ByteBuffer.wrap("And miles to go before I sleep.".getBytes()));

インストール不要・無料のKaede翻訳ツール:
http://kaedetrans-hrd.appspot.com/