-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<create>UploadImage.js에 아바타 S3 업로드 테스트까지 완료
- Loading branch information
Showing
7 changed files
with
125 additions
and
109 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//Blob 객체로 변환 | ||
function base64ToBlob(base64, mimeType) { | ||
const byteCharacters = atob(base64); | ||
const byteNumbers = new Array(byteCharacters.length); | ||
for (let i = 0; i < byteCharacters.length; i++) { | ||
byteNumbers[i] = byteCharacters.charCodeAt(i); | ||
} | ||
const byteArray = new Uint8Array(byteNumbers); | ||
return new Blob([byteArray], {type: mimeType}); | ||
} | ||
|
||
function getPresignedURL(base64Img) { | ||
let uuid = self.crypto.randomUUID(); | ||
const base64ImgValue = base64Img.replace(/^data:image\/(png|jpg);base64,/, ""); | ||
const blob = base64ToBlob(base64ImgValue, 'image/png'); | ||
console.log(blob); | ||
|
||
const formData = new FormData(); | ||
const fileName = { | ||
"imageUrl": `logo-images/${uuid}/logo.png` | ||
} | ||
formData.append("avatarImage", blob, fileName); | ||
|
||
const url = "http://localhost/api/v1/images/presigned-url"; | ||
|
||
axios | ||
.post(url, fileName, { | ||
headers: { | ||
'accept': '*/*', | ||
"Content-Type": "application/json", | ||
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjIzMTM1ODk1NTcsImlhdCI6MTcwODc4OTU1Nywicm9sZXMiOlsiUk9MRV9VU0VSIl0sIm1lbWJlcklkIjoxfQ.Eh-HoCbCCGEMltKsGX03UqGwUecj2WWkSR-GmtDNKBU' | ||
}, | ||
}) | ||
.then((res) => { | ||
const presignedUrl = res.data; | ||
uploadImageToS3(presignedUrl, blob) | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}) | ||
} | ||
|
||
async function uploadImageToS3(presignedUrl, data) { | ||
console.log("presignedURL is ", presignedUrl); | ||
console.log("data is ", data); | ||
const response = await axios.put(presignedUrl, data, { | ||
headers: { | ||
"Content-Type": "image/png" | ||
} | ||
}).then(response => { | ||
// 처리 성공 | ||
console.log(response); | ||
}) | ||
.catch(error => { | ||
// 오류 처리 | ||
console.log(error); | ||
}); | ||
} | ||
|
||
export { getPresignedURL }; |
This file was deleted.
Oops, something went wrong.
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