Last active
August 20, 2020 03:47
-
-
Save naramdash/cd426138f218c116f129c73932524e56 to your computer and use it in GitHub Desktop.
Upload image to S3 in React + Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Amplify, { Auth, Storage } from 'aws-amplify'; | |
Amplify.configure({ | |
Auth: { | |
identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', //REQUIRED - Amazon Cognito Identity Pool ID | |
region: 'XX-XXXX-X', // REQUIRED - Amazon Cognito Region | |
userPoolId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito User Pool ID | |
userPoolWebClientId: 'XX-XXXX-X_abcd1234', //OPTIONAL - Amazon Cognito Web Client ID | |
}, | |
Storage: { | |
AWSS3: { | |
bucket: '', //REQUIRED - Amazon S3 bucket name | |
region: 'XX-XXXX-X', //OPTIONAL - Amazon service region | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [file, setFile] = useState<File>() | |
async function uploadImage() { | |
const path = "my/path/your/path" | |
const fileName = "fileName" | |
const fileExtention = file?.name.split('.').pop() | |
const key = `${path}/${fileName}.${fileExtention}` | |
return await Storage.put(key, file, { level: 'public' }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment