Skip to content

Commit 2394e2f

Browse files
authored
Resolve s3 credentials wrongly defined (#19506)
1 parent c5ab348 commit 2394e2f

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

src/lightning/data/streaming/client.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from time import time
33
from typing import Any, Optional
44

5-
from lightning.data.constants import _BOTO3_AVAILABLE
5+
from lightning.data.constants import _BOTO3_AVAILABLE, _IS_IN_STUDIO
66

77
if _BOTO3_AVAILABLE:
88
import boto3
@@ -17,15 +17,14 @@ class S3Client:
1717
def __init__(self, refetch_interval: int = 3300) -> None:
1818
self._refetch_interval = refetch_interval
1919
self._last_time: Optional[float] = None
20-
self._has_cloud_space_id: bool = "LIGHTNING_CLOUD_SPACE_ID" in os.environ
2120
self._client: Optional[Any] = None
2221

2322
def _create_client(self) -> None:
2423
has_shared_credentials_file = (
2524
os.getenv("AWS_SHARED_CREDENTIALS_FILE") == os.getenv("AWS_CONFIG_FILE") == "/.credentials/.aws_credentials"
2625
)
2726

28-
if has_shared_credentials_file:
27+
if has_shared_credentials_file or not _IS_IN_STUDIO:
2928
self._client = boto3.client(
3029
"s3", config=botocore.config.Config(retries={"max_attempts": 1000, "mode": "adaptive"})
3130
)
@@ -42,10 +41,9 @@ def _create_client(self) -> None:
4241

4342
@property
4443
def client(self) -> Any:
45-
if not self._has_cloud_space_id:
46-
if self._client is None:
47-
self._create_client()
48-
return self._client
44+
if self._client is None:
45+
self._create_client()
46+
self._last_time = time()
4947

5048
# Re-generate credentials for EC2
5149
if self._last_time is None or (time() - self._last_time) > self._refetch_interval:

tests/tests_data/streaming/test_client.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ def test_s3_client_without_cloud_space_id(monkeypatch):
3030

3131

3232
@pytest.mark.skipif(sys.platform == "win32", reason="not supported on windows")
33-
@pytest.mark.parametrize("use_shared_credentials", [False, True])
33+
@pytest.mark.parametrize("use_shared_credentials", [False, True, None])
3434
def test_s3_client_with_cloud_space_id(use_shared_credentials, monkeypatch):
3535
boto3 = mock.MagicMock()
3636
monkeypatch.setattr(client, "boto3", boto3)
3737

3838
botocore = mock.MagicMock()
3939
monkeypatch.setattr(client, "botocore", botocore)
4040

41-
monkeypatch.setenv("LIGHTNING_CLOUD_SPACE_ID", "dummy")
42-
43-
if use_shared_credentials:
41+
if isinstance(use_shared_credentials, bool):
42+
monkeypatch.setenv("LIGHTNING_CLOUD_SPACE_ID", "dummy")
4443
monkeypatch.setenv("AWS_SHARED_CREDENTIALS_FILE", "/.credentials/.aws_credentials")
4544
monkeypatch.setenv("AWS_CONFIG_FILE", "/.credentials/.aws_credentials")
4645

0 commit comments

Comments
 (0)