1
1
""" s3 support for remote file interactivity """
2
+ from typing import IO , Any , Optional , Tuple
2
3
from urllib .parse import urlparse as parse_url
3
4
4
5
from pandas .compat ._optional import import_optional_dependency
5
6
7
+ from pandas ._typing import FilePathOrBuffer
8
+
6
9
s3fs = import_optional_dependency (
7
10
"s3fs" , extra = "The s3fs package is required to handle s3 files."
8
11
)
@@ -14,17 +17,17 @@ def _strip_schema(url):
14
17
return result .netloc + result .path
15
18
16
19
17
- def get_filepath_or_buffer (
18
- filepath_or_buffer , encoding = None , compression = None , mode = None
19
- ):
20
+ def get_file_and_filesystem (
21
+ filepath_or_buffer : FilePathOrBuffer , mode : Optional [ str ] = None
22
+ ) -> Tuple [ IO , Any ] :
20
23
from botocore .exceptions import NoCredentialsError
21
24
22
25
if mode is None :
23
26
mode = "rb"
24
27
25
28
fs = s3fs .S3FileSystem (anon = False )
26
29
try :
27
- filepath_or_buffer = fs .open (_strip_schema (filepath_or_buffer ), mode )
30
+ file = fs .open (_strip_schema (filepath_or_buffer ), mode )
28
31
except (FileNotFoundError , NoCredentialsError ):
29
32
# boto3 has troubles when trying to access a public file
30
33
# when credentialed...
@@ -33,5 +36,15 @@ def get_filepath_or_buffer(
33
36
# A NoCredentialsError is raised if you don't have creds
34
37
# for that bucket.
35
38
fs = s3fs .S3FileSystem (anon = True )
36
- filepath_or_buffer = fs .open (_strip_schema (filepath_or_buffer ), mode )
37
- return filepath_or_buffer , None , compression , True
39
+ file = fs .open (_strip_schema (filepath_or_buffer ), mode )
40
+ return file , fs
41
+
42
+
43
+ def get_filepath_or_buffer (
44
+ filepath_or_buffer : FilePathOrBuffer ,
45
+ encoding : Optional [str ] = None ,
46
+ compression : Optional [str ] = None ,
47
+ mode : Optional [str ] = None ,
48
+ ) -> Tuple [IO , Optional [str ], Optional [str ], bool ]:
49
+ file , _fs = get_file_and_filesystem (filepath_or_buffer , mode = mode )
50
+ return file , None , compression , True
0 commit comments