Skip to content

Commit

Permalink
finished s3 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
stevem-zhou committed Feb 2, 2024
1 parent c0f9a93 commit 8eeb198
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/components/Dropzone.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useEffect, useMemo, useState } from 'react';

import axios from 'axios';
import React, { useMemo } from 'react';
import Backend from '../utils/utils';
import { useDropzone } from 'react-dropzone';

const Dropzone = () => {
const { getRootProps, getInputProps, isDragAccept, isDragReject, acceptedFiles } = useDropzone({
noKeyboard: true,
accept: { 'image/jpeg': ['jpeg', 'jpg'], 'image/png': ['.png']},
accept: { 'image/jpeg': ['.jpeg', '.jpg'], 'image/png': ['.png'] },
maxFiles: 1,
});

Expand All @@ -17,6 +18,23 @@ const Dropzone = () => {
// }
// }, [acceptedFiles]);

const uploadImage = async (file: File) => {
// get S3 upload url from server
const { data: uploadUrl } = await Backend.get('/s3Upload');

// upload image directly to S3 bucket
await axios.put(uploadUrl, file, {
headers: {
'Content-Type': 'multipart/form-data',
},
});

const imageUrl = uploadUrl.split('?')[0];
console.log(imageUrl);

return imageUrl;
};

const dropzoneBox = useMemo(() => {
let base = 'dropzone-zone';
base += isDragAccept ? ' dropzone-accept' : '';
Expand All @@ -40,6 +58,22 @@ const Dropzone = () => {
{file.name} - {file.size} bytes
</li>
))}

<button
onClick={() => {
uploadImage(acceptedFiles[0]);
}}
disabled={!acceptedFiles}
style={{
background: 'blue',
color: 'white',
padding: '4px',
borderRadius: '5px',
margin: '4px',
}}
>
UPLOAD BUTTON
</button>
</aside>
</section>
);
Expand Down

0 comments on commit 8eeb198

Please sign in to comment.