-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.js
65 lines (55 loc) · 1.6 KB
/
draft.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import config from '../../_S3/S3.js';
import AWS from 'aws-sdk';
import React, { useState } from 'react';
AWS.config.update({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
});
const myBucket = new AWS.S3({
params: { Bucket: config.bucketName },
region: config.region,
});
const UploadImage = () => {
const [progress, setProgress] = useState(0);
const [selectedFile, setSelectedFile] = useState(null);
const [filePath, setFilePath] = useState();
const handleFileInput = (e) => {
setSelectedFile(e.target.files[0]);
setFilePath(URL.createObjectURL(e.target.files[0]));
};
const uploadFile = (file) => {
const params = {
ACL: 'public-read',
Body: file,
Bucket: config.bucketName,
Key: file.name,
};
myBucket
.upload(params)
.on('httpUploadProgress', (evt) => {
setProgress(Math.round((evt.loaded / evt.total) * 100));
})
.promise()
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
};
return (
<div>
<div>{progress}%</div>
<input type="file" onChange={handleFileInput} />
<button onClick={() => uploadFile(selectedFile)}>Upload to S3</button>
<img src={filePath} alt="" />
</div>
);
};
export default UploadImage;
// D:\Github\Images-Social\node_modules\react-scripts\config\webpack.config.js
// 333
// fallback: {
// path: require.resolve("path-browserify"),
// crypto: require.resolve("crypto-browserify"),
// os: require.resolve("os-browserify/browser"),
// stream: require.resolve("stream-browserify")
// }