Created
February 17, 2021 19:31
-
-
Save dabit3/113df99cbd3aadfa35504cf84ca30287 to your computer and use it in GitHub Desktop.
Example uploading / downloading files with S3, Amplify, & React
This file contains hidden or 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 { useState, useEffect } from 'react' | |
import { Storage } from 'aws-amplify' | |
function App() { | |
const [images, setImages] = useState([]) | |
useEffect(() => { | |
fetchImages() | |
}, []) | |
async function fetchImages() { | |
let imageKeys = await Storage.list('') | |
imageKeys = await Promise.all(imageKeys.map(async k => { | |
const key = await Storage.get(k.key) | |
return key | |
})) | |
console.log('imageKeys: ', imageKeys) | |
setImages(imageKeys) | |
} | |
async function onChange(e) { | |
const file = e.target.files[0]; | |
const result = await Storage.put(file.name, file, { | |
contentType: 'image/png' | |
}) | |
console.log({ result }) | |
fetchImages() | |
} | |
return ( | |
<div className="App"> | |
<h1>Test</h1> | |
<div style={{ display: 'flex', flexDirection: 'column' }}> | |
{ | |
images.map(image => ( | |
<img | |
src={image} | |
key={image} | |
style={{width: 500, height: 300}} | |
/> | |
)) | |
} | |
</div> | |
<input | |
type="file" | |
onChange={onChange} | |
/> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment