Watson Studio의 Jupyter Notebook에서 ICOS로 파일 출력

Watson Studio의 Jupyter Notebook에서 ICOS(IBM Cloud Object Storage)로 파일을 출력하는 방법의 메모입니다.
그 이외의 파일의 취급에 관해서도 정리해 보았습니다.
구체적으로는, 이하의 1~4의 조작입니다.
ICOS로의 파일 출력은 4개의 조작입니다.



1. PC에서 ICOS로 업로드



Watson Studio를 사용하는 경우에는 "Data Assets에 데이터 등록"이라는 조작이 되지만, 물리적으로는 파일은 ICOS에 놓여집니다.
프로젝트의 Assets 화면 등에서 1010 마크를 클릭하고 업로드하고 싶은 파일을 드래그 드롭 하면 됩니다.
(일본어가 포함되어 있는 경우는, 파일의 인코딩은 UTF-8로 해 주세요)



2. ICOS 파일을 Jupyter notebook에 가져오기



Jupyter Notebook을 시작하고 1010 마크를 클릭하고 가져올 파일을 "Insert pandas DataFrames"하십시오.



다음과 같은 코드가 자동으로 생성됩니다.
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share your notebook.
client_ABC = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='xxx',
    ibm_auth_endpoint="xxx",
    config=Config(signature_version='oauth'),
    endpoint_url='xxx')

body = client_ABC.get_object(Bucket='xxx',Key='potato_list.csv')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

df_data_1 = pd.read_csv(body)
df_data_1.head()

이 셀을 실행하면 파일이 Pandas의 데이터 프레임으로 채워지고 데이터 프레임의 처음 10개 행이 표시됩니다.


3. ICOS 파일을 파일 시스템으로 가져오기



다음 한 줄로 할 수 있습니다.
Bucket이나 key의 지정은, 상기의 client_ABC.get_object 의 내용을 그대로 카피해 주세요.
Filename은 파일 시스템으로 가져올 때 지정할 이름을 지정하십시오.
client_ABC.download_file(Bucket='xxx',Key='potato_list.csv',Filename='potato_list_copy1.csv')

파일 시스템에 저장된 파일은 다음과 같이 ls나 cat에서 확인, 참조할 수 있습니다.


4. 파일 시스템의 파일을 ICOS에 업로드



여기가이 기사의 메인이지만 작업은 한 줄뿐입니다.

Filename은 파일 시스템에 있는 파일을 지정합니다. key는 ICOS의 파일 이름으로 임의의 이름을 지정합니다.
client_ABC.upload_file(Filename='potato_list_copy1.csv',Bucket='xxx',Key='potato_list_copy2.csv')

실행해도 아무 것도 출력되지 않습니다.
확인하려면 프로젝트의 Assets 탭 등을 열고 1010 마크의 Files에서 확인하십시오.



이 시점에서는 Data Assets로 등록되지 않으므로 다음과 같이 함으로써 Data Assets로 등록하는 것도 가능합니다.

좋은 웹페이지 즐겨찾기