You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从S3 Bucket下载ZIP文件时遇IsADirectoryError错误求助

解决S3下载ZIP文件时的IsADirectoryError错误

问题背景

尝试从S3 Bucket下载ZIP文件时触发IsADirectoryError,相关代码与报错信息如下:

原代码

import boto3

client = boto3.client('s3')

bucket = 'picarro-da-gis'
file = 'ReteGasBari/10082021/Incoming/20211008_ReteGasBari.zip'
filename = '/Users/vithushan/Desktop/S3-Downloads/'

# Downloading ZIP file
client.download_file(
    Bucket=bucket,
    Key=file,
    Filename=filename
)

报错信息

File "/Users/vithushan/Desktop/S3-Connect-Automation/zip_download.py", line 12, in <module>
    client.download_file(
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/boto3/s3/inject.py", line 190, in download_file
    return transfer.download_file(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/boto3/s3/transfer.py", line 326, in download_file
    future.result()
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/futures.py", line 103, in result
    return self._coordinator.result()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/futures.py", line 266, in result
    raise self._exception
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/tasks.py", line 139, in __call__
    return self._execute_main(kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/tasks.py", line 162, in _execute_main
    return_value = self._main(**kwargs)
                   ^^^^^^^^^^^^^^^^^^^^
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/download.py", line 673, in _main
    osutil.rename_file(fileobj.name, final_filename)
  File "/Users/vithushan/Desktop/S3-Connect-Automation/s3-Env/lib/python3.11/site-packages/s3transfer/utils.py", line 284, in rename_file
    rename_file(current_filename, new_filename)
IsADirectoryError: [Errno 21] Is a directory: '/Users/vithushan/Desktop/S3-Downloads/.b3aAB3c8' -> '/Users/vithushan/Desktop/S3-Downloads/'

错误原因

client.download_file的Filename参数必须指定完整的文件路径(包含文件名),而非仅目录路径。原代码中filename仅设置为目录,boto3尝试将临时下载文件重命名为目录,因此触发IsADirectoryError。

修正后的代码

可从S3文件键中提取文件名,拼接成完整本地路径:

import boto3
import os

client = boto3.client('s3')

bucket = 'picarro-da-gis'
s3_file_key = 'ReteGasBari/10082021/Incoming/20211008_ReteGasBari.zip'
# 从S3键中提取原始文件名
file_name = os.path.basename(s3_file_key)
# 拼接完整本地文件路径
local_file_path = f'/Users/vithushan/Desktop/S3-Downloads/{file_name}'

# 下载ZIP文件
client.download_file(
    Bucket=bucket,
    Key=s3_file_key,
    Filename=local_file_path
)

额外说明

  • 若需自定义文件名,直接修改local_file_path中的文件名即可,例如local_file_path = '/Users/vithushan/Desktop/S3-Downloads/custom_name.zip'
  • 确保目标目录已存在,若不存在可提前用os.makedirs('/Users/vithushan/Desktop/S3-Downloads/', exist_ok=True)创建

内容的提问来源于stack exchange,提问作者Vithushan Logeswaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 03:05:53