-
Notifications
You must be signed in to change notification settings - Fork 4
s3へのput objectで400のRequestTimeoutが発生する現象について
mechamogera edited this page Sep 11, 2012
·
5 revisions
- AWSのs3へput objectのリクエストを行った際、以下のエラーレスポンスが返る
- ステータスコード:400
- ボディ:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>RequestTimeout</Code><Message>Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.</Message><RequestId>77DE2218A1C6579A</RequestId><HostId>olfndHJMMFZDY4BALZiLpUd0aX8jQljK0y1qj+qkTb89WMQJiDmqYcJRGxCOMPOi</HostId></Error>
- Developer Forumに同現象についての書き込みがあり、以下のような回答がある。
Amazon S3 will send that error response after 20 seconds of inactivity. The error indicates that Amazon S3 was attempting to read the request body, but no new data arrived over a period of 20 seconds.
- s3がリクエストボディを読もうとして20秒間データが受け取れなかった場合にエラーとなるらしい
- リクエストのContent-Lengthヘッダを2にして1byteのデータを送ると上記のエラーが発生した。
- 認証が面倒なのでs3 bucketに以下のポリシーを適用し、認証なしでput objectできるようにする
- Principal(s):*
- Effect:Allow
- Action:s3:PutObject、s3:PutObjectAcl
- Resource:arn:aws:s3:::[bucket名]/*
- Conditions:None
- 以下のrubyスクリプトを作成する
- rest-client、net-httpでContent-Lengthが自動設定され指定できなかったのでsocketで実装
- Content-Length:1にすると内容が「a」のファイルがputできることを確認済
require 'socket'
s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(80, "[bucket名].s3.amazonaws.com")
s.connect(sockaddr)
req = "PUT \/hoge HTTP\/1.0\r\n"
req += "Host:[bucket名].s3.amazonaws.com\r\n"
req += "Content-Length:2\r\n"
req += "x-amz-acl:public-read\r\n"
req += "\r\n"
req += "a"
s.write req
print s.read
- rubyスクリプトを実行する
- なぜかエラーまで40秒ほどかかっている、仕様変更?
$ time ruby upload_s3_socket.rb
HTTP/1.1 400 Bad Request
x-amz-request-id: AF63A65907B601DF
x-amz-id-2: Hnc0CZvhayWk8JRf3u8LI31/NBgRTq1WanWrY8M3caR8NHdYrHRgO3/eqsglyVdR
Content-Type: application/xml
Date: Tue, 11 Sep 2012 21:48:16 GMT
Connection: close
Server: AmazonS3
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>RequestTimeout</Code><Message>Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.</Message><RequestId>AF63A65907B601DF</RequestId><HostId>Hnc0CZvhayWk8JRf3u8LI31/NBgRTq1WanWrY8M3caR8NHdYrHRgO3/eqsglyVdR</HostId></Error>
real 0m40.759s
user 0m0.005s
sys 0m0.008s