Created
March 3, 2020 08:30
-
-
Save maxoobot/4ab166364157217c79eb58717764be2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"uuid": "5b22d64f-bbb6-4456-baa0-207b3bb6a40f" | |
}, | |
"outputs": [], | |
"source": [ | |
"import oss2\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"uuid": "3a22a74c-7124-400f-ac14-e05826705870" | |
}, | |
"outputs": [], | |
"source": [ | |
"# OSSのendpoint&バケット設定\n", | |
"auth = oss2.Auth('アクセスID', 'アクセスKey') # 自分のアクセスIDとkeyを指定\n", | |
"bucket = oss2.Bucket(auth, 'http://oss-ap-southeast-1.aliyuncs.com', 'pai-demo-app')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"uuid": "da6a123e-0945-4f53-99cf-b61581004519" | |
}, | |
"outputs": [], | |
"source": [ | |
"def import_files_from_oss(bucket, local_dir):\n", | |
"\n", | |
" # retrieve oss directory and file names\n", | |
" oss_dirs = [f.key for f in oss2.ObjectIterator(bucket) if f.key.endswith('/')]\n", | |
" oss_files = [f.key for f in oss2.ObjectIterator(bucket) if not f.key.endswith('/')]\n", | |
"\n", | |
" # create directory structure\n", | |
" for d in oss_dirs:\n", | |
" directory = local_dir + d\n", | |
" if not os.path.exists(directory):\n", | |
" os.makedirs(directory)\n", | |
"\n", | |
" # copy files \n", | |
" for f in oss_files:\n", | |
" file = local_dir + f\n", | |
" bucket.get_object_to_file(f, file)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"uuid": "024434f6-162c-40c6-8737-366ee724d8ac" | |
}, | |
"outputs": [], | |
"source": [ | |
"target_dir = '/nas/' # データを保存するディレクトリー\n", | |
"import_files_from_oss(bucket, target_dir)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"uuid": "9a2fc3c0-36af-4531-9265-34b4d8d4cac6" | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment