LoginSignup
8
8

More than 5 years have passed since last update.

GCSのFileをImageServiceで扱う

Last updated at Posted at 2013-11-17

GAEには画像を編集することができるImageServiceがあります。
そのImageServiceでGCSのFileを扱うやり方の記事です。

GCSの操作などについては、以下を参照してください。

ImageServiceを利用した画像のURLを発行

GCSにあるファイルをImageServiceで扱うためには、ServingUrlOptionsにwithGoogleStorageFileNameを指定します。
その時に、以下のようにGCSのFilePathを指定します。

/gs/{{your gcs bucket name}}/{{file name}}
public class ImageController extends Controller {

    @Override
    protected Navigation run() throws Exception {
        // "/gs/{{your gcs bucket name}}/{{file name}}"
        final String fileName = asString("fileName");

        ImagesService imagesService = ImagesServiceFactory.getImagesService();
        ServingUrlOptions options = ServingUrlOptions.Builder.withGoogleStorageFileName(fileName);
        String imageUrl = imagesService.getServingUrl(options);
        response.getWriter().write(imageUrl);

        return null;
    }
}

ImageServiceを利用した場合の注意点

キャッシュ

ImageService.getServingUrl()を利用した場合、Serverにキャッシュがあるような動きをします。
例えば、GCSのファイルを差し替えても、丸一日ぐらいは更新されません。
キャッシュを削除する術もないので、すぐにファイルを更新したい場合は、GCSにファイルを新しく保存して、再度ImageSErvice.getServingUrl()を実行し、URLごと差し替えてしまう必要があります。

GCSのファイルへの参照はあるようで、ImageServiceを利用してResizeなどを行っている場合は、キャッシュが効かずに、最新のファイルをResizeして返してきます。

ImageServiceを使うかどうか

Blobstoreに保存したファイルを、ImageSerivice.getServingUrl()で読み込んだ場合、課金対象になっていなかったので、お金を節約したい時に便利でした。
GCSを利用した場合、どうなるのかは試してないので、今のところ謎です。

もし、そこに課金が発生するなら、Resizeなどをしない限り、ImageServiceを使う利点は無いのかもしれません。
GCSのファイルを直接読んだ方が良いですね。
キャッシュがあるので、たぶん課金は無いんじゃないかなとは思っているのだけど。

Github

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8