LoginSignup
1
1

More than 5 years have passed since last update.

[JAWS-UG CLI] Cognito #2 S3コンテンツの作成

Last updated at Posted at 2015-09-28

前提条件

Cognito (Identity)への権限

  • Cognito (Identity)に対してフル権限があること。

AWS CLIのバージョン

  • 以下のバージョンで動作確認済

    • AWS CLI 1.8.3
コマンド
aws --version
結果(例)
      aws-cli/1.8.3 Python/2.7.5 Darwin/13.4.0

0 準備

0.1. 識別子プールIDの指定

変数の設定
COGNITO_IDPOOL_ID=<IDプールの識別子>

識別子プール名の存在するリージョンも指定します。

変数の設定
AWS_REGION='ap-northeast-1'

0.2. S3バケット名の指定

変数の設定
S3_BUCKET_NAME=<S3バケット名>

0.3. ファイル名の指定

変数の設定
FILE_CONTENTS='post.html'

1. コンテンツの作成

変数の確認
cat << ETX

        COGNITO_IDPOOL_ID: ${COGNITO_IDPOOL_ID}
        AWS_REGION:        ${AWS_REGION}
        S3_BUCKET_NAME:    ${S3_BUCKET_NAME}
        FILE_CONTENTS:     ${FILE_CONTENTS}

ETX
コマンド
cat << EOF > ${FILE_CONTENTS}
      <!DOCTYPE html>
      <meta charset="UTF-8">
      <script src="https://sdk.amazonaws.com/js/aws-sdk-2.2.0.min.js"></script>
      <title>お問い合わせフォーム</title>
      <script>
        var func = function(id) { return document.getElementById(id); };
        AWS.config.region = "${AWS_REGION}";
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({IdentityPoolId: "${COGNITO_IDPOOL_ID}"});
        AWS.config.credentials.get(function(err) {
            if (!err) {
                console.log("Cognito Identify Id: " + AWS.config.credentials.identityId);
            }
        });

        function uploadFile() {
          AWS.config.region = '${AWS_REGION}';
          var s3BucketName = "${S3_BUCKET_NAME}";
          var now = new Date();
          var obj = {"title": func("title").value, "mail": func("mail").value ,"contents": func("contents").value, "date": now.toLocaleString()};
          var s3 = new AWS.S3({params: {Bucket: s3BucketName}});
          var blob = new Blob([JSON.stringify(obj, null, 2)], {type:'text/plain'});
          s3.putObject({Key: "uploads/" +now.getTime()+".txt", ContentType: "text/plain", Body: blob, ACL: "public-read"},
          function(err, data){
            if(data !== null){
              alert("お問い合わせ完了致しました");
            }
            else{
              alert("Upload Failed" + err.message);
            }
          });
        }
      </script>

      <div>
        <div>
          <form>
            <table>
              <tr>
                <th>件名</th>
                <td><input id="title" type="text" name="title" maxlength="40" value="" /></td>
              </tr>
              <tr>
                <th>メールアドレス</th>
                <td><input id="mail" type="email" name="mail" size="20" maxlength="50" /></td>
              </tr>
              <tr>
                <th>お問い合わせ内容</th>
                <td><textarea id="contents" cols="70" rows="6" name="contents"></textarea></td>
              </tr>
            </table>
            <div>
              <input onClick="uploadFile();" type="button" value="送信" id="button" />
            </div>
          </form>
        </div>
      </div>
EOF

cat ${FILE_CONTENTS}

2. コンテンツの転送

コマンド
aws s3 cp ${FILE_CONTENTS} "s3://${S3_BUCKET_NAME}/"
結果(例)
      upload: ./post.html to s3://example-handson-20150928/post.html

完了

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