Python SDK
    • PDF

    Python SDK

    • PDF

    Article Summary

    Classic/VPC 환경에서 이용 가능합니다.

    OpenStack에서 제공하는 Python SDK를 이용하여 네이버 클라우드 플랫폼의 Archive Storage를 사용하는 예제입니다. 이 가이드에서는 python-swiftclient 3.6.0, python-keystoneclient 3.17.0 버전을 기준으로 설명합니다.

    SDK 설치

    SDK를 설치하는 방법은 다음과 같습니다.

    pip install python-swiftclient==3.6.0
    pip install python-keystoneclient==3.17.0
    
    참고

    python-swiftclient 3.6.0 버전과 python-keystoneclient 3.17.0 버전의 소스 정보 및 관련 문서는 다음과 같습니다.

    인증

    인증 정보를 포함하고 있는 keystoneauth Session 오브젝트를 이용하여 swiftclient Connection 객체를 생성합니다. swiftclient Connection 객체는 인증 재시도 기능과 토큰 관리 기능을 제공하여 오퍼레이션을 쉽게 사용할 수 있습니다. 한 번 발급된 토큰을 재사용하므로 불필요한 인증 요청을 줄일 수 있고, 토큰이 유효하지 않거나 만료된 경우에도 토큰 재발급을 요청하여 자동으로 갱신합니다. 인증 재시도 기본 설정값은 5회입니다.

    참고

    이 가이드에서 설명하는 모든 예제는 swiftclient Connection을 이용합니다.

    예제에서 사용하는 username, password에 적용할 값은 네이버 클라우드 플랫폼 포털에서 생성한 API 인증키 정보를 사용합니다. API 인증키 정보에서 Access Key ID를 username의 값에 입력하고 Secret Key를 password의 값에 입력합니다. domain_id, project_id는 Archive Storage 화면의 [API 이용 정보 확인] 버튼을 클릭하면 나타나는 팝업 창의 정보를 사용합니다. Domain ID를 domain_id의 값에 입력하고 Project ID의 값을 project_id의 값에 에 입력합니다.

    참고
    import swiftclient
    from keystoneauth1 import session
    from keystoneauth1.identity import v3
    
    endpoint = 'https://kr.archive.ncloudstorage.com:5000/v3'
    username = 'ACCESS_KEY_ID'
    password = 'SECRET_KEY'
    domain_id = 'DOMAIN_ID'
    project_id = 'PROJECT_ID'
    
    auth = v3.Password(auth_url=endpoint, username=username, password=password, project_id=project_id, user_domain_id=domain_id)
    auth_session = session.Session(auth=auth)
    
    swift_connection = swiftclient.Connection(retries=5, session=auth_session)
    
    

    예제

    Python SDK를 이용하여 Archive Storage를 사용하는 방법을 설명합니다.

    컨테이너(버킷) 목록 조회

    컨테이너(버킷) 목록을 조회하는 예시는 다음과 같습니다.

    account = swift_connection.get_account()
    for container in account[1]:
        pprint.pprint(container)
    
    

    컨테이너(버킷) 생성

    컨테이너(버킷)을 생성하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    
    swift_connection.put_container(container_name)
    
    

    컨테이너(버킷) 삭제

    컨테이너(버킷)을 삭제하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    
    swift_connection.delete_container(container_name)
    
    

    오브젝트 업로드

    오브젝트를 업로드하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-object'
    
    content_type = 'text/plain'
    
    local_file_name = '/tmp/test.txt'
    
    with open(local_file_name) as local_file:
        swift_connection.put_object(container_name, object_name,
                                    contents=local_file.read(),
                                    content_type=content_type)
    
    

    디렉터리 오브젝트 생성

    디렉터리 오브젝트를 생성하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-directory'
    
    content_type = 'application/directory'
    
    swift_connection.put_object(container_name, object_name,
                                contents='',  # empty content
                                content_type=content_type)
    
    

    오브젝트 목록 조회

    오브젝트 목록을 조회하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    
    container = swift_connection.get_container(container_name)
    for obj in container[1]:
        pprint.pprint(obj)
    
    

    오브젝트 다운로드

    오브젝트를 다운로드하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-object'
    
    local_file_name = '/tmp/test'
    
    obj = swift_connection.get_object(container_name, object_name)
    with open(local_file_name, 'wb+') as local_file:
        local_file.write(obj[1])
    
    

    오브젝트 삭제

    오브젝트를 삭제하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-object'
    
    swift_connection.delete_object(container_name, object_name)
    
    

    대용량 파일 업로드(SLO)

    SLO 방식을 사용하여 대용량 파일을 업로드하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-big-file.mp4'
    content_type = 'video/mp4'
    
    segment_container_name = 'segment-container'
    segment_object_prefix = 'segments-for-sample-big-file.mp4/segment-'
    
    local_file_path = '/tmp/sample.mp4'
    segment_size = 10 * 1024 * 1024  # 10MB
    manifest = []
    
    with open(local_file_path, 'rb') as local_file:
        segment_number = 1
        while True:
            data = local_file.read(segment_size)
            if not len(data):
                break
    
            segment_object_name = '%s%d' % (segment_object_prefix, segment_number)
            etag = swift_connection.put_object(segment_container_name, segment_object_name, contents=data)
            manifest.append({
                "path": '%s/%s' % (segment_container_name, segment_object_name),
                "etag": etag
            })
            segment_number += 1
    
    body = json.dumps(manifest)
    
    swift_connection.put_object(container_name, object_name,
                                contents=body,
                                content_type=content_type,
                                query_string='multipart-manifest=put')
    
    

    대용량 파일 Segments 조회(SLO)

    대용량 파일 Segments를 조회하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-big-file.mp4'
    
    obj = swift_connection.get_object(container_name, object_name, query_string='multipart-manifest=get')
    pprint.pprint(json.loads(obj[1]))
    
    

    대용량 파일 삭제(SLO)

    대용량 파일을 삭제하는 예시는 다음과 같습니다.

    container_name = 'sample-container'
    object_name = 'sample-big-file.mp4'
    
    swift_connection.delete_object(container_name, object_name, query_string='multipart-manifest=delete')
    
    

    이 문서가 도움이 되었습니까?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.