33
loading...
This website collects cookies to deliver better user experience
npx create-react-app monitoring-storage-progress
npm install -g @aws-amplify/cli
amplify init
amplify add storage
npm install aws-amplify
index.js
before rendering the React tree:import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
aws-exports
file when you set up Amplify in case you were wondering.src/App.js
add an input element of type file
:<div>
<div>
<label htmlFor="f">
<input
type="file"
id="f"
onChange={(e) => {
uploadFile(e.target.files[0]);
}}
/>
<div>Pick a File to Upload</div>
</label>
</div>
</div>;
onChange
method calls uploadFile
and passes it the file that you picked.uploadFile
, import Storage
from the Amplify library in the src/App.js
file. You need the methods it exposes to upload and download files from AWS S3:import { Storage } from "aws-amplify";
uploadFile
function in the App
component:const [key, setKey] = React.useState("");
const uploadFile = async (file) => {
try {
const result = await Storage.put(file.name, file, {
contentType: file.type,
});
console.log(result);
setKey(result.key);
} catch (error) {
console.log(error);
}
};
uploadFile
forwards the file object to Storage.put
which takes the name of the file, the file object and a configuration object. The file name does not have to be the name of the file that you are uploading, you can use any string.Storage.put
returns a key which you can use to identify the file that was uploaded. Kinda like the unique ID for the uploaded file. I’ve set the returned key as the value of key
state because we need the key to download the uploaded file.Storage.get
function and pass it a key to the file you want to download.downloadFile
function in the onClick
event handler:{key && (
<button
onClick={() => {
downloadFile();
}}>
Download
</button>
)}
downloadFile
function in the App
component:const downloadFile = async () => {
try {
const data = await Storage.get(key, { download: true });
console.log(data);
} catch (error) {
console.log(error);
}
};
put
and get
methods on the Storage
object take a config object as one of the arguments:Storage.put(file.name, file, { /* Config */ });
Storage.get(key, { /* Config */ });
progressCallback
to the object and Amplify will call it with progress data:const progressCallback = (progress) => {
console.log(`Progress: ${progress.loaded}/${progress.total}`);
};
Storage.put(file.name, file, { progressCallback });
Storage.get(key, { progressCallback });
const progressCallback = (progress) => {
const progressInPercentage = Math.round(
(progress.loaded / progress.total) * 100
);
console.log(`Progress: ${progressInPercentage}%`);
};
const uploadFile = async (file) => {
try {
const result = await Storage.put(file.name, file, {
contentType: file.type,
// Progress callback
progressCallback,
});
console.log(result);
setKey(result.key);
} catch (error) {
console.log(error);
}
};
const downloadFile = async () => {
try {
const data = await Storage.get(key, { download: true, progressCallback /*Progress Callback*/ });
console.log(data);
} catch (error) {
console.log(error);
}
};