使用API从Smartsheet获取行附件保存至Databricks DBFS无输出无报错求助
问题:Smartsheet附件保存至Databricks DBFS无输出排查指导
我正在编写Python脚本,将指定Smartsheet中逐行的附件保存至Databricks DBFS的文件夹内,逻辑是按上传附件用户的邮箱创建子文件夹存储对应附件。代码执行无报错,但DBFS中无任何输出,恳请提供相关解决指导。
脚本代码
# Import required libraries import os import smartsheet # Initialize Smartsheet client SMARTSHEET_ACCESS_TOKEN = dbutils.secrets.get(scope="***********", key="smartsheetapi") smartsheet_client = smartsheet.Smartsheet(SMARTSHEET_ACCESS_TOKEN) # Get Smartsheet attachments and store in DBFS SHEET_ID = ****************** sheet = smartsheet_client.Sheets.get_sheet(SHEET_ID) # Define the master folder in FileStore of DBFS master_folder = os.path.join('/dbfs/FileStore', 'smartsheet_attachments') for row in sheet.rows: if row.attachments: for attachment in row.attachments: response = smartsheet_client.Attachments.get_attachment(SHEET_ID, attachment.id) # Create a subfolder for the user who uploaded the attachment user_folder = os.path.join(master_folder, attachment.created_by.email) if not os.path.exists(user_folder): os.makedirs(user_folder) # Save the attachment to the user's folder in DBFS with open(os.path.join(user_folder, attachment.name), 'wb') as file: file.write(response.stream.read()) # Log the user who uploaded the attachment print(f'User {attachment.created_by.email} uploaded the attachment {attachment.name}')
排查与解决步骤
确认Sheet中是否存在带附件的行
在循环前添加日志:print(f"总行数: {len(sheet.rows)}"),循环内添加:print(f"行ID {row.id} 的附件数量: {len(row.attachments)}"),验证是否真的拉取到了带附件的行数据。验证Smartsheet API权限与附件访问权限
调用smartsheet_client.Attachments.list_attachments(SHEET_ID)列出所有附件,打印返回结果,确认账号有权限访问这些附件。检查DBFS路径与权限
- 执行
print(os.path.exists(master_folder))确认主目录是否可访问,也可用dbutils.fs.ls('/FileStore')查看DBFS原生路径下的目录情况。 - 替换目录创建方式为Databricks原生API:将
os.makedirs(user_folder)改为dbutils.fs.mkdirs(user_folder.replace('/dbfs', '')),避免本地映射路径的权限问题。
- 执行
检查附件流读取有效性
在读取流后添加日志:content_length = len(response.stream.read()),print(f"附件 {attachment.name} 内容大小: {content_length}"),如果大小为0,说明API未返回有效附件内容,需检查附件状态或重新获取。改用Databricks原生API写入文件
替换open写入逻辑为dbutils.fs.put,示例:attachment_content = response.stream.read() # 转换为DBFS原生路径(去掉/dbfs前缀) dbfs_path = os.path.join('/FileStore/smartsheet_attachments', attachment.created_by.email, attachment.name) dbutils.fs.put(dbfs_path, attachment_content, overwrite=True)增强调试日志
在创建目录、写入文件等关键步骤后添加明确日志,确认每个步骤是否实际执行,比如:print(f"已创建用户目录: {user_folder}") print(f"已成功写入附件至: {dbfs_path}")
内容的提问来源于stack exchange,提问作者boomdiggity87
相关产品推荐
相关产品推荐

