11""" s3 support for remote file interactivity """
2+ from typing import IO , Any , Optional , Tuple
23from urllib .parse import urlparse as parse_url
34
45from pandas .compat ._optional import import_optional_dependency
56
7+ from pandas ._typing import FilePathOrBuffer
8+
69s3fs = import_optional_dependency (
710 "s3fs" , extra = "The s3fs package is required to handle s3 files."
811)
@@ -14,17 +17,17 @@ def _strip_schema(url):
1417 return result .netloc + result .path
1518
1619
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 ] :
2023 from botocore .exceptions import NoCredentialsError
2124
2225 if mode is None :
2326 mode = "rb"
2427
2528 fs = s3fs .S3FileSystem (anon = False )
2629 try :
27- filepath_or_buffer = fs .open (_strip_schema (filepath_or_buffer ), mode )
30+ file = fs .open (_strip_schema (filepath_or_buffer ), mode )
2831 except (FileNotFoundError , NoCredentialsError ):
2932 # boto3 has troubles when trying to access a public file
3033 # when credentialed...
@@ -33,5 +36,15 @@ def get_filepath_or_buffer(
3336 # A NoCredentialsError is raised if you don't have creds
3437 # for that bucket.
3538 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