-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePresignedUrlHook.ts
More file actions
50 lines (41 loc) · 1.51 KB
/
usePresignedUrlHook.ts
File metadata and controls
50 lines (41 loc) · 1.51 KB
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
import { PresignedUrlRequest, PresignedUrlResponse } from '../model/presignedUrl';
import { axiosClient } from '../../../shared/types/api/http-client';
import axios from 'axios';
import { ApiResponse } from '../../../shared/types/api/apiResponse';
import { convertImageToWebP } from '../../../shared/lib/convertImageToWebP';
const getPresignedUrl = async (dto: PresignedUrlRequest) => {
try {
const response = await axiosClient.get<ApiResponse<PresignedUrlResponse>>('/generate-presigned-url', {
params: dto,
});
return response.data.result?.preSignedUrl;
} catch (error) {
console.error('Presigned URL 요청 실패:', error);
throw error;
}
};
export const putS3Image = async ({ url, file }: { url: string; file: File }) => {
try {
delete axiosClient.defaults.headers.common.Authorization;
await axios.put(url, file, {
headers: {
'Content-Type': 'image/webp',
},
});
} catch {
alert('이미지 업로드에 실패했습니다.');
throw new Error('Failed to upload image');
}
};
export const uploadFile = async (file: File) => {
const webFile = await convertImageToWebP(file);
const fileName = webFile.name;
const presignedUrlResponse = await getPresignedUrl({ fileName });
if (!presignedUrlResponse) {
throw new Error('Failed to get presigned url');
}
const url = presignedUrlResponse;
await putS3Image({ url, file: webFile });
// S3 URL에서 presigned URL 파라미터를 제거하고 기본 URL 반환
return url.split('?')[0];
};