Skip to content

Commit 579ba7b

Browse files
author
Phil Varner
authored
Support explicitly specifying the boto3utils3.s3 object (#92)
* Task.upload_item_assets_to_s3 and asset_io.upload_item_assets_to_s3 support explicitly specifying the boto3utils3.s3 object
1 parent 3656cbb commit 579ba7b

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Diff for: CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [unreleased] - TBD
9+
10+
### Added
11+
12+
- ([#92](https://github.com/stac-utils/stac-task/pull/92)) Task.upload_item_assets_to_s3 and asset_io.upload_item_assets_to_s3 support explicitly specifying the boto3utils3.s3 object.
13+
814
## [v0.4.1] - 2024-03-06
915

1016
### Fixed
@@ -65,7 +71,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6571

6672
Initial release.
6773

68-
<!-- [unreleased]: <https://github.com/stac-utils/stac-task/compare/v0.4.1...main>-->
74+
[unreleased]: <https://github.com/stac-utils/stac-task/compare/v0.4.1...main>
6975
[v0.4.1]: <https://github.com/stac-utils/stac-task/compare/v0.4.0...v0.4.1>
7076
[v0.4.0]: <https://github.com/stac-utils/stac-task/compare/v0.3.0...v0.4.0>
7177
[v0.3.0]: <https://github.com/stac-utils/stac-task/compare/v0.2.0...v0.3.0>

Diff for: pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dev = [
3737
"pre-commit~=3.5.0",
3838
"pytest-cov~=4.1.0",
3939
"pytest~=8.0",
40-
"ruff==0.3.0",
40+
"ruff~=0.3.1",
4141
"types-setuptools~=69.0",
4242
]
4343

@@ -53,5 +53,5 @@ strict = true
5353
module = ["boto3utils", "jsonpath_ng.ext", "fsspec"]
5454
ignore_missing_imports = true
5555

56-
[tool.ruff]
56+
[tool.ruff.lint]
5757
select = ["F", "E", "W", "I", "ERA", "RUF"]

Diff for: stactask/asset_io.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
logger = logging.getLogger(__name__)
1414

1515
# global dictionary of sessions per bucket
16-
s3_client = s3()
16+
global_s3_client = s3()
1717

1818
SIMULTANEOUS_DOWNLOADS = int(os.getenv("STAC_SIMULTANEOUS_DOWNLOADS", 3))
1919
sem = asyncio.Semaphore(SIMULTANEOUS_DOWNLOADS)
@@ -104,6 +104,7 @@ def upload_item_assets_to_s3(
104104
path_template: str = "${collection}/${id}",
105105
s3_urls: bool = False,
106106
headers: Optional[Dict[str, Any]] = None,
107+
s3_client: Optional[s3] = None,
107108
**kwargs: Any,
108109
) -> Item:
109110
"""Upload Item assets to s3 bucket
@@ -117,10 +118,16 @@ def upload_item_assets_to_s3(
117118
s3_urls (bool, optional): Return s3 URLs instead of http URLs. Defaults
118119
to False.
119120
headers (Dict, optional): Dictionary of headers to set on uploaded
120-
assets. Defaults to {},
121+
assets. Defaults to {}.
122+
s3_client (boto3utils.s3, optional): Use this s3 object instead of the default
123+
global one. Defaults to None.
121124
Returns:
122125
Dict: A new STAC Item with uploaded assets pointing to newly uploaded file URLs
123126
"""
127+
128+
if s3_client is None:
129+
s3_client = global_s3_client
130+
124131
if headers is None:
125132
headers = {}
126133

@@ -160,4 +167,5 @@ def upload_item_assets_to_s3(
160167
filename, url, public=public, extra=_headers, http_url=not s3_urls
161168
)
162169
_item["assets"][key]["href"] = url_out
170+
163171
return Item.from_dict(_item)

Diff for: stactask/task.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Any, Callable, Dict, Iterable, List, Optional, Union
1616

1717
import fsspec
18+
from boto3utils import s3
1819
from pystac import Item, ItemCollection
1920

2021
from .asset_io import (
@@ -279,12 +280,17 @@ def download_items_assets(
279280
return list(items)
280281

281282
def upload_item_assets_to_s3(
282-
self, item: Item, assets: Optional[List[str]] = None
283+
self,
284+
item: Item,
285+
assets: Optional[List[str]] = None,
286+
s3_client: Optional[s3] = None,
283287
) -> Item:
284288
if self._skip_upload:
285289
self.logger.warning("Skipping upload of new and modified assets")
286290
return item
287-
item = upload_item_assets_to_s3(item, assets=assets, **self.upload_options)
291+
item = upload_item_assets_to_s3(
292+
item=item, assets=assets, s3_client=s3_client, **self.upload_options
293+
)
288294
return item
289295

290296
# this should be in PySTAC

0 commit comments

Comments
 (0)