-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (35 loc) · 1.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from boxsdk import Client, OAuth2
from dotenv import load_dotenv
import os
# .envファイルから環境変数を読み込む
load_dotenv()
# OAuth2認証の設定
auth = OAuth2(
client_id=os.getenv('BOX_CLIENT_ID'),
client_secret=os.getenv('BOX_CLIENT_SECRET'),
access_token=os.getenv('BOX_ACCESS_TOKEN')
)
# Boxクライアントの初期化
client = Client(auth)
def list_folder_items(folder_id='0'): # 0はルートフォルダ
"""
指定されたフォルダ内のアイテム(フォルダとファイル)を一覧表示する
Args:
folder_id (str): 一覧を取得するフォルダのID
"""
folder = client.folder(folder_id).get()
items = client.folder(folder_id).get_items()
print(f"フォルダ名: {folder.name}")
print("----------------------------------------")
for item in items:
# アイテムのタイプ(folder/file)に基づいて表示
item_type = "📁 " if item.type == "folder" else "📄 "
print(f"{item_type}{item.name} (ID: {item.id})")
# 使用例
try:
# ルートフォルダの内容を表示
list_folder_items()
# 特定のフォルダの内容を表示する場合
# list_folder_items('SPECIFIC_FOLDER_ID')
except Exception as e:
print(f"エラーが発生しました: {e}")