-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.api.ts
More file actions
39 lines (33 loc) · 1019 Bytes
/
image.api.ts
File metadata and controls
39 lines (33 loc) · 1019 Bytes
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
import axios from 'axios';
import api from '@/api/api';
import type { ApiResponse } from '@/types/api-response';
import type { PresignedUrlInfo } from '@/types/image';
/**
* presigned URL 발급
* @param fileNames 업로드할 파일 이름 배열
* @returns presigned URL 문자열 배열
*/
const getPresignedUrls = async (fileNames: string[]): Promise<PresignedUrlInfo[]> => {
const res = await api.get<ApiResponse<PresignedUrlInfo[]>>('/images/upload-url', {
params: { file: fileNames },
paramsSerializer: { indexes: null },
});
const urls = res.data.data;
if (!urls || urls.length === 0) {
throw new Error('Presigned URL 발급에 실패했습니다.');
}
return urls;
};
/**
* S3에 파일 업로드
* @param url presigned URL
* @param file 업로드할 File 객체
*/
const uploadToS3 = async (url: string, file: File): Promise<void> => {
await axios.put(url, file, {
headers: {
'Content-Type': file.type,
},
});
};
export { getPresignedUrls, uploadToS3 };