{"id":257,"date":"2019-09-01T18:00:17","date_gmt":"2019-09-01T09:00:17","guid":{"rendered":"https:\/\/blog.hacya.com\/?p=257"},"modified":"2021-07-04T14:57:33","modified_gmt":"2021-07-04T05:57:33","slug":"cognitos3amplifyreactdropzone3","status":"publish","type":"post","link":"https:\/\/blog.hacya.com\/index.php\/2019\/09\/01\/cognitos3amplifyreactdropzone3\/","title":{"rendered":"Cognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u306b\u3088\u308bS3 \u306eAccess\u5236\u5fa1\u3092Amplify\u3068React\u3068Dropzone\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002(\u305d\u306e3)"},"content":{"rendered":"\n<p>Web Application\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002\u300c<a href=\"https:\/\/blog.hacya.com\/index.php\/2019\/06\/29\/reactstaticwebhosting1\/\">React\u3092\u4f7f\u3063\u3066Static\u306aWeb Hosting\u3092\u3059\u308b\u3002<\/a>\u300d\u3068\u540c\u3058\u3088\u3046\u306b\u3001\u307e\u305a\u306f\u3001React\u306e\u3072\u306a\u578b\u3092\u4f5c\u308a\u307e\u3059\u3002<br><code>create-react-app upload<\/code><br><code>cd upload<\/code><br><code>npm install -save aws-amplify aws-amplify-react<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"904\" src=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit-1024x904.png\" alt=\"\" class=\"wp-image-262\" srcset=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit-1024x904.png 1024w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit-300x265.png 300w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit-768x678.png 768w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit-1200x1060.png 1200w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito30_edit.png 1223w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"904\" src=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit-1024x904.png\" alt=\"\" class=\"wp-image-263\" srcset=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit-1024x904.png 1024w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit-300x265.png 300w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit-768x678.png 768w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit-1200x1060.png 1200w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/09\/S3Cognito31_edit.png 1223w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>\u524d\u56de\u4f5c\u6210\u3001\u307e\u305f\u306f\u3001\u5909\u66f4\u3057\u305fFile\u3092\u53c2\u8003\u306b\u3001S3\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u306a\u304c\u3089\u3001\u4f5c\u6210\u3057\u305f\u3072\u306a\u5f62\u306bCopy\u3057\u307e\u3059\u3002\u8a73\u7d30\u306a\u8aac\u660e\u306f\u3001\u300c<a href=\"https:\/\/blog.hacya.com\/index.php\/2019\/07\/11\/cognitoamplifyreact3\/\">Cognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092Amplify\u3068React\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002(\u305d\u306e3)<\/a>\u300d\u306b\u623b\u3063\u3066\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"index.js\"><code>import React from &#39;react&#39;;\nimport ReactDOM from &#39;react-dom&#39;;\nimport &#39;.\/index.css&#39;;\nimport AppWithAuth from &#39;.\/AppWithAuth&#39;;\nimport * as serviceWorker from &#39;.\/serviceWorker&#39;;\n\n\/\/Import Amplify Configure function\nimport { configureAmplify } from &#39;.\/services&#39;\n\nconfigureAmplify();\n\nReactDOM.render(&lt;AppWithAuth \/&gt;, document.getElementById(&#39;root&#39;));\n\n\/\/ If you want your app to work offline and load faster, you can change\n\/\/ unregister() to register() below. Note this comes with some pitfalls.\n\/\/ Learn more about service workers: http:\/\/bit.ly\/CRA-PWA\nserviceWorker.unregister();<\/code><\/pre><\/div>\n\n\n\n<p>services.js\u3067\u306f\u3001S3\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u307e\u305f\u3001SetS3Config()\u3068\u3044\u3046function\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002Bucket\u3084S3\u306eAccess Level\u3092\u5909\u66f4\u3057\u305f\u5f8c\u3001Amplify\u306eStorage.Configure()\u3092Call\u3057\u3066\u3001Amplify\u306e\u8a2d\u5b9a\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"services.js\"><code>import Amplify from &#39;@aws-amplify\/core&#39;;\nimport Auth from &#39;@aws-amplify\/auth&#39;;\nimport Storage from &#39;@aws-amplify\/storage&#39;;\n\nexport function configureAmplify() {\n  Amplify.configure(\n  {\n    Auth: {\n      region: process.env.REACT_APP_region,\n      userPoolId: process.env.REACT_APP_userPoolId,\n      userPoolWebClientId: process.env.REACT_APP_userPoolWebClientId,\n      identityPoolId: process.env.REACT_APP_identityPoolId,\n    },\n    Storage: {\n      AWSS3: {\n        bucket: process.env.REACT_APP_bucket_name,\n        region: process.env.REACT_APP_region,\n      }\n    }\n\n  } );\n}\n\n\/\/Configure Storage with S3 bucket information\nexport function SetS3Config(bucket, level){\n  Auth.configure({\n    region:  process.env.REACT_APP_region,\n    identityPoolId: process.env.REACT_APP_identityPoolId,\n  });\n  Storage.configure({ \n    bucket: bucket,\n    level: level,\n  });\n}<\/code><\/pre><\/div>\n\n\n\n<p>project\u306eroot directory\u306e  \u201c.env\u201d\u306bS3\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u305f\u30015\u3064\u306e\u60c5\u5831\u3092\u8a2d\u5b9a\u307e\u3059\u3002<br><code>REACT_APP_region=ap-northeast-1<\/code><br><code>REACT_APP_userPoolId=ap-northeast-1<em>yourPoolID<\/em><\/code><br><code>REACT_APP_userPoolWebClientId=<em>yourWebClientId<\/em><\/code><br><code>REACT_APP_identityPoolId=ap-northeast-1:<em>yourIdentityPoolId<\/em><\/code><br><code>REACT_APP_bucket_name=<em>yourBucketName<\/em><\/code><\/p>\n\n\n\n<p>AppWithAuth.js\u3001MyVerifyContact.js\u3001MyConfirmSignUp.js\u306f\u5909\u66f4\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"AppWithAuth.js\"><code>import React from &#39;react&#39;;\nimport { Authenticator, ConfirmSignIn, RequireNewPassword, ConfirmSignUp, VerifyContact, ForgotPassword, TOTPSetup } from &#39;aws-amplify-react&#39;;\nimport App from &quot;.\/App&quot;;\nimport MyVerifyContact from &quot;.\/MyVerifyContact&quot;\nimport MyConfirmSignUp from &quot;.\/MyConfirmSignUp&quot;\n\nconst signUpConfig = {\n  hideAllDefaults: true,\n  signUpFields: [\n    {\n      label: &#39;Username&#39;,\n      key: &#39;username&#39;,\n      required: true,\n      placeholder: &#39;Username&#39;,\n      displayOrder: 1\n    },\n    {\n      label: &#39;Password&#39;,\n      key: &#39;password&#39;,\n      required: true,\n      placeholder: &#39;Password&#39;,\n      type: &#39;password&#39;,\n      displayOrder: 2\n    }\n  ]\n}\n\nfunction AppWithAuth() {\n\n  function handleAuthStateChange(state, data) {\n    if (state === &#39;confirmSignIn&#39;) {\n        \/* Do something when the user has signed-in *\/\n    }\n  }\n\n  return (\n    &lt;Authenticator signUpConfig={signUpConfig} hide={[ConfirmSignIn, RequireNewPassword, VerifyContact, ConfirmSignUp, ForgotPassword, TOTPSetup]} onStateChange={handleAuthStateChange}&gt;\n      &lt;MyVerifyContact override={VerifyContact} \/&gt;\n      &lt;MyConfirmSignUp override={ConfirmSignUp} \/&gt;\n      &lt;App \/&gt;\n    &lt;\/Authenticator&gt;\n  )\n}\n\nexport default AppWithAuth;<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"MyVerifyContact.js\"><code>import { AuthPiece } from &#39;aws-amplify-react&#39;;\n\nclass MyVerifyContact extends AuthPiece {\n  constructor(props) {\n    super(props);\n    this._validAuthStates = [&quot;verifyContact&quot;];\n    this.state = {}\n  }\n\n  static getDerivedStateFromProps(props, state) {\n    if(props.authState === &quot;verifyContact&quot;) {\n      props.onStateChange(&#39;signedIn&#39;);\n      return props;\n    } else {\n      return null;\n    }\n  }\n\n  showComponent(theme) {\n    return null;\n  }\n\n}\n\nexport default MyVerifyContact;<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"MyConfirmSignUp.js\"><code>import React from &#39;react&#39;;\n\nimport { I18n } from &#39;aws-amplify&#39;;\nimport { AuthPiece } from &#39;aws-amplify-react&#39;;\n\nimport {\n  FormSection,\n  SectionHeader,\n  SectionBody,\n  SectionFooter,\n  Button\n} from &#39;aws-amplify-react&#39;;\n\nclass MyConfirmSignUp extends AuthPiece {\n  constructor(props) {\n    super(props);\n    this._validAuthStates = [&quot;confirmSignUp&quot;];\n    this.gotoSignIn = this.gotoSignIn.bind(this);\n  }\n\n  gotoSignIn() {\n    super.changeState(&#39;signIn&#39;);\n  }\n\n  showComponent(theme) {\n    return (\n      &lt;FormSection theme={theme}&gt;\n        &lt;SectionHeader theme={theme}&gt;{I18n.get(&#39;Please contact your administrator to grant your account.&#39;)}&lt;\/SectionHeader&gt;\n        &lt;SectionBody theme={theme}&gt;\n        &lt;\/SectionBody&gt;\n        &lt;SectionFooter theme={theme}&gt;\n          &lt;Button onClick={this.gotoSignIn} theme={theme}&gt;\n            {I18n.get(&#39;Goto SignIn&#39;)}\n          &lt;\/Button&gt;\n        &lt;\/SectionFooter&gt;\n      &lt;\/FormSection&gt;\n    );\n\n  }\n\n}\n\nexport default MyConfirmSignUp;<\/code><\/pre><\/div>\n\n\n\n<p>\u6700\u5f8c\u306bApp.js\u3092\u7de8\u96c6\u3057\u3066\u3001S3\u3078Upload\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"App.js\"><code>import React, { useState, createRef }  from &#39;react&#39;;\nimport logo from &#39;.\/logo.svg&#39;;\nimport Storage from &quot;@aws-amplify\/storage&quot;;\nimport { SetS3Config } from &quot;.\/services&quot;;\nimport &#39;.\/App.css&#39;;\n\nfunction App(props) {\n\n  const [uploadFile, setUploadFile] = useState({});\n  const [response, setResponse] = useState(&quot;&quot;);\n  const uploadRef = createRef();\n  const onInputClick = () =&gt; {\n    if(uploadRef.current) {\n      uploadRef.current.value = null;\n      uploadRef.current.click();\n    }\n    setResponse(&quot;&quot;);\n  }\n\n  function uploadFileToS3() {\n    SetS3Config(process.env.REACT_APP_bucket_name, &quot;protected&quot;);\n    Storage.put(`upload\/${uploadFile.name}`,\n                uploadFile,\n                { contentType: uploadFile.type })\n      .then(result =&gt; {\n        console.log(result);\n        setUploadFile({});\n        setResponse(&quot;File is uploaded!&quot;);\n      })\n      .catch(err =&gt; {\n        console.log(err);\n        setResponse(`File is not uploaded: ${err}`);\n      });\n  };\n\n  function InputUploadFile(props) {\n    return(\n      &lt;div  align=&quot;center&quot;&gt;\n        &lt;input\n          type=&quot;file&quot;\n          style={{ display: &quot;none&quot; }}\n          accept=&quot;image\/tif, image\/tiff&quot;\n          ref={uploadRef}\n          onChange={(e) =&gt; setUploadFile(e.target.files[0])}\n        \/&gt;\n        &lt;input value={uploadFile.name} placeholder=&quot;Select file&quot; \/&gt;\n        &lt;button onClick={onInputClick}&gt; Browse &lt;\/button&gt;\n        &lt;button onClick={uploadFileToS3}&gt; Upload File &lt;\/button&gt;\n\n        {!!response && &lt;div&gt;{response}&lt;\/div&gt;}\n      &lt;\/div&gt;\n    )\n  }\n\n  if(props.authState === &#39;signedIn&#39;) {\n    return (\n      &lt;div className=&quot;App&quot;&gt;\n        &lt;InputUploadFile \/&gt;\n\n        &lt;header className=&quot;App-header&quot;&gt;\n          &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; \/&gt;\n          &lt;p&gt;\n            Edit &lt;code&gt;src\/App.js&lt;\/code&gt; and save to reload.\n          &lt;\/p&gt;\n          &lt;a\n            className=&quot;App-link&quot;\n            href=&quot;https:\/\/reactjs.org&quot;\n            target=&quot;_blank&quot;\n            rel=&quot;noopener noreferrer&quot;\n          &gt;\n            Learn React\n          &lt;\/a&gt;\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else if(props.authState === &#39;confirmSignUp&#39;) {\n    return (\n      &lt;div className=&quot;Your account is not granted&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          Your account is not granted.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else if(props.authState === &#39;signIn&#39;) {\n    return (\n      &lt;div className=&quot;Welcome to the world&quot;&gt;\n        &lt;InputUploadFile \/&gt;\n\n        &lt;header className=&quot;App-header&quot;&gt;\n          Welcome to the world.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else {\n    return (\n      &lt;div className=&quot;Login failed&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          Login failed.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n\nexport default App;<\/code><\/pre><\/div>\n\n\n\n<p>npm start\u304b\u3089\u306e\u8aac\u660e\u306f\u6b21\u56de\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web Application\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002\u300cReact\u3092\u4f7f\u3063\u3066Static\u306aWeb Hosting\u3092\u3059\u308b\u3002\u300d\u3068\u540c\u3058\u3088\u3046\u306b\u3001\u307e\u305a\u306f\u3001React\u306e\u3072\u306a\u578b\u3092\u4f5c\u308a\u307e\u3059\u3002create-react-app uploadcd upl &hellip; <a href=\"https:\/\/blog.hacya.com\/index.php\/2019\/09\/01\/cognitos3amplifyreactdropzone3\/\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Cognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u306b\u3088\u308bS3 \u306eAccess\u5236\u5fa1\u3092Amplify\u3068React\u3068Dropzone\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002(\u305d\u306e3)&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[14,4,10,13,17,7],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-aws","tag-amplify","tag-aws","tag-s3","tag-cognito","tag-dropzone","tag-react"],"_links":{"self":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":5,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}