11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AmazonS3Client#getObject使用後は必ずcloseしよう

Last updated at Posted at 2014-03-25

参考: http://stackoverflow.com/questions/17782937/connectionpooltimeoutexception-when-iterating-objects-in-s3

S3のファイルの最終更新日時を取得するためにこういうコードを書くと、50回ほど実行したときにConnectionPoolTimeoutExceptionが発生します。

AmazonS3Client s3 = new AmazonS3Client(引数省略);
S3Object obj = s3.getObject("backetname", "filename.txt");
long lastModified = obj.getObjectMetadata().getLastModified().getTime();

これは、getObjectがcloseされてないせいで、ちゃんとobj.close()を呼べば解決。

S3Object#getObjectContentを呼んだときだけcloseしなきゃならないと思っていたけれど、どうも無条件でcloseしなきゃならないようです。

ObjectMetadataはAmazonS3Clientから直接取れるので、getObjectを使わない以下のコードでもOK。

AmazonS3Client s3 = new AmazonS3Client(引数省略);
long lastModified = s3.getObjectMetadata("backetname", "filename.txt").getLastModified().getTime();

S3ObjectはCloseableです。Closeableなインスタンスは必ずcloseしろということですね。

11
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
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?