1616
1717
1818def _describe_object (
19- path : str , boto3_session : boto3 .Session , s3_additional_kwargs : Optional [Dict [str , Any ]]
19+ path : str ,
20+ boto3_session : boto3 .Session ,
21+ s3_additional_kwargs : Optional [Dict [str , Any ]],
22+ version_id : Optional [str ] = None ,
2023) -> Tuple [str , Dict [str , Any ]]:
2124 client_s3 : boto3 .client = _utils .client (service_name = "s3" , session = boto3_session )
2225 bucket : str
@@ -28,21 +31,30 @@ def _describe_object(
2831 )
2932 else :
3033 extra_kwargs = {}
31- desc : Dict [str , Any ] = _utils .try_it (
34+ desc : Dict [str , Any ]
35+ if version_id :
36+ extra_kwargs ["VersionId" ] = version_id
37+ desc = _utils .try_it (
3238 f = client_s3 .head_object , ex = client_s3 .exceptions .NoSuchKey , Bucket = bucket , Key = key , ** extra_kwargs
3339 )
3440 return path , desc
3541
3642
3743def _describe_object_concurrent (
38- path : str , boto3_primitives : _utils .Boto3PrimitivesType , s3_additional_kwargs : Optional [Dict [str , Any ]]
44+ path : str ,
45+ boto3_primitives : _utils .Boto3PrimitivesType ,
46+ s3_additional_kwargs : Optional [Dict [str , Any ]],
47+ version_id : Optional [str ] = None ,
3948) -> Tuple [str , Dict [str , Any ]]:
4049 boto3_session = _utils .boto3_from_primitives (primitives = boto3_primitives )
41- return _describe_object (path = path , boto3_session = boto3_session , s3_additional_kwargs = s3_additional_kwargs )
50+ return _describe_object (
51+ path = path , boto3_session = boto3_session , s3_additional_kwargs = s3_additional_kwargs , version_id = version_id
52+ )
4253
4354
4455def describe_objects (
4556 path : Union [str , List [str ]],
57+ version_id : Optional [Union [str , Dict [str , str ]]] = None ,
4658 use_threads : bool = True ,
4759 last_modified_begin : Optional [datetime .datetime ] = None ,
4860 last_modified_end : Optional [datetime .datetime ] = None ,
@@ -75,6 +87,9 @@ def describe_objects(
7587 path : Union[str, List[str]]
7688 S3 prefix (accepts Unix shell-style wildcards)
7789 (e.g. s3://bucket/prefix) or list of S3 objects paths (e.g. [s3://bucket/key0, s3://bucket/key1]).
90+ version_id: Optional[Union[str, Dict[str, str]]]
91+ Version id of the object or mapping of object path to version id.
92+ (e.g. {'s3://bucket/key0': '121212', 's3://bucket/key1': '343434'})
7893 use_threads : bool
7994 True to enable concurrent requests, False to disable multiple threads.
8095 If enabled os.cpu_count() will be used as the max number of threads.
@@ -116,20 +131,32 @@ def describe_objects(
116131 resp_list : List [Tuple [str , Dict [str , Any ]]]
117132 if len (paths ) == 1 :
118133 resp_list = [
119- _describe_object (path = paths [0 ], boto3_session = boto3_session , s3_additional_kwargs = s3_additional_kwargs )
134+ _describe_object (
135+ path = paths [0 ],
136+ version_id = version_id .get (paths [0 ]) if isinstance (version_id , dict ) else version_id ,
137+ boto3_session = boto3_session ,
138+ s3_additional_kwargs = s3_additional_kwargs ,
139+ )
120140 ]
121141 elif use_threads is False :
122142 resp_list = [
123- _describe_object (path = p , boto3_session = boto3_session , s3_additional_kwargs = s3_additional_kwargs )
143+ _describe_object (
144+ path = p ,
145+ version_id = version_id .get (p ) if isinstance (version_id , dict ) else version_id ,
146+ boto3_session = boto3_session ,
147+ s3_additional_kwargs = s3_additional_kwargs ,
148+ )
124149 for p in paths
125150 ]
126151 else :
127152 cpus : int = _utils .ensure_cpu_count (use_threads = use_threads )
153+ versions = [version_id .get (p ) if isinstance (version_id , dict ) else version_id for p in paths ]
128154 with concurrent .futures .ThreadPoolExecutor (max_workers = cpus ) as executor :
129155 resp_list = list (
130156 executor .map (
131157 _describe_object_concurrent ,
132158 paths ,
159+ versions ,
133160 itertools .repeat (_utils .boto3_to_primitives (boto3_session = boto3_session )),
134161 itertools .repeat (s3_additional_kwargs ),
135162 )
@@ -140,6 +167,7 @@ def describe_objects(
140167
141168def size_objects (
142169 path : Union [str , List [str ]],
170+ version_id : Optional [Union [str , Dict [str , str ]]] = None ,
143171 use_threads : bool = True ,
144172 s3_additional_kwargs : Optional [Dict [str , Any ]] = None ,
145173 boto3_session : Optional [boto3 .Session ] = None ,
@@ -162,6 +190,9 @@ def size_objects(
162190 path : Union[str, List[str]]
163191 S3 prefix (accepts Unix shell-style wildcards)
164192 (e.g. s3://bucket/prefix) or list of S3 objects paths (e.g. [s3://bucket/key0, s3://bucket/key1]).
193+ version_id: Optional[Union[str, Dict[str, str]]]
194+ Version id of the object or mapping of object path to version id.
195+ (e.g. {'s3://bucket/key0': '121212', 's3://bucket/key1': '343434'})
165196 use_threads : bool
166197 True to enable concurrent requests, False to disable multiple threads.
167198 If enabled os.cpu_count() will be used as the max number of threads.
@@ -184,7 +215,11 @@ def size_objects(
184215
185216 """
186217 desc_list : Dict [str , Dict [str , Any ]] = describe_objects (
187- path = path , use_threads = use_threads , boto3_session = boto3_session , s3_additional_kwargs = s3_additional_kwargs
218+ path = path ,
219+ version_id = version_id ,
220+ use_threads = use_threads ,
221+ boto3_session = boto3_session ,
222+ s3_additional_kwargs = s3_additional_kwargs ,
188223 )
189224 size_dict : Dict [str , Optional [int ]] = {k : d .get ("ContentLength" , None ) for k , d in desc_list .items ()}
190225 return size_dict
0 commit comments