1. IAM 사용자에게 S3FullAccess 권한을 주었다
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*",
"s3-object-lambda:*"
],
"Resource": "*"
}
]
}
2. 퍼블릭 액세스 차단을 푼 채 S3 버킷을 생성했다
3. 스프링 부트 애플리케이션에 아래와 같은 의존성을 추가하고 설정을 하였다
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.518'
@Configuration
public class AmazonS3Config {
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;
@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;
@Bean
public AmazonS3Client amazonS3Client() {
AWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client)AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.withRegion(region)
.build();
}
}
4. IAM 사용자의 권한에 S3에 쓰기 권한이 있으므로 사용자가 사진 등록을 요청하면,
쀼 백엔드 프로그램에서 업로드 할 수 있다.
5. 클라이언트에게 객체 URL을 전달하고 클라이언트는 이를 화면에 보여줬다
1. IAM 사용자가 모든 S3객체에 접근이 가능하다.
지금 버킷이 하나밖에 없긴 하지만 다른 서비스 버킷이 있는 경우 다 접근이 가능한 상태이다.
2. S3에 수많은 Get요청이 들어오며 느리다
3. 해당 객체는 인터넷 모든 사용자가 객체 URL 만 알고 있다면 접근이 가능한 상태이다.
쀼 사용자들만 접근이 가능해야 한다. (이거를 핫링크 문제라고 한다는 걸 배웠다)
1. IAM 사용자의 권한(Resource)을 축소하였다
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*",
"s3-object-lambda:*"
],
"Resource": [
"arn:aws:s3:::vvue-s3",
"arn:aws:s3:::vvue-s3/*"
]
}
]
}
2. CloudFront 도입
- S3 버킷 앞에 CloudFront 도입
CloudFront는 정적, 동적 컨텐츠를 빠르게 응답하기 위한 캐시 기능을 제공하는 CDN서비스이다.
자세한 내용은 공식문서를 찾아보자
https://docs.aws.amazon.com/ko_kr/AmazonCloudFront/latest/DeveloperGuide/Introduction.html
Amazon CloudFront란 무엇입니까? - Amazon CloudFront
Amazon CloudFront란 무엇입니까? Amazon CloudFront는 .html, .css, .js 및 이미지 파일과 같은 정적 및 동적 웹 콘텐츠를 사용자에게 더 빨리 배포하도록 지원하는 웹 서비스입니다. CloudFront는 엣지 로케이션
docs.aws.amazon.com
CloudFront와 S3를 연동해보자
1. CloudFront에서 버킷에 대한 배포를 생성한다.
2. S3 버킷 정책을 복사해서 변경하고 퍼블릭엑세스 차단을 활성화한다
{
"Version": "2008-10-17",
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::버킷명/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::숫자:distribution/문자"
}
}
}
]
}
3. (선택사항) CloudFront의 배포 > 설정으로 이동하여 대체 도메인을 등록한다
1) Request certificate를 눌러 도메인 이름을 입력한다
그러면 도메인 영역에 검증대기중이라고 뜨며
유형, CNAME이름, CNAME값이 나오는데
이 값을 입력해서 검증을 진행하면 된다
2) 가비아에서 도메인을 구입했으므로 가비아에 가서
위의 검증값과 함께 CNAME 도메인이름 클라우드프론트도메인 을 입력한다
30초도 안되어서 발급되었다고 뜨게 된다
저장 버튼을 누르면 도메인 등록이 완료되었으며 대체 도메인 이름으로도 사진에 접근이 가능하다
내가 원하는 도메인에서만 이미지에 접근이 가능하도록 변경해보자
(핫링크 방지)
다양한 방법이 있다 (아래 링크 확인)
https://aws.amazon.com/ko/blogs/security/how-to-prevent-hotlinking-by-using-aws-waf-amazon-cloudfront-and-referer-checking/
How to Prevent Hotlinking by Using AWS WAF, Amazon CloudFront, and Referer Checking | Amazon Web Services
At some point, you might have to deal with hotlinking: when third parties embed in their websites the content they find on your websites. The third-party website does not incur the cost of hosting the content, which means your website can end up paying for
aws.amazon.com
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PrivateContent.html
Serve private content with signed URLs and signed cookies - Amazon CloudFront
Thanks for letting us know this page needs work. We're sorry we let you down. If you've got a moment, please tell us how we can make the documentation better.
docs.aws.amazon.com
[쀼] S3에 업로드한 사진 : 이미지 핫링크 방지 과정 (3) (0) | 2025.02.11 |
---|---|
[쀼] S3에 업로드한 사진 : 이미지 핫링크 방지 과정 (2) (1) | 2025.02.10 |
[쀼] 카카오맵 API 사용하기 (0) | 2025.01.08 |
[쀼] 화면이 실시간으로 바뀌어야 하는 요구사항을 해결해보자 (2) : 프론트앤드 (0) | 2025.01.06 |
[쀼] 화면이 실시간으로 바뀌어야 하는 요구사항을 해결해보자 (1) 백엔드 : 다양한 방식 비교 : SSE 구현 방법, webSocket 구현 (0) | 2025.01.06 |