-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from Strong-Potato/177-feat-add-s3-api-apply-…
…presigned-url 177 feat add s3 api apply presigned url
- Loading branch information
Showing
9 changed files
with
118 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import axios from 'axios'; | ||
|
||
import {PresignedUrlElement} from '@/types/s3'; | ||
|
||
export const s3Request = { | ||
uploadImage: async (image: FileList) => { | ||
try { | ||
// presigned url 발급 | ||
const res = await axios.post('/api/s3/presigned', [image[0].name]); | ||
const presignedUrl = await res.data.data.elements[0].preSignedUrl; | ||
console.log('api/s3/presigned response', res); | ||
|
||
// s3에 이미지 업로드 | ||
const uploadRes = await axios.put(presignedUrl, image[0], { | ||
headers: { | ||
'Content-Type': 'image/*', | ||
}, | ||
}); | ||
console.log('s3 upload response: ', uploadRes); | ||
|
||
return presignedUrl; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, | ||
|
||
uploadImages: async (images: FileList[]) => { | ||
try { | ||
// 이미지 당 presigned url 발급 | ||
const res = await axios.post( | ||
'/api/s3/presigned', | ||
images.map((image) => image[0].name), | ||
); | ||
const presignedUrls = await res.data.data.elements.map((element: PresignedUrlElement) => element.preSignedUrl); | ||
console.log('api/s3/presigned response', res); | ||
|
||
// s3에 이미지들 업로드 | ||
const uploadRes = presignedUrls.map( | ||
async (url: string, index: number) => | ||
await axios.put(url, images[index], { | ||
headers: { | ||
'Content-Type': 'image/*', | ||
}, | ||
}), | ||
); | ||
console.log('s3 upload response: ', uploadRes); | ||
|
||
return presignedUrls; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, | ||
}; |
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
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
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,4 @@ | ||
export interface PresignedUrlElement { | ||
fileName: string; | ||
preSignedUrl: string; | ||
} |