디지털 오션에 이미지를 업로드 하려면
cafe24같은 해외 웹호스팅 업체입니다. AWS S3같은 Storage 서비스도 제공합니다.
디지털 오션 공식 홈페이지
디지털 오션 관리자 페이지에서 Key를 발급받습니다.
Origin, Methods, Headers를 적절히 설정해 줍니다.
CORS가 궁금하다면?
npm install @aws-sdk/client-s3
const region = 'sgp1';
const config = {
endpoint: 'https://sgp1.digitaloceanspaces.com',
forcePathStyle: false,
region,
credentials: {
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY',
},
};
const s3Client = new S3Client(config);
async function handleUpload(file: any) {
const key = `${Date.now()}.${file.name}`;
const params = {
Bucket: 'vendor-apmmust',
Key: key, // 이미지의 id와 같은 역할입니다
Body: file,
ACL: 'public-read',
};
try {
const data = await s3Client.send(new PutObjectCommand(params));
...
} catch (err) {
throw new Error('이미지 업로드 실패');
}
}
이미지가 잘 올라갑니다 :D
이미지 링크는 다음과 같습니다
https://vendor-apmmust.sgp1.digitaloceanspaces.com/1678611135775.SCR-20230312-o3a.png
즉
https://{bucket}.{region}.digitaloceanspaces.com/{key}
이러한 구조입니다.
읽어주셔서 감사합니다 :D