使用boto3从S3 GLACIER_IR取回对象遇ObjectNotInActiveTierError求助
问题描述
我已通过以下boto3代码成功将S3对象复制到GLACIER_IR存储类:
copy_source = {'Bucket': obj['Bucket'], 'Key': obj['Key']} s3.copy(CopySource=copy_source, Bucket=bucket, Key=filename_w_timestamp, StorageClass='GLACIER_IR')
但使用类似代码取回该文件时,出现错误:
botocore.errorfactory.ObjectNotInActiveTierError: An error occurred (ObjectNotInActiveTierError) when calling the CopyObject operation: The source object of the COPY operation is not in the active tier and is only stored in Amazon Glacier.
我的取回代码如下:
s3.copy(CopySource=copy_source, Bucket=bucket, Key=filename_w_timestamp)
恳请帮忙排查问题所在。
问题原因与解决方法
原因
GLACIER_IR(Glacier即时检索)属于S3归档存储类,这类对象默认不在活跃访问层,CopyObject操作仅支持直接处理活跃层的对象,因此直接复制会触发错误。
解决步骤
发起对象取回请求
先调用restore_object将GLACIER_IR中的对象临时恢复到活跃层:s3.restore_object( Bucket=copy_source['Bucket'], Key=copy_source['Key'], RestoreRequest={ 'Days': 7, # 恢复后在活跃层保留的天数,可按需调整 'GlacierJobParameters': { 'Tier': 'Expedited' # 取回优先级:Expedited(最快,数小时)/Standard(1-2天)/Bulk(5-7天,成本最低) } } )等待取回任务完成
发起请求后需等待任务结束,可通过head_object检查恢复状态:import time while True: response = s3.head_object(Bucket=copy_source['Bucket'], Key=copy_source['Key']) if 'Restore' in response and 'ongoing-request="false"' in response['Restore']: break time.sleep(60) # 每分钟检查一次,可根据所选取回 tier 调整间隔执行复制操作
确认对象恢复完成后,再运行你的复制代码即可成功取回:s3.copy(CopySource=copy_source, Bucket=bucket, Key=filename_w_timestamp)
额外提示
- 若需长期频繁访问该对象,建议复制后将目标对象存储在活跃类(如STANDARD),避免重复取回的成本和等待时间。
- 不同取回Tier对应不同的费用和速度,可根据业务需求选择合适的选项。
内容的提问来源于stack exchange,提问作者python_enthusiast
相关产品推荐
相关产品推荐

