- ãããããã¨
- åè
- æ¤è¨¼ç°å¢
- Data Bucket ã«æªèªè¨¼ã¦ã¼ã¶ã¼ãæ¸ãè¾¼ãããããªç°å¢ã AWS CLI ã§ç¨æãã
- ãã¨ã¯ HTML 㨠JavaScript ã§ãã©ã¼ã ãç¨æã㦠HTML Bucket ã«æ¾ãè¾¼ã
- éè¨ã¨ã
- 以ä¸
ãããããã¨
- S3 ã§æ§æããããµã¤ãã« HTML ãã©ã¼ã ãå®è£ ããã¢ã³ã±ã¼ããã¼ã¸çãªãã®ãæä¾ããã
- æçµçã«ã¯ S3 ã®åã«ã¯ CloudFront ãæãäºå®
- æ稿ã®å 容㯠S3 ã« JSON ã§ä¿åãã
å³ã«ããã¨ä»¥ä¸ã®ãããªæãã
åè
ãããã¨ããããã¾ããã»ã¼ãä¸è¨ãã¼ã¸ã®åçµã¨ãªãã¾ãã
HTML ãã©ã¼ã ã JavaScript ããæ±ãæ¹æ³ã¯ä¸è¨ã®ãã¼ã¸ãåèã«ãªãã¾ãããæé£ããããã¾ãã
æ¤è¨¼ç°å¢
OS
$ sw_vers ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1217
AWS CLI
$ aws --version
aws-cli/1.11.19 Python/2.7.10 Darwin/15.6.0 botocore/1.4.76
Data Bucket ã«æªèªè¨¼ã¦ã¼ã¶ã¼ãæ¸ãè¾¼ãããããªç°å¢ã AWS CLI ã§ç¨æãã
Cognito ã IAM çãè²ã ã¨è¨å®ããå¿ è¦ããããã©ãAWS CLI ã§å ¨é¨æºåããã
# # åç°å¢å¤æ°ã«å¤ãå®ç¾© # _PROFILE=washino-profile _REGION=ap-northeast-1 _DOMAIN_NAME=washi.inokara.tech _IDENTITY_POOL_NAME=washino_identity _S3_BUCKET_NAME=data-bucket # # identity pool ãä½æ # _IDENTITY_POOL_ID=$(aws --profile ${_PROFILE} cognito-identity create-identity-pool --identity-pool-name ${_IDENTITY_POOL_NAME} --allow-unauthenticated-identities --query IdentityPoolId --output text) echo ${_IDENTITY_POOL_ID} # # S3 ãã±ãããä½æ # aws --profile ${_PROFILE} --region ${_REGION} s3 mb s3://${_S3_BUCKET_NAME} # # ä½æãããã±ããã« CORS ãè¨å®ï¼ããããã£ã¨ããã¨ãããã°ãï¼ # cat << EOT >> cors.json { "CORSRules": [ { "AllowedOrigins": ["https://${_DOMAIN_NAME}"], "AllowedHeaders": ["*"], "AllowedMethods": ["PUT"] } ] } EOT aws --profile ${_PROFILE} s3api put-bucket-cors --bucket ${_S3_BUCKET_NAME} --cors-configuration file://cors.json # # ãã±ããããªã·ã¼ãä½æ # cat << EOT >> s3PutAllowPolicy.json { "Version": "2012-10-17", "Statement": [ { "Sid": "S3PutAllowPolicy", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::${_S3_BUCKET_NAME}/*" } ] } EOT _POLICY_ARN=$(aws --profile ${_PROFILE} iam create-policy --policy-name s3PutAllowPolicy --policy-document file://s3PutAllowPolicy.json --query Policy.Arn --output text) echo ${_POLICY_ARN} # # identity pool ã«ä»ä¸ãã IAM Role ãä½æ # cat << EOT >> ${_IDENTITY_POOL_NAME}_authenticated.json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "cognito-identity.amazonaws.com" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "cognito-identity.amazonaws.com:aud": "${_IDENTITY_POOL_ID}" }, "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "authenticated" } } } ] } EOT _IAM_ROLE_ARN=$(aws --profile ${_PROFILE} iam create-role --role-name cognito_${_IDENTITY_POOL_NAME}_authenticated --assume-role-policy-document file://${_IDENTITY_POOL_NAME}_authenticated.json --query Role.Arn --output text) aws --profile ${_PROFILE} iam attach-role-policy --role-name cognito_${_IDENTITY_POOL_NAME}_authenticated --policy-arn ${_POLICY_ARN} # # identity pool ã« IAM Role ãé©ç¨ãã # aws --profile ${_PROFILE} cognito-identity set-identity-pool-roles --identity-pool-id ${_IDENTITY_POOL_ID} --roles authenticated=${_IAM_ROLE_ARN} # # identity pool ã«ä»ä¸ãã IAM Role ãä½æï¼æªèªè¨¼ã¦ã¼ã¶ã¼ç¨ï¼ # cat << EOT >> ${_IDENTITY_POOL_NAME}_unauthenticated.json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Federated": "cognito-identity.amazonaws.com" }, "Action": "sts:AssumeRoleWithWebIdentity", "Condition": { "StringEquals": { "cognito-identity.amazonaws.com:aud": "${_IDENTITY_POOL_ID}" }, "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "unauthenticated" } } } ] } EOT _IAM_ROLE_ARN=$(aws --profile ${_PROFILE} iam create-role --role-name cognito_${_IDENTITY_POOL_NAME}_unauthenticated --assume-role-policy-document file://${_IDENTITY_POOL_NAME}_unauthenticated.json --query Role.Arn --output text) aws --profile ${_PROFILE} iam attach-role-policy --role-name cognito_${_IDENTITY_POOL_NAME}_unauthenticated --policy-arn ${_POLICY_ARN} # # identity pool ã« IAM Role ãé©ç¨ãã # aws --profile ${_PROFILE} cognito-identity set-identity-pool-roles --identity-pool-id ${_IDENTITY_POOL_ID} --roles unauthenticated=${_IAM_ROLE_ARN} # # å¾çä»ãï¼ãããªãã¦ãè¯ãï¼ # rm -f cors.json s3PutAllowPolicy.json ${_IDENTITY_POOL_NAME}_authenticated.json ${_IDENTITY_POOL_NAME}_unauthenticated.json
ãã¨ã¯ HTML 㨠JavaScript ã§ãã©ã¼ã ãç¨æã㦠HTML Bucket ã«æ¾ãè¾¼ã
HTML ã¯ãããªæã
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="aws-sdk.min.js"></script> <script src="demo.js"></script> <title>éãªã¢ã³ã±ã¼ã</title> </head> <body> <form name='orenoForm'> <table> <tr> <th>ãåå</th> <td> <input name="_name" type="text" maxlength="50" value="" /> </td> </tr> <tr> <th>ã¡ã¼ã«ã¢ãã¬ã¹</th> <td> <input name="_mail" type="email" maxlength="50" value="" /> </td> </tr> <tr> <th>æ§å¥</th> <td> <input name="_sex" type="radio" value="ç·"/>ç·</input> <input name="_sex" type="radio" value="女"/>女</input> <input name="_sex" type="radio" value="æªé¸æ" checked="checked" />æªé¸æ</input> </td> </tr> <tr> <th>ä¸ã®ä¸ã«ä¸æºã</th> <td> <input name="_fuman" type="radio" value="æãã"/>æãã</input> <input name="_fuman" type="radio" value="æããªã"/>æããªã</input> <input name="_fuman" type="radio" value="æªé¸æ" checked="checked" />æªé¸æ</input> </td> </tr> <tr> <th>2017 å¹´ãã¼ã¯ã¹ã¯</th> <td> <input name="_hawks" type="radio" value="åªåãã"/>åªåãã</input> <input name="_hawks" type="radio" value="åªåããªã"/>åªåããªã</input> <input name="_hawks" type="radio" value="æªé¸æ" checked="checked" />æªé¸æ</input> </td> </tr> <tr> <th>ã³ã¡ã³ããã©ãã</th> <td> <TEXTAREA name="_comment" cols="40" rows="6" name="comment"></TEXTAREA> </td> </tr> </table> <input onClick="postMessage();" type="button" value="æ稿"/> </form> </body> </html>
JavaScript ã¯ãããªæã
// // demo.js // var $aws_region = "ap-northeast-1"; var $aws_cognito_identity_pool_id = "ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx"; // Cognito ã§ä½æãã identity pool id ãè¨å®ãã var $s3_prefix = "data/201702/"; var $s3_bucket = "xxxxxxxxxxxxxxxxxxxxxxxx"; AWS.config.region = $aws_region; AWS.config.credentials = new AWS.CognitoIdentityCredentials({IdentityPoolId: $aws_cognito_identity_pool_id}); // AWS.config.credentials.get(function(err) { // if (!err) { // console.log("Cognito Identify Id: " + AWS.config.credentials.identityId); // } // }); function postMessage() { var form = document.forms.orenoForm; var now = new Date(); var obj = {"name": form._name.value, "mail": form._mail.value, "comment": form._comment.value, "sex": form._sex.value, "fuman": form._fuman.value, "hawks": form._hawks.value, "date": now.toLocaleString()}; var s3 = new AWS.S3({params: {Bucket: $s3_bucket}}); var blob = new Blob([JSON.stringify(obj, null, 2)], {type:'text/plain'}); s3.putObject({Key: $s3_prefix + now.getTime() +".json", ContentType: "text/plain", Body: blob, ACL: "public-read"}, function(err, data){ if(data !== null){ location.href="thanks.html"; } else{ alert("Post Failed: " + err.message); } }); }
Chrome ã§è¦ã㨅
ãªããã¢ã³ã±ã¼ããã¼ã¸ã£ã½ãã§ããªã
ã¢ã³ã±ã¼ãã«çãã㨅
以ä¸ã®ããã« S3 ãã±ããã« JSON ãã¼ã¿ãä¿åãããã
bash-3.2$ ls -tr *.json 1486298468151.json 1486298522074.json 1486298702117.json bash-3.2$ cat 1486298702117.json | jq . { "date": "2017/2/5 21:45:02", "hawks": "åªåãã", "fuman": "æããªã", "sex": "女", "comment": "ãããã", "mail": "[email protected]", "name": "ãã¾ã ã¯ãªã" }
éè¨ã¨ã
S3 ã® put ã¤ãã³ããæ¾ã£ã¦ Lambda ã¨çµã¿åãããä¾ãç´¹ä»ããã¦ãããã©ãã¨ãããã㯠Google SpreadSheet ã«çµæãçªã£è¾¼ãã§ããã°è¯ãããã¨ãããã¨ã§ã以ä¸ã®è¨äºã®ããã« Google SpreadSheet ã«çªã£è¾¼ãã§ãããã¨ã«ããã
Elasticsearch ã¨ãã«çªã£è¾¼ãã§ãé¢ç½ããã ãã©ã
以ä¸
ã¡ã¢ã§ããã