1
4

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.

JSのheadObjectで期待したレスポンスが取得できない時

Posted at

普通にコード書いてたらハマる人が多いのではないかと寄稿します。

ハマった経緯

S3上のファイルの存在確認や、サイズ確認でgetObjectを利用するのは無駄ですので、headObjectを使いたいところです。

ところがドキュメントに従って実装しても、Content-Length等のレスポンスが返ってきません。

HEADメソッドは200を返しているし、Response HeaderにもContent-Lengthは取得できています。でも上記メソッドのレスポンスにContent-Lengthはありません。

???

こんな時はだいたいIAMが悪さしてるのかと権限を見直しても問題なし。
バケットポリシーか?と疑って見ても問題なし。
CORS設定でもちゃんとHEADは許可している。

これaws-sdkのバグじゃないの、、、わしもう知らん、、、と心が折れかけたところで同僚が

つ https://github.com/aws/aws-sdk-js/issues/631

と出してくれました。

解決策

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>300</MaxAgeSeconds>
        <ExposeHeader>Content-Length</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

ExposeHeader...だと、、、!!

pythonのSDKでは問題なく動いていたので、JS動かないのはJSのバグだ、と思いたくなっていたんですが、そうか、CORSなら納得だ、、、

SDKのドキュメントにもExposeHeaderについて記載しておいて欲しいなぁ、、と思った一幕でした。

1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?