디렉터리 버킷에 객체 업로드하기
디렉터리 버킷에 객체 업로드하기 (Uploading objects to a directory bucket)
Amazon S3 디렉터리 버킷을 만들고 나면 그 안에 객체를 업로드할 수 있어요. 아래 예제들은 S3 콘솔과 AWS SDK를 사용해 디렉터리 버킷에 객체를 업로드하는 방법을 보여줘요. S3 Express One Zone으로 객체를 대량 업로드하는 작업에 대한 내용은 객체 관리(Object management) 를 참고하세요.
출처: 문서
본문
디렉터리 버킷에 객체를 업로드하는 콘솔 절차부터 볼게요.
콘솔로 객체 업로드하기
- AWS Management Console에 로그인하고 https://console.aws.amazon.com/s3/ 에서 Amazon S3 콘솔을 엽니다.
- 왼쪽 탐색 창에서 Directory buckets(디렉터리 버킷)을 선택합니다.
- 폴더나 파일을 업로드할 버킷의 이름을 선택합니다.
- Objects(객체) 목록에서 Upload(업로드)를 선택합니다.
- Upload 페이지에서 다음 중 하나를 수행합니다.
- 점선으로 표시된 업로드 영역에 파일과 폴더를 끌어다 놓습니다.
- Add files(파일 추가) 또는 Add folder(폴더 추가)를 선택하고 업로드할 파일이나 폴더를 고른 뒤 Open 또는 Upload를 선택합니다.
- Checksums(체크섬) 아래에서 사용할 체크섬 함수를 선택합니다. (선택 사항) 16MB 미만의 단일 객체를 업로드하는 경우 미리 계산된 체크섬 값을 지정할 수도 있어요. 미리 계산된 값을 제공하면 Amazon S3는 선택한 체크섬 함수로 계산한 값과 비교해요. 값이 일치하지 않으면 업로드가 시작되지 않습니다.
- Permissions(권한) 및 Properties(속성) 섹션의 옵션은 기본 설정으로 자동 지정되며 수정할 수 없어요. Block Public Access는 자동으로 활성화되고, 디렉터리 버킷에서는 S3 Versioning과 S3 Object Lock을 활성화할 수 없어요. (선택 사항) 객체에 키-값 형태의 메타데이터를 추가하려면 Properties 섹션을 펼친 뒤 Metadata 섹션에서 Add metadata(메타데이터 추가)를 선택합니다.
- 나열된 파일과 폴더를 업로드하려면 Upload를 선택합니다. Amazon S3가 객체와 폴더를 업로드하고, 완료되면 Upload: status 페이지에 성공 메시지가 표시됩니다.
Java 예제 (AWS SDK for Java 2.x)
다음은 putObject로 디렉터리 버킷에 객체를 업로드하는 Java 코드예요.
public static void putObject(S3Client s3Client, String bucketName, String objectKey, Path filePath)
{
//Using File Path to avoid loading the whole file into memory
try
{
PutObjectRequest putObj = PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
//.metadata(metadata)
.build();
s3Client.putObject(putObj, filePath);
System.out.println("Successfully placed " + objectKey +" into bucket "+bucketName);
}
catch (S3Exception e)
{
System.err.println(e.getMessage());
System.exit(1);
}
}
Python 예제 (Boto3)
다음은 put_object로 디렉터리 버킷에 데이터를 업로드하는 Python 코드예요.
import boto3
import botocore
from botocore.exceptions import ClientError
def put_object(s3_client, bucket_name, key_name, object_bytes):
"""
Upload data to a directory bucket.
:param s3_client: The boto3 S3 client
:param bucket_name: The bucket that will contain the object
:param key_name: The key of the object to be uploaded
:param object_bytes: The data to upload
"""
try:
response = s3_client.put_object(Bucket=bucket_name, Key=key_name,
Body=object_bytes)
print(f"Upload object '{key_name}' to bucket '{bucket_name}'.")
return response
except ClientError:
print(f"Couldn't upload object '{key_name}' to bucket '{bucket_name}'.")
raise
def main():
# Share the client session with functions and objects to benefit from S3 Express One Zone auth key
s3_client = boto3.client('s3')
# Directory bucket name must end with --zone-id--x-s3
resp = put_object(s3_client, 'doc-bucket-example--use1-az5--x-s3', 'sample.txt', b'Hello, World!')
print(resp)
if __name__ == "__main__":
main()
AWS CLI 예제
아래 put-object 예제 명령은 AWS CLI로 객체를 업로드하는 방법을 보여줘요. 실행 시 사용자 입력 자리표시자는 직접 채워 넣어야 합니다.
aws s3api put-object --bucket bucket-base-name--zone-id--x-s3 --key sampleinut/file001.bin --body bucket-seed/file001.bin
CLI put-object 명령에 대한 자세한 내용은 AWS CLI Command Reference를 참고하세요.