Skip to content

Commit 6843ca0

Browse files
authored
S3 region fix and improve hidden file check for Windows (#64)
* specify region when initializing s3 client * improve hidden file check for windows * param change * bump version * import lint
1 parent 60b97b3 commit 6843ca0

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

cirro/api/clients/s3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def __call__(self, bytes_amount):
4343

4444

4545
class S3Client:
46-
def __init__(self, creds_getter: Callable[[], Creds]):
46+
def __init__(self, creds_getter: Callable[[], Creds], region_name: str):
4747
self._creds_getter = creds_getter
48-
self._client = self._build_session_client()
48+
self._client = self._build_session_client(region_name)
4949

5050
def upload_file(self, local_path: Path, bucket: str, key: str):
5151
file_size = local_path.stat().st_size
@@ -88,7 +88,7 @@ def get_file(self, bucket: str, key: str) -> bytes:
8888
def get_file_stats(self, bucket: str, key: str):
8989
return self._client.head_object(Bucket=bucket, Key=key)
9090

91-
def _build_session_client(self):
91+
def _build_session_client(self, region_name: str):
9292
creds = self._creds_getter()
9393

9494
if creds['Expiration']:
@@ -105,7 +105,7 @@ def _build_session_client(self):
105105
aws_secret_access_key=creds['SecretAccessKey'],
106106
aws_session_token=creds['SessionToken']
107107
)
108-
return session.client('s3')
108+
return session.client('s3', region_name=region_name)
109109

110110
def _refresh_credentials(self):
111111
new_creds = self._creds_getter()

cirro/api/services/file.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_file_from_path(self, access_context: FileAccessContext, file_path: str)
3030
"""
3131
Gets the contents of a file by providing the path, used internally
3232
"""
33-
s3_client = S3Client(partial(self.get_access_credentials, access_context))
33+
s3_client = S3Client(partial(self.get_access_credentials, access_context), self._configuration.region)
3434
full_path = f'{access_context.path_prefix}/{file_path}'.lstrip('/')
3535
return s3_client.get_file(access_context.bucket, full_path)
3636

@@ -39,7 +39,7 @@ def create_file(self, access_context: FileAccessContext, key: str,
3939
"""
4040
Creates a file at the specified path
4141
"""
42-
s3_client = S3Client(partial(self.get_access_credentials, access_context))
42+
s3_client = S3Client(partial(self.get_access_credentials, access_context), self._configuration.region)
4343
s3_client.create_object(key=key,
4444
contents=contents,
4545
content_type=content_type,
@@ -53,7 +53,7 @@ def upload_files(self, access_context: FileAccessContext, directory: str, files:
5353
:param files: relative path of files to upload
5454
:return:
5555
"""
56-
s3_client = S3Client(partial(self.get_access_credentials, access_context))
56+
s3_client = S3Client(partial(self.get_access_credentials, access_context), self._configuration.region)
5757
upload_directory(directory, files, s3_client, access_context.bucket, access_context.path_prefix)
5858

5959
def download_files(self, access_context: FileAccessContext, directory: str, files: List[str]):
@@ -63,7 +63,7 @@ def download_files(self, access_context: FileAccessContext, directory: str, file
6363
:param directory: download location
6464
:param files: relative path of files to download
6565
"""
66-
s3_client = S3Client(partial(self.get_access_credentials, access_context))
66+
s3_client = S3Client(partial(self.get_access_credentials, access_context), self._configuration.region)
6767
download_directory(directory, files, s3_client, access_context.bucket, access_context.path_prefix)
6868

6969
def get_file_listing(self, access_context: FileAccessContext) -> List[File]:

cirro/file_utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from pathlib import Path, PurePath
23
from typing import List, Union
34

@@ -6,6 +7,10 @@
67
from cirro.api.clients import S3Client
78
from cirro.api.models.file import DirectoryStatistics, File
89

10+
if os.name == 'nt':
11+
import win32api
12+
import win32con
13+
914
DEFAULT_TRANSFER_SPEED = 160
1015

1116

@@ -22,6 +27,15 @@ def matches_glob(file: Union[File, str]):
2227
]
2328

2429

30+
def _is_hidden_file(file_path: Path):
31+
# Remove hidden files from listing, desktop.ini .DS_Store, etc.
32+
if os.name == 'nt':
33+
attributes = win32api.GetFileAttributes(str(file_path))
34+
return attributes & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
35+
else:
36+
return file_path.name.startswith('.')
37+
38+
2539
def get_files_in_directory(directory) -> List[str]:
2640
path = Path(directory)
2741
path_posix = str(path.as_posix())
@@ -31,7 +45,8 @@ def get_files_in_directory(directory) -> List[str]:
3145
for file_path in path.rglob("*"):
3246
if file_path.is_dir():
3347
continue
34-
if file_path.name.startswith('.'):
48+
49+
if _is_hidden_file(file_path):
3550
continue
3651

3752
str_file_path = str(file_path.as_posix())

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cirro"
3-
version = "0.6.7"
3+
version = "0.6.8"
44
description = "CLI tool and SDK for interacting with the Cirro platform"
55
authors = ["Fred Hutch <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)