s3fsよりも高速なgoofysが待望のContent-Type自動設定をサポート

2016.06.03

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

西澤です。S3バケットを直接マウントして高速に使えるgoofysが待望のContent-Typeの自動設定をサポートしたとの情報を見つけたので、早速試してみることにしました。

Content-Typeとは

S3にオブジェクトを配置する際には、システムメタデータを設定することができます。S3のウェブホスティング機能を使ってWEB公開する場合、ブラウザは配置されたオブジェクトのContent−Typeを参照して挙動を変える為、WEB公開するオブジェクトには適切なContent−Typeを仕込んであげる必要があります。

AWS Management ConsoleやAWS SDKを利用する場合にはContent-Typeを自動で設定してくれるのですが、前回検証を行った時点ではgoofys経由でS3にオブジェクトを配置するとContent-Typeは全てbinary/octet-streamにされてしまいました。その為、goofys経由で配置したオブジェクトをWEB公開する為には、S3イベントを検知してLambdaで変換をかける等の何らかの仕掛けを考えて組み合わせる必要がありました。

Content-Type自動設定できるか確認してみた

/etc/mime.typesの準備

s3fsと同様、goofysのContent-Type自動設定では、/etc/mime.typesをマッピング情報として利用します。こちらを利用する為、mailcapパッケージをインストールする必要がありますが、最新のAmazon Linuxではデフォルトで導入されているようでした。

$ sudo rpm -qi mailcap
Name        : mailcap
Version     : 2.1.31
Release     : 2.7.amzn1
Architecture: noarch
Install Date: 2016年04月29日 23時58分12秒
Group       : System Environment/Base
Size        : 52877
License     : Public Domain and MIT
Signature   : RSA/SHA256, 2012年02月17日 18時14分57秒, Key ID bcb4a85b21c0f39f
Source RPM  : mailcap-2.1.31-2.7.amzn1.src.rpm
Build Date  : 2012年01月05日 21時59分44秒
Build Host  : build-31005.build
Relocations : (not relocatable)
Packager    : Amazon.com, Inc. <http://aws.amazon.com>
Vendor      : Amazon.com
URL         : http://git.fedoraproject.org/git/mailcap.git
Summary     : Helper application and MIME type associations for file types
Description :
The mailcap file is used by the metamail program.  Metamail reads the
mailcap file to determine how it should display non-text or multimedia
material.  Basically, mailcap associates a particular type of file
with a particular program that a mail agent or other program can call
in order to handle the file.  Mailcap should be installed to allow
certain programs to be able to handle non-text files.

Also included in this package is the mime.types file which contains a
list of MIME types and their filename "extension" associations, used
by several applications e.g. to determine MIME types for filenames.

$ sudo rpm -ql mailcap
/etc/mailcap
/etc/mime.types
/usr/share/doc/mailcap-2.1.31
/usr/share/doc/mailcap-2.1.31/COPYING
/usr/share/doc/mailcap-2.1.31/NEWS
/usr/share/man/man4/mailcap.4.gz

$ ls -la /etc/mime.types
-rw-r--r-- 1 root root 43591  1月  5  2012 /etc/mime.types

goofysのインストール

goofysのインストール手順は、以前に書いた記事をご覧ください。

最新バージョンの確認(2016年6月執筆時点)

それでは、最新バージョンのオプションを確認してみましょう。

$ goofys -h
Error: goofys takes exactly two arguments.

NAME:
   goofys - Mount an S3 bucket locally

USAGE:
   goofys [global options] bucket[:prefix] mountpoint

VERSION:
   0.0.6

GLOBAL OPTIONS:
   --help, -h              Print this help text and exit successfuly.
   -o value                Additional system-specific mount options. Be careful!
   --dir-mode value        Permissions bits for directories. (default: 0755) (default: 493)
   --file-mode value       Permission bits for files (default: 0644) (default: 420)
   --uid value             UID owner of all inodes. (default: 500)
   --gid value             GID owner of all inodes. (default: 500)
   --endpoint value        The non-AWS endpoint to connect to. Possible values: http://127.0.0.1:8081/
   --region value          The non-AWS endpoint to connect to. Possible values: us-east-1, us-west-1, us-west-2, eu-west-1, eu-central-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, sa-east-1, cn-north-1 (default: "us-west-2")
   --storage-class value   The type of storage to use when writing objects. Possible values: REDUCED_REDUNDANCY, STANDARD, STANDARD_IA. (default: "STANDARD")
   --use-path-request      Use a path-style request instead of virtual host-style. (deprecated, always on)
   --profile value         Use a named profile from $HOME/.aws/credentials instead of "default"
   --use-content-type      Set Content-Type according to file extension and /etc/mime.types (default: off)
   --stat-cache-ttl value  How long to cache StatObject results and inode attributes. (default: 1m0s)
   --type-cache-ttl value  How long to cache name -> file/dir mappings in directory inodes. (default: 1m0s)
   --debug_fuse            Enable fuse-related debugging output.
   --debug_s3              Enable S3-related debugging output.
   -f                      Run goofys in foreground.
   --version, -v           print the version

--use-content-typeオプションが追加されていることが確認できました。

動作確認

それでは、簡単に動作確認をしてみます。まずは--use-content-typeオプションなしで、マウントしてファイルを配置してみます。

$ goofys webhosting-test-bucket ./mnt
$ cp index.html ./mnt/
$ cp aaa.jpeg ./mnt/

配置されたオブジェクトのメタデータを確認してみます。

$ aws s3api head-object --bucket webhosting-test-bucket --key index.html
{
    "AcceptRanges": "bytes", 
    "ContentType": "binary/octet-stream", 
    "LastModified": "Fri, 03 Jun 2016 13:16:36 GMT", 
    "ContentLength": 4, 
    "ETag": "\"5c9597f3c8245907ea71a89d9d39d08e\"", 
    "Metadata": {}
}
$ aws s3api head-object --bucket webhosting-test-bucket --key aaa.jpeg
{
    "AcceptRanges": "bytes", 
    "ContentType": "binary/octet-stream", 
    "LastModified": "Fri, 03 Jun 2016 13:16:41 GMT", 
    "ContentLength": 4, 
    "ETag": "\"5c9597f3c8245907ea71a89d9d39d08e\"", 
    "Metadata": {}
}

やはり、binary/octet-streamになってしまいます。それでは新しく追加された--use-content-typeオプションをつけてマウントし直してみましょう。

$ fusermount -u ./mnt
$ goofys --use-content-type webhosting-test-bucket ./mnt
$ cp index.html ./mnt/
$ cp aaa.jpeg ./mnt/

同様に配置されたオブジェクトのメタデータを確認してみましょう。

$ aws s3api head-object --bucket webhosting-test-bucket --key index.html
{
    "AcceptRanges": "bytes", 
    "ContentType": "text/html", 
    "LastModified": "Fri, 03 Jun 2016 13:23:22 GMT", 
    "ContentLength": 4, 
    "ETag": "\"5c9597f3c8245907ea71a89d9d39d08e\"", 
    "Metadata": {}
}
$ aws s3api head-object --bucket webhosting-test-bucket --key aaa.jpeg
{
    "AcceptRanges": "bytes", 
    "ContentType": "image/jpeg", 
    "LastModified": "Fri, 03 Jun 2016 13:23:15 GMT", 
    "ContentLength": 4, 
    "ETag": "\"5c9597f3c8245907ea71a89d9d39d08e\"", 
    "Metadata": {}
}

ファイルの拡張子を見て、ContentTypeメタデータがtext/htmlimage/jpegと自動設定されていますね。/etc/mime.typesでは、下記のようにマッピングされていました。

$ grep text/html /etc/mime.types
text/html                   html htm
$ grep image/jpeg /etc/mime.types
image/jpeg                  jpeg jpg jpe jfif

まとめ

s3fsよりも速いことが売りであるgoofysがさらに便利になって嬉しいです。これで、WEB公開用のコンテンツ配置にも使える可能性が広がったのではないでしょうか?

どこかの誰かのお役に立てば嬉しいです。