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

使用Python从AWS S3下载指定Version-ID版本的文件

S3指定版本文件下载实现方案

你可以在调用download_file方法时,通过ExtraArgs参数传入指定的VersionId,即可下载对应版本的文件,修改后代码如下:

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket("mybucket")
# 替换为实际需要下载的文件版本ID
target_version_id = "你的目标版本ID字符串"
bucket.download_file(
    "somefile",
    "/download/path/somefile.txt",
    ExtraArgs={"VersionId": target_version_id}
)

如果你还没有获取到目标文件的版本ID,可以通过以下代码枚举该文件的所有版本信息:

# 过滤查询指定对象的所有版本
object_versions = bucket.object_versions.filter(Prefix="somefile")
for version in object_versions:
    print(f"版本ID:{version.id},最后修改时间:{version.last_modified},是否为最新版本:{version.is_latest}")

注意:执行下载操作的账号需要具备s3:GetObjectVersion权限,否则会触发权限不足报错

内容的提问来源于stack exchange,提问作者Nasser Alkhateeb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 08:24:04