AWS CLI에서 상위 수준(s3) 명령 사용
이 주제에서는 AWS CLI에서 aws s3
aws s3
상위 수준 aws s3
명령은 Amazon S3 객체 관리를 간소화합니다. 이 명령을 사용하면 명령 자체 내에서와 로컬 디렉터리를 사용하여 Amazon S3의 내용을 관리할 수 있습니다.
aws s3
명령을 사용하여 Amazon S3 버킷에 대용량 객체를 업로드하면 AWS CLI에서 자동으로 멀티파트 업로드를 수행합니다. 이러한 aws s3
명령을 사용할 때는 실패한 업로드를 재개할 수 없습니다.
시간 초과로 인해 멀티파트 업로드가 실패하거나 AWS CLI에서 수동으로 취소할 경우 AWS CLI는 업로드를 중지하고 생성된 모든 파일을 정리합니다. 이 프로세스는 몇 분 정도 걸릴 수 있습니다.
kill 명령이나 시스템 오류로 인해 멀티파트 업로드 또는 정리 프로세스가 취소되면 생성된 파일은 Amazon S3 버킷에 남아 있습니다. 멀티파트 업로드를 정리하려면 s3api abort-multipart-upload
자세한 내용은 Amazon Simple Storage Service 사용 설명서에서 멀티파트 업로드 개요를 참조하세요.
사전 조건
s3
명령을 실행하려면 다음을 수행해야 합니다.
-
AWS CLI 설치. 자세한 내용은 최신 버전의 AWS CLI 설치 또는 업데이트 단원을 참조하세요.
-
AWS CLI 구성. 자세한 내용은 구성 기본 사항 단원을 참조하세요. 사용하는 프로파일에는 예제에서 수행하는 AWS 작업을 허용하는 권한이 있어야 합니다.
-
다음 Amazon S3 용어를 이해하세요.
-
버킷 - 최상위 Amazon S3 폴더입니다.
-
접두사 - 버킷의 Amazon S3 폴더입니다.
-
객체 - Amazon S3 버킷에서 호스팅되는 모든 항목입니다.
-
버킷 만들기
s3 mb
버킷 이름에는 소문자, 숫자, 하이픈, 마침표가 포함될 수 있습니다. 버킷 이름은 문자나 숫자로만 시작하고 끝날 수 있으며 하이픈이나 다른 마침표 옆에 마침표가 포함될 수 없습니다.
구문
$
aws s3 mb <target> [--options]
다음 예제에서는 s3://bucket-name
버킷을 생성합니다.
$
aws s3 mb s3://
bucket-name
버킷 및 객체 나열
버킷, 폴더 또는 객체를 나열하려면 s3 ls
구문
$
aws s3 ls <target> [--options]
이 명령과 함께 사용할 몇 가지 일반적인 옵션 및 예제는 s3 명령에 자주 사용되는 옵션 섹션을 참조하세요. 사용 가능한 옵션의 전체 목록은 AWS CLI 명령 참조에서 s3 ls
다음 예제에서는 모든 Amazon S3 버킷을 나열합니다.
$
aws s3 ls
2018-12-11 17:08:50 my-bucket 2018-12-14 14:55:44 my-bucket2
다음 명령은 버킷에 있는 모든 객체와 접두사를 나열합니다. 이 예제 출력에서 접두사 example/
에는 MyFile1.txt
라는 이름의 파일이 하나 있습니다.
$
aws s3 ls
s3://bucket-name
PRE example/ 2018-12-04 19:05:48 3 MyFile1.txt
명령에 특정 접두사를 포함하여 출력을 필터링할 수 있습니다. 다음 명령은 bucket-name/example/
에 있는 객체(즉, 접두사 example/
을 기준으로 필터링된 bucket-name
에 있는 객체)를 나열합니다.
$
aws s3 ls
s3://bucket-name/example/
2018-12-06 18:59:32 3 MyFile1.txt
버킷 삭제
버킷을 삭제하려면 s3 rb
구문
$
aws s3 rb <target> [--options]
다음 예제에서는 s3://bucket-name
버킷을 제거합니다.
$
aws s3 rb
s3://bucket-name
기본적으로 작업에 성공하려면 버킷이 비어 있어야 합니다. 비어 있지 않은 버킷을 제거하려면 --force
옵션을 포함시켜야 합니다. 이전에 삭제했지만 보관된 객체가 포함되어 있는 버전 지정된 버킷을 사용할 경우 이 명령을 사용하여 버킷을 제거할 수 없습니다. 먼저 모든 내용을 제거해야 합니다.
다음 예제에서는 버킷의 모든 객체와 접두사를 삭제한 다음 버킷을 삭제합니다.
$
aws s3 rb
s3://bucket-name
--force
객체 삭제
버킷이나 로컬 디렉터리의 객체를 삭제하려면 s3 rm
구문
$
aws s3 rm <target> [--options]
이 명령과 함께 사용할 몇 가지 일반적인 옵션 및 예제는 s3 명령에 자주 사용되는 옵션 섹션을 참조하세요. 전체 옵션 목록은 AWS CLI 명령 참조에서 s3 rm
다음 예제에서는 filename.txt
에서 s3://bucket-name/example
파일을 삭제합니다.
$
aws s3 rm s3://bucket-name/example/filename.txt --recursive
다음 예제에서는 s3://bucket-name/example
옵션을 사용하여 --recursive
에서 모든 객체를 삭제합니다.
$
aws s3 rm s3://bucket-name/example --recursive
객체 이동
s3 mv
구문
$
aws s3 mv <source> <target> [--options]
이 명령과 함께 사용할 몇 가지 일반적인 옵션 및 예제는 s3 명령에 자주 사용되는 옵션 섹션을 참조하세요. 사용 가능한 옵션의 전체 목록은 AWS CLI 명령 참조에서 s3 mv
다음 예제에서는 s3://bucket-name/example
에서 모든 객체를 s3://my-bucket/
으로 이동합니다.
$
aws s3 mv s3://bucket-name/example s3://my-bucket/
다음 예제에서는 s3 cp
명령을 사용하여 현재 작업 디렉터리에서 Amazon S3 버킷으로 로컬 파일을 이동합니다.
$
aws s3 mv filename.txt s3://bucket-name
다음 예제에서는 Amazon S3 버킷에서 현재 작업 디렉터리로 파일을 이동합니다. 여기서 ./
는 현재 작업 디렉터리를 지정합니다.
$
aws s3 mv s3://bucket-name/filename.txt ./
객체 복사
s3 cp
구문
$
aws s3 cp <source> <target> [--options]
표준 입력(stdin
) 또는 표준 출력(stdout
)으로의 파일 스트리밍을 위해 dash 파라미터를 사용할 수 있습니다.
PowerShell을 사용하는 경우 셸은 CRLF의 인코딩을 변경하거나, 파이프 입력이나 출력 또는 리디렉션된 출력에 CRLF를 추가할 수 있습니다.
s3 cp
명령은 다음 구문을 사용하여 stdin
에서 지정된 버킷으로 파일 스트림을 업로드합니다.
구문
$
aws s3 cp - <target> [--options]
s3 cp
명령은 다음 구문을 사용하여 stdout
에 대한 Amazon S3 파일 스트림을 다운로드합니다.
구문
$
aws s3 cp <target> [--options] -
이 명령과 함께 사용할 몇 가지 일반적인 옵션 및 예제는 s3 명령에 자주 사용되는 옵션 섹션을 참조하세요. 전체 옵션 목록은 AWS CLI 명령 참조에서 s3 cp
다음 예제에서는 s3://bucket-name/example
에서 s3://my-bucket/
으로 모든 객체를 복사합니다.
$
aws s3 cp s3://bucket-name/example s3://my-bucket/
다음 예제에서는 s3 cp
명령을 사용하여 현재 작업 디렉터리에서 Amazon S3 버킷으로 로컬 파일을 복사합니다.
$
aws s3 cp filename.txt s3://bucket-name
다음 예제에서는 Amazon S3 버킷에서 현재 작업 디렉터리로 파일을 복사합니다. 여기서 ./
는 현재 작업 디렉터리를 지정합니다.
$
aws s3 cp s3://bucket-name/filename.txt ./
다음 예제에서는 echo를 사용하여 "hello world" 텍스트를 s3://bucket-name/filename.txt
파일로 스트리밍합니다.
$
echo "hello world" | aws s3 cp - s3://bucket-name/filename.txt
다음 예제에서는 s3://bucket-name/filename.txt
파일을 stdout
으로 스트리밍하고 내용을 콘솔로 인쇄합니다.
$
aws s3 cp s3://bucket-name/filename.txt -
hello world
다음 예제에서는 s3://bucket-name/pre
의 내용을 stdout
으로 스트리밍하고, bzip2
명령을 사용하여 파일을 압축하고 key.bz2
라는 새 압축 파일을 s3://bucket-name
에 업로드합니다.
$
aws s3 cp s3://bucket-name/pre - | bzip2 --best | aws s3 cp - s3://bucket-name/key.bz2
객체 동기화
s3 sync
s3 sync
는 원본과 대상 간에 누락되거나 오래된 파일 또는 객체를 복사합니다. 하지만 --delete
옵션을 제공하여 원본에 없는 파일이나 객체를 대상에서 제거할 수도 있습니다.
구문
$
aws s3 sync <source> <target> [--options]
이 명령과 함께 사용할 몇 가지 일반적인 옵션 및 예제는 s3 명령에 자주 사용되는 옵션 섹션을 참조하세요. 전체 옵션 목록은 AWS CLI 명령 참조에서 s3 sync
다음 예제에서는 my-bucket이라는 버킷에 있는 path라는 Amazon S3 접두사의 내용을 현재 작업 디렉터리와 동기화합니다.
s3 sync
는 대상에 있는 동일한 이름의 파일과 크기 또는 수정 시간이 다른 모든 파일을 업데이트합니다. 출력에는 동기화 중에 수행된 특정 작업이 표시됩니다. 이 작업은 하위 디렉터리 MySubdirectory
와 해당 내용을 s3://my-bucket/path/MySubdirectory
와 반복적으로 동기화합니다.
$
aws s3 sync . s3://my-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://my-bucket/path/MySubdirectory/MyFile3.txt upload: MyFile2.txt to s3://my-bucket/path/MyFile2.txt upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt
다음 예제에서는 이전 예제를 확장하여 --delete
옵션을 사용하는 방법을 보여줍니다.
// Delete local file
$
rm ./MyFile1.txt
// Attempt sync without --delete option - nothing happens
$
aws s3 sync . s3://my-bucket/path
// Sync with deletion - object is deleted from bucket
$
aws s3 sync . s3://my-bucket/path --delete
delete: s3://my-bucket/path/MyFile1.txt // Delete object from bucket
$
aws s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://my-bucket/path/MySubdirectory/MyFile3.txt // Sync with deletion - local file is deleted
$
aws s3 sync s3://my-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt // Sync with Infrequent Access storage class
$
aws s3 sync . s3://my-bucket/path --storage-class STANDARD_IA
--delete
옵션을 사용할 때 --exclude
및 --include
옵션은 s3
sync
작업 중에 삭제할 파일 또는 객체를 필터링할 수 있습니다. 이 경우 파라미터 문자열은 대상 디렉터리 또는 버킷의 맥락에서 삭제에서 제외하거나 삭제를 위해 포함할 파일을 지정해야 합니다. 다음은 그 한 예입니다.
Assume local directory and s3://my-bucket/path currently in sync and each contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt '''
// Sync with delete, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt is not.
$
aws s3 sync . s3://my-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://my-bucket/path/MyFile88.txt '''
// Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$
aws s3 sync s3://my-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://my-bucket/path/MyFile1.txt to MyFile1.txt ''' // Sync with delete, local copy of MyFile2.rtf is deleted
$
aws s3 sync s3://my-bucket/path . --delete
delete: MyFile2.rtf
s3 명령에 자주 사용되는 옵션
다음 옵션은 이 주제에서 설명하는 명령에 자주 사용됩니다. 명령에 사용할 수 있는 옵션의 전체 목록은 AWS CLI 버전 2 참조 가이드
- acl
-
s3 sync
및s3 cp
는--acl
옵션을 사용할 수 있습니다. 이렇게 하면 Amazon S3에 복사된 파일에 대한 액세스 권한을 설정할 수 있습니다.--acl
옵션에는private
,public-read
및public-read-write
값을 적용할 수 있습니다. 자세한 내용은 Amazon Simple Storage Service 사용 설명서에서 미리 제공된 ACL을 참조하세요.$
aws s3 sync . s3://my-bucket/path --acl public-read
- exclude
-
s3 cp
,s3 mv
,s3 sync
또는s3 rm
명령을 사용하는 경우--exclude
또는--include
옵션을 사용하여 결과를 필터링할 수 있습니다.--exclude
옵션은 명령에서 객체만 제외하도록 규칙을 설정하고 옵션은 지정된 순서대로 적용됩니다. 방법은 다음 예제와 같습니다.Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt"
// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt"
// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt"
- 포함
-
s3 cp
,s3 mv
,s3 sync
또는s3 rm
명령을 사용하는 경우--exclude
또는--include
옵션을 사용하여 결과를 필터링할 수 있습니다.--include
옵션은 명령에 지정된 객체만 포함하도록 규칙을 설정하며 옵션은 지정된 순서대로 적용됩니다. 방법은 다음 예제와 같습니다.Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Include all .txt files, resulting in MyFile1.txt and MyFile88.txt being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt"
// Include all .txt files but exclude all files with the "MyFile*.txt" format, resulting in no files being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt"
// Include all .txt files, but exclude all files with the "MyFile*.txt" format, but include all files with the "MyFile?.txt" format resulting in MyFile1.txt being copied
$
aws s3 cp . s3://my-bucket/path --include "*.txt" --exclude "MyFile*.txt" --include "MyFile?.txt"
- 권한 부여
-
s3 cp
,s3 mv
및s3 sync
명령에는 지정된 사용자 또는 그룹에게 객체에 대한 권한을 부여하기 위해 사용할 수 있는--grants
옵션이 포함됩니다. 다음 구문을 사용하여--grants
옵션을 권한 목록으로 설정합니다.Permission
,Grantee_Type
및Grantee_ID
를 사용자의 값으로 바꿉니다.구문
--grants
Permission
=Grantee_Type
=Grantee_ID
[Permission
=Grantee_Type
=Grantee_ID
...]각 값에는 다음 요소가 포함됩니다.
-
Permission
- 부여된 권한을 지정합니다.read
,readacl
,writeacl
또는full
로 설정할 수 있습니다. -
Grantee_Type
- 피부여자 식별 방법을 지정합니다.uri
,emailaddress
또는id
로 설정할 수 있습니다. -
Grantee_ID
-Grantee_Type
을 기준으로 피부여자를 지정합니다.-
uri
– 그룹의 URI입니다. 자세한 내용은 피부여자란?을 참조하세요. -
emailaddress
- 계정의 이메일 주소입니다. -
id
- 계정의 정식 ID입니다.
-
Amazon S3 액세스 제어에 대한 자세한 내용은 액세스 제어를 참조하세요.
다음 예제에서는 버킷에 객체를 복사합니다. 여기서는 모든 사람에게 객체에 대한
read
권한을 부여하고full
과 연결된 계정에read
권한(readacl
,writeacl
및user@example.com
)을 부여합니다.$
aws s3 cp file.txt s3://my-bucket/ --grants
read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=emailaddress=user@example.com
Amazon S3에 업로드하는 객체에 대해 기본값이 아닌 스토리지 클래스(
REDUCED_REDUNDANCY
또는STANDARD_IA
)를 지정할 수도 있습니다. 이렇게 하려면--storage-class
옵션을 사용합니다.$
aws s3 cp file.txt s3://my-bucket/
--storage-class REDUCED_REDUNDANCY
-
- recursive
-
이 옵션을 사용하면 지정된 디렉터리 또는 접두사 아래의 모든 파일 또는 객체에 대해 명령이 수행됩니다. 다음 예제에서는
s3://my-bucket/path
및 모든 내용을 삭제합니다.$
aws s3 rm s3://my-bucket/path --recursive
리소스
AWS CLI 참조:
서비스 참조:
-
Amazon Simple Storage Service 사용 설명서의 Amazon S3 버킷 작업
-
Amazon Simple Storage Service 사용 설명서의 Amazon S3 객체 작업
-
Amazon Simple Storage Service 사용 설명서의 접두사 및 구분 기호를 사용하여 계층적 구조로 키 나열
-
Amazon Simple Storage Service 사용 설명서의 AWS SDK for .NET(낮은 수준)를 사용하여 S3 버킷에 대한 멀티파트 업로드 중단