{"id":3593,"date":"2024-06-23T17:33:32","date_gmt":"2024-06-23T08:33:32","guid":{"rendered":"https:\/\/blue-bear.jp\/kb\/?p=3593"},"modified":"2024-06-23T17:33:33","modified_gmt":"2024-06-23T08:33:33","slug":"python-aws-cognito%e3%81%ab%e3%83%a1%e3%83%bc%e3%83%ab%e3%82%a2%e3%83%89%e3%83%ac%e3%82%b9csv%e3%82%92cli%e3%81%a7%e6%b5%81%e3%81%97%e8%be%bc%e3%82%80%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/python-aws-cognito%e3%81%ab%e3%83%a1%e3%83%bc%e3%83%ab%e3%82%a2%e3%83%89%e3%83%ac%e3%82%b9csv%e3%82%92cli%e3%81%a7%e6%b5%81%e3%81%97%e8%be%bc%e3%82%80%e6%96%b9%e6%b3%95\/","title":{"rendered":"[Python] AWS Cognito\u306b\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9CSV\u3092CLI\u3067\u6d41\u3057\u8fbc\u3080\u65b9\u6cd5"},"content":{"rendered":"\n<p>Python\u3092\u4f7f\u3063\u3066\u3001AWS Cognito\u306b\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9CSV\u3092CLI\u3067\u6d41\u3057\u8fbc\u3080\u65b9\u6cd5\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n\n\n\n<p>\u305f\u3060\u3057\u3001\uff11\u884c\uff11\u884cPUSH\u3059\u308b\u306e\u3067\u3001AWS\u30dd\u30fc\u30bf\u30eb\u306eCSV\u30a4\u30f3\u30dd\u30fc\u30c8\u306e\u307b\u3046\u304c\u65e9\u3044\u306f\u305a\u3002CSV\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3092Cognito\u7528\u306b\u5909\u3048\u305f\u304f\u306a\u3044\u4eba\u306e\u305f\u3081\u7528<\/p>\n\n\n\n<p>\u672cCSV\u306f\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3001\u540d\u524d\u3068\u3044\u3046\uff12\u3064\u306e\u30ab\u30e9\u30e0\u304c\u3042\u308bCSV\uff08shift-jis\uff09\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u624b\u9806<\/h2>\n\n\n\n<p>\u5fc5\u8981\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install boto3 chardet\u3000awscli<\/code><\/pre>\n\n\n\n<p>IAM\u3092\u4f5c\u6210\u3057\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3068\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc\u3092\u4f5c\u6210\u3002<\/p>\n\n\n\n<p>aws configure\u3067AWS CLI\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304b\u3089\u3001<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws configure<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c<\/p>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u540d\u306fmain.py\u306a\u3069\u3067\u3088\u3044<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\nimport boto3\nimport chardet\n\n# Set the User Pool ID\nuser_pool_id = '&lt;POOL ID>'\ncsv_file = 'mail.csv'\n\n# Manually set the encoding to 'shift_jis'\nencoding = 'shift_jis'\nprint(f\"Using encoding: {encoding}\")\n\n# Initialize a Cognito client with the specified region\nclient = boto3.client('cognito-idp', region_name='ap-northeast-1')  # Replace 'ap-northeast-1' with your region\n\n# Open the CSV file with the specified encoding\nwith open(csv_file, newline='', encoding=encoding) as csvfile:\n    reader = csv.DictReader(csvfile)\n    \n    # Print the header row to debug\n    headers = reader.fieldnames\n    print(f\"CSV Headers: {headers}\")\n\n    # Strip whitespace from headers\n    headers = &#91;header.strip() for header in headers]\n    \n    for row in reader:\n        # Strip whitespace from keys in each row\n        row = {key.strip(): value for key, value in row.items()}\n        \n        email = row.get('\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9')\n        name = row.get('\u540d\u524d', '')\n\n        if email:  # Ensure email is not empty\n            try:\n                response = client.admin_create_user(\n                    UserPoolId=user_pool_id,\n                    Username=email,\n                    UserAttributes=&#91;\n                        {'Name': 'email', 'Value': email},\n                        {'Name': 'email_verified', 'Value': 'true'},\n                    ],\n                    MessageAction='SUPPRESS'  # \u78ba\u8a8d\u30e1\u30fc\u30eb\u3092\u9001\u4fe1\u3057\u306a\u3044\n                )\n                print(f\"Successfully added {email}\")\n            except Exception as e:\n                print(f\"Failed to add {email}: {str(e)}\")\n\nprint(\"Import process completed.\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3092\u4f7f\u3063\u3066\u3001AWS Cogni<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,500],"tags":[532,305],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3593"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=3593"}],"version-history":[{"count":1,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3593\/revisions"}],"predecessor-version":[{"id":3594,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/3593\/revisions\/3594"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=3593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=3593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=3593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}