[Python] boto3.resource와 boto3.client 차이

오도원공육사·2021년 10월 12일
1

파이썬

목록 보기
5/11

출처.

Quickstart - Boto3 Docs 1.18.58 documentation

boto3는 Python 애플리케이션, 라이브러리 또는 스크립트에서 Amazon S3, Amazon EC2, Amazon DynamoDB 등 AWS 서비스를 쉽게 사용할 수 있도록 하는 Python 모듈이다.

boto3를 사용하는 방식은 client, resource, session 세 가지 방식이 존재한다. 이 중 가장 많이 사용하는 client와 resource 두 방식의 차이점에 대해서 알아보자.

1. Client

  • low-level 인터페이스
  • service description에 의해서 만들어진다.
  • AWS API와 일대일로 지원한다.

예시. 버킷에 존재하는 파일들의 마지막 수정 날짜 조회

import boto3

bucket_name = "mybucket"
s3_client = boto3.client("s3")
res = s3_client.list_objects(Bucket=bucket_name)

for content in res["Contents"]:
	obj_dict = s3_client.get_object(Bucket=bucket_name, Key=content["Key"])
	print(content["key"], obj_dict["LastModified"])

2. Resource

  • high-level, 객체지향적 인터페이스
  • boto3.client를 wrapping해서 구현되었다.
  • resource description에 의해 만들어진다.
  • 식별자(identifier)와 속성(attribute)를 사용한다.
  • 자원에 대한 조작 중심

예시. 위와 동일

import boto3

bucket_name = "mybucket"
s3r = boto3.resource("s3")
bucket = s3r.Bucket(bucket_name)

for obj in bucket.objects.all():
	print(obj.key, obj.last_modified)

자원에 대한 조작 중심으로 설계되었기 때문에 직관적이고 사용하기가 편하다. boto3.resourceboto3.client를 wrapping한 고수준 인터페이스이기 때문이다. 그러나 boto3.client의 모든 기능을 wrapping하지 않아서 boto3.client 혹은 boto3.resource.meta.client를 사용하여 작업해야 한다.

profile
잘 먹고 잘살기

0개의 댓글