Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

参考: 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しろということですね。

ApplePedlar
最近はサーバ周りの仕事をしてます。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした