본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

aws CLI로 R2 사용하기

원문 보기 위키 갱신

Cloudflare R2를 S3 호환 API로 사용할 때 aws CLI로 버킷과 객체를 관리하는 방법을 소개해요. aws configure로 프로필을 만들고, 버킷·객체 목록을 조회하며 사전 서명 URL을 생성하는 예시를 다뤄요.

출처: 문서

본문

aws CLI를 설치했다면 aws configure ↗︎를 실행해서 새 프로필을 설정할 수 있어요. 새 프로필의 세부 정보를 묻는 질문이 순서대로 표시될 거예요.

aws configure
AWS Access Key ID [None]: <ACCESS_KEY_ID>
AWS Secret Access Key [None]: <SECRET_ACCESS_KEY>
Default region name [None]: auto
Default output format [None]: json

region 값은 SDK에서 필요로 하지만 R2에서는 사용하지 않으므로 auto로 설정하면 돼요.

# Provide your Cloudflare account ID
aws s3api list-buckets --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com
# {
#     "Buckets": [
#         {
#             "Name": "my-bucket",
#             "CreationDate": "2022-05-18T17:19:59.645000+00:00"
#         }
#     ],
#     "Owner": {
#         "DisplayName": "134a5a2c0ba47b38eada4b9c8ead10b6",
#         "ID": "134a5a2c0ba47b38eada4b9c8ead10b6"
#     }
# }

aws s3api list-objects-v2 --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com --bucket my-bucket
# {
#     "Contents": [
#         {
#             "Key": "ferriswasm.png",
#             "LastModified": "2022-05-18T17:20:21.670000+00:00",
#             "ETag": "\"eb2b891dc67b81755d2b726d9110af16\"",
#             "Size": 87671,
#             "StorageClass": "STANDARD"
#         }
#     ]
# }

사전 서명 URL 생성 (Generate presigned URLs)

파일을 일시적으로 공개적으로 공유할 수 있게 해주는 사전 서명 링크(presigned link)도 생성할 수 있어요.

# You can pass the --expires-in flag to determine how long the presigned link is valid.
aws s3 presign --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com  s3://my-bucket/ferriswasm.png --expires-in 3600
# https://<ACCOUNT_ID>.r2.cloudflarestorage.com/my-bucket/ferriswasm.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credential>&X-Amz-Date=<timestamp>&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature>

더 알아보기 (Learn more)