Amazon S3의 데이터 통합

Red Hat OpenShift Data Science 1

AWS(Amazon Web Services) Simple Storage Service(S3) 버킷에 저장된 데이터 사용

초록

AWS(Amazon Web Services) Simple Storage Service(S3) 버킷에 저장된 데이터를 사용하는 방법을 알아봅니다.

머리말

Jupyter Notebook에서 작업할 때 AWS(Amazon Web Services) Simple Storage Service(S3) 버킷에 저장된 데이터로 작업해야 할 수 있습니다. 이 섹션에서는 Amazon S3에 저장된 데이터로 작업하는 명령 및 절차에 대해 설명합니다.

1장. 사전 요구 사항

  • Red Hat OpenShift Data Science에서 실행되는 Jupyter 서버.
  • Amazon Web Services S3 버킷에 액세스합니다.
  • Amazon S3 계정의 AWS 액세스 키 IDAWS Secret Access Key 를 찾습니다.
  • sendpyter Notebook.

2장. 랩기 셀을 사용하여 Amazon S3 클라이언트 생성

Amazon S3 버킷의 데이터와 상호 작용하려면 해당 서비스에 대한 요청을 처리할 로컬 클라이언트를 만들어야 합니다.

사전 요구 사항

  • Red Hat OpenShift Data Science에서 실행되는 Jupyter 노트북 서버에 액세스할 수 있습니다.
  • 노트북 서버를 시작할 때 My Security Credentials 아래의 Amazon Web Services 계정 값을 사용하여 AWS_ACCESS_KEY 및 AWS_SECRET_ACCESS_KEY 환경 변수의 값을 정의합니다.

절차

  1. 새 노트 셀에서 다음을 추가하여 필요한 라이브러리를 가져옵니다.

    import os
    import boto3
    from boto3 import session
  2. 다른 새로운 노트 셀에서 다음을 정의하여 세션 및 클라이언트를 만듭니다.

    1. 자격 증명을 정의합니다.

      key_id = os.environ.get('AWS_ACCESS_KEY_ID')
      secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
    2. 클라이언트 세션을 정의합니다.

      session = boto3.session.Session(aws_access_key_id=key_id, aws_secret_access_key=secret_key)
    3. 클라이언트 연결을 정의합니다.

      s3_client = boto3.client('s3', aws_access_key_id=key_id, aws_secret_access_key=secret_key)

검증

  • 새 셀을 생성하고 다음과 같은 Amazon S3 명령을 실행합니다.

    s3_client.list_buckets()

    성공적인 응답에는 다음과 유사한 버킷 목록이 200HTTPStatusCode 가 포함됩니다.

     'Buckets': [{'Name': 'my-app-asdf3-image-registry-us-east-1-wbmlcvbasdfasdgvtsmkpt',
       'CreationDate': datetime.datetime(2021, 4, 21, 6, 8, 52, tzinfo=tzlocal())},
      {'Name': 'cf-templates-18rxasdfggawsvb-us-east-1',
       'CreationDate': datetime.datetime(2021, 2, 15, 18, 35, 34, tzinfo=tzlocal())}

3장. 랩기 셀을 사용하여 사용 가능한 Amazon S3 버킷 나열

계정에서 사용할 수 있는 버킷을 나열하여 액세스할 수 있는 버킷을 확인할 수 있습니다.

사전 요구 사항

절차

  1. 새 노트 셀을 만들고 s3_client 를 사용하여 사용 가능한 버킷을 나열합니다.

    s3_client.list_buckets()
  2. 이 버킷 목록은 전체 응답이 아닌 이름 만 인쇄하여 읽을 수 있도록 할 수 있습니다. 예를 들면 다음과 같습니다.

    for bucket in s3_client.list_buckets()['Buckets']:
        print(bucket['Name'])

    그러면 다음과 유사한 출력이 반환됩니다.

    my-app-asdf3-image-registry-us-east-1-wbmlcvbasdgasdgtkpt
    cf-templates-18rxuasgasgvb-us-east-1

4장. 노트 셀을 사용하여 사용 가능한 Amazon S3 버킷의 파일 나열

버킷에 오브젝트를 나열하여 액세스할 수 있는 버킷에서 사용 가능한 파일을 확인할 수 있습니다. 버킷은 일반적인 파일 시스템이 아닌 오브젝트 스토리지를 사용하므로 오브젝트 이름 지정은 일반 파일 이름 지정과 다르게 작동합니다. 버킷의 오브젝트는 항상 키로 알려져 있으며, 이 키는 버킷에 있는 전체 경로와 파일 자체의 이름으로 구성됩니다.

사전 요구 사항

절차

  1. 새 노트 셀을 만들고 버킷에 개체를 나열합니다. 예를 들면 다음과 같습니다.

    bucket_name = 'std-user-bucket1'
    s3_client.list_objects_v2(Bucket=bucket_name)

    그러면 다음 형식으로 여러 개체가 반환됩니다.

    {'Key': 'docker/registry/v2/blobs/sha256/00/0080913dd3f10aadb34asfgsgsdgasdga072049c93606b98bec84adb259b424f/data',
    'LastModified': datetime.datetime(2021, 4, 22, 1, 26, 1, tzinfo=tzlocal()),
    'ETag': '"6e02fad2deassadfsf900a4bd7344ffe"',
    'Size': 4052,
    'StorageClass': 'STANDARD'}
  2. 전체 응답이 아닌 키만 인쇄하여 이 목록을 더 쉽게 읽을 수 있습니다. 예를 들면 다음과 같습니다.

    bucket_name = 'std-user-bucket1'
    for key in s3_client.list_objects_v2(Bucket=bucket_name)['Contents']:
        print(key['Key'])

    그러면 다음과 유사한 출력이 반환됩니다.

    docker/registry/v2/blobs/sha256/00/0080913dd3f10aadb34asfgsgsdgasdga072049c93606b98bec84adb259b424f/data
  3. 예를 들어 특정 "path" 또는 파일 이름을 나열하도록 쿼리를 필터링할 수도 있습니다.

    bucket_name = 'std-user-bucket1'
    for key in s3_client.list_objects_v2(Bucket=bucket_name,Prefix='<start_of_file_path>')['Contents']:
        print(key['Key'])

    이전 예에서 < start_of_file_path> 를 자체 값으로 바꿉니다.

5장. 노트 셀을 사용하여 사용 가능한 Amazon S3 버킷에서 파일 다운로드

download_file 방법을 사용하여 파일을 랩고 서버에 다운로드할 수 있습니다.

사전 요구 사항

절차

  1. 노트북 셀에 다음 세부 정보를 정의합니다.

    1. 파일이 들어 있는 버킷입니다. & lt;name_of_the_bucket&gt;을 고유한 값으로 바꿉니다.

      bucket_name = '<name_of_the_bucket>'
    2. 다운로드할 파일의 이름입니다. & lt;name_of_the_file_to_download&gt;를 자체 값으로 바꿉니다.

      file_name = '<name_of_the_file_to_download>' # Full path from the bucket
    3. 파일이 다운로드된 후 포함할 이름입니다. 전체 경로, 상대 경로 또는 새 파일 이름일 수 있습니다. & lt;name_of_the_file_when_downloaded&gt;를 자체 값으로 바꿉니다.

      new_file_name = '<name_of_the_file_when_downloaded>'
  2. 이전 변수를 인수로 지정하여 파일을 다운로드합니다.

    s3_client.download_file(bucket_name, file_name, new_file_name)
    참고

    read() 메서드를 사용하여 표준 파일로 스트리밍할 수 있는 오브젝트로 파일을 검색하려면 Amazon Web Services get 개체 명령 참조를 참조하십시오.

6장. 노트 셀을 사용하여 사용 가능한 Amazon S3 버킷에 파일 업로드

upload_file 방법을 사용하여 노트북 서버의 파일을 Amazon S3 버킷에 업로드할 수 있습니다.

사전 요구 사항

절차

  1. 노트북 셀에 다음 세부 정보를 정의합니다.

    1. 업로드할 파일의 이름입니다. 여기에는 파일의 전체 로컬 경로가 포함되어야 합니다. & lt;name_of_the_file_to_upload&gt;를 고유한 값으로 바꿉니다.

      file_name = '<name_of_the_file_to_upload>'
    2. 파일을 업로드할 버킷의 이름입니다. & lt;name_of_the_bucket&gt;을 고유한 값으로 바꿉니다.

      bucket_name = '<name_of_the_bucket>'
    3. 파일을 버킷에 저장하는 데 사용할 전체 키입니다. & lt;full_path_and_file_name&gt;을 고유한 값으로 바꿉니다.

      key = '<full_path_and_file_name>'
  2. 이전 변수를 인수로 지정하여 파일을 업로드합니다.

    s3_client.upload_file(file_name, bucket_name, key)

7장. 추가 리소스

법적 공지

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.