6
5

More than 5 years have passed since last update.

s3のファイルをexistsチェック(ファイルの存在チェック)したい

Last updated at Posted at 2015-09-24

Exceptionを無理矢理...

aws sdk for Javaには配置オブジェクトのexistsチェックapiが存在しない。仕方ないので、exceptionを無理矢理処理に埋め込みました。


public boolean existsFile(String bucketName, String key) {
        /**
         * ファイルの存在をチェックするapiがs3に存在しないので、やむなく例外処理を利用しています
         * ファイルの数がすくなければ、bucket以下のファイルをlistで取得してから舐める方法もありかもしれません
         */
        try {
            s3.getObjectMetadata(bucketName, key);
            return true;
        } catch(AmazonServiceException e) {
            if (e.getStatusCode() != 404) {
                throw e;
            }
            return false;
        }
    }

参考

6
5
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
6
5