Azure DataLake Gen2文件重命名报错:名称长度超出限制
Azure Synapse中DataLakeServiceClient重命名文件报错“资源名称长度超出允许范围”的解决方法
问题概述
在Azure Synapse笔记本中,使用DataLakeServiceClient完成文件解压后执行重命名操作时,触发HttpResponseError: (OutOfRangeInput) 资源名称长度超出允许范围错误。代码中rename_file方法执行失败,尽管变量输出显示路径、文件名均符合预期。
原代码
extraction_path = f"/synfs/{mssparkutils.env.getJobId()}/sink/{self.sink_path}" with zipfile.ZipFile(compressed_data) as zip_file: zip_file.extractall(extraction_path) dl_service_client_source = DataLakeServiceClient(f"https://{lake}.dfs.core.windows.net", credential=credential) dl_file_system_client_source = dl_service_client_source.get_file_system_client("refined") dl_directory_client_source = dl_file_system_client_source.get_file_client(self.solution_folder) files1 = (self.solution_folder + '/' + self.sink_path) files = dl_file_system_client_source.get_paths(path=files1) for file in files: if not file.is_directory: file_client = dl_file_system_client_source.get_file_client(file.name) dir_path, file_name1 = os.path.split(file.name) file_name, file_extension = os.path.splitext(file.name) new_name = 'test' new_file_path = os.path.join(dir_path, new_name) file_client.rename_file(new_file_path)
变量输出
- file_name:
IT/SpatioTemporalAssetCatalog/0_unzipped/1511111156_DodgeDeploy/Manufacturing/ORTHOMOSAIC/TKC/gmwc/TKC_Orthomosaic_export_TueAug31183858312496 - file.name:
IT/SpatioTemporalAssetCatalog/0_unzipped/1511111156_DodgeDeploy/Manufacturing/ORTHOMOSAIC/TKC/gmwc/TKC_Orthomosaic_export_TueAug31183858312496.kml - new_name:
test - new_file_path:
IT/SpatioTemporalAssetCatalog/0_unzipped/1511111156_DodgeDeploy/Manufacturing/ORTHOMOSAIC/TKC/gmwc/test
问题分析
从变量输出看,目标路径和文件名长度均远低于ADLS Gen2的限制(单个文件名≤255字符,完整路径≤1024字符),报错大概率由以下原因导致:
FileClient.rename_file对路径格式敏感,相对路径可能被错误解析;- 使用
FileClient调用重命名方法,不如直接使用FileSystemClient的方法可靠。
解决方案
1. 改用FileSystemClient的rename_file方法
直接传入源文件路径和目标路径,避免FileClient的路径解析问题。
2. 确保目标路径为绝对路径
在路径前添加/,明确指向文件系统根目录下的路径。
修改后的代码
extraction_path = f"/synfs/{mssparkutils.env.getJobId()}/sink/{self.sink_path}" with zipfile.ZipFile(compressed_data) as zip_file: zip_file.extractall(extraction_path) dl_service_client_source = DataLakeServiceClient(f"https://{lake}.dfs.core.windows.net", credential=credential) dl_file_system_client_source = dl_service_client_source.get_file_system_client("refined") # 修正原代码中错误的目录客户端初始化(用get_directory_client替代get_file_client) dl_directory_client_source = dl_file_system_client_source.get_directory_client(self.solution_folder) files1 = f"{self.solution_folder}/{self.sink_path}" files = dl_file_system_client_source.get_paths(path=files1) for file in files: if not file.is_directory: dir_path, file_name1 = os.path.split(file.name) new_name = 'test' # 生成以/开头的绝对路径,明确指向文件系统根 new_file_path = os.path.join('/', dir_path, new_name) # 使用FileSystemClient直接执行重命名 dl_file_system_client_source.rename_file(file.name, new_file_path)
额外检查项
- 打印
len(new_file_path)确认完整路径长度是否≤1024字符; - 确保目标路径未包含特殊字符或不可见字符。
内容的提问来源于stack exchange,提问作者Marilyn
相关产品推荐
相关产品推荐

