Azure Blob存储中IngestionTimestamp文件夹冒号转义为%3A的解决请求
解决Azure Blob Storage中文件夹名称冒号被编码为%3A的问题
问题核心
你构造的ISO 8601格式时间戳在本地显示正常,但上传到Azure Blob Storage后,冒号被自动编码为%3A。这是因为Azure存储SDK默认会对URL保留字符进行编码,但实际上Blob存储支持路径中包含冒号,只需调整代码逻辑即可避免自动编码。
修正方案
直接构造包含冒号的完整时间戳字符串,通过SDK标准方法创建虚拟文件夹(无需使用unquote,该操作对未编码的字符串无效):
完整代码示例
from datetime import datetime, timezone from azure.storage.blob import BlobServiceClient # 1. 构造符合要求的IngestionTimestamp路径 current_timestamp = datetime.now(timezone.utc) ingestion_folder_name = f"IngestionTimestamp={current_timestamp.strftime('%Y-%m-%dT%H:%M:%S+00:00')}" # 2. 连接Azure Blob存储(替换为你的连接字符串和容器名) connection_string = "你的Azure存储连接字符串" container_name = "目标容器名称" blob_service_client = BlobServiceClient.from_connection_string(connection_string) container_client = blob_service_client.get_container_client(container_name) # 3. 创建虚拟文件夹(通过上传空Blob实现,路径以/结尾) folder_blob_path = f"{ingestion_folder_name}/" container_client.upload_blob(name=folder_blob_path, data=b"", overwrite=True)
关键说明
- 移除多余的
unquote操作:你原本构造的current_timestamp_str + '+00:00'已经是带冒号的合法字符串,unquote对未经过URL编码的字符串没有作用,完全没必要添加。 - 利用SDK标准方法:Azure Blob存储的虚拟文件夹本质是路径以
/结尾的空Blob,通过upload_blob方法直接指定带冒号的路径,SDK会正确处理路径字符,不会将冒号编码为%3A。 - 时间戳格式直接生成:通过
strftime('%Y-%m-%dT%H:%M:%S+00:00')直接生成包含时区的完整ISO 8601字符串,无需手动拼接时区部分。
内容的提问来源于stack exchange,提问作者Kramer
相关产品推荐
相关产品推荐

