pyarrow.fs.AzureFileSystem#

class pyarrow.fs.AzureFileSystem(account_name, account_key=None, *, blob_storage_authority=None, blob_storage_scheme=None, client_id=None, client_secret=None, dfs_storage_authority=None, dfs_storage_scheme=None, sas_token=None, tenant_id=None)#

Bases: FileSystem

基于 Azure Blob Storage 的 FileSystem 实现

此实现支持平面命名空间和分层命名空间(HNS),也称为 Data Lake Gen2 存储账户。HNS 将被自动检测,并在提供性能优势时使用 HNS 特定功能。还支持 Azurite 模拟器。注意:/ 是唯一受支持的分隔符。

存储账户被视为文件系统的根目录。启用后,容器将在相关的目录操作期间创建或删除。显然,这也需要额外的权限进行身份验证。

默认情况下,使用 DefaultAzureCredential 进行身份验证。这意味着它将尝试几种类型的身份验证,并使用第一个成功的那种。如果在初始化 FileSystem 时提供了任何身份验证参数,它们将取代默认凭证。

参数:
account_namestr

Azure Blob Storage 账户名称。这是存储账户的全局唯一标识符。

account_keystr, default None

存储账户的账户密钥。如果 sas_token 和 account_key 都是 None,将使用默认凭证。参数 account_key 和 sas_token 互斥。

blob_storage_authoritystr, default None

Blob Service 的 hostname[:port]。默认为 .blob.core.windows.net。对于连接到本地模拟器(如 Azurite)很有用。

blob_storage_schemestr, default None

要么是 http 要么是 https。默认为 https。对于连接到本地模拟器(如 Azurite)很有用。

client_idstr, default None

用于 Azure Active Directory 身份验证的客户端 ID(应用程序 ID)。它的解释取决于所使用的凭证类型。

  • 对于 ClientSecretCredential:它是您注册的 Azure AD 应用程序(服务主体)的应用程序(客户端)ID。必须与 tenant_idclient_secret 一起提供才能使用 ClientSecretCredential。

  • 对于 ManagedIdentityCredential:它是特定用户分配的托管标识的客户端 ID。仅当您使用用户分配的托管标识并需要明确指定是哪一个时才需要此参数(例如,如果资源有多个用户分配的标识)。对于系统分配的托管标识,通常不需要此参数。

client_secretstr, default None

用于 Azure Active Directory 身份验证的客户端密钥。必须与 tenant_idclient_id 一起提供才能使用 ClientSecretCredential。

dfs_storage_authoritystr, default None

Data Lake Gen 2 Service 的 hostname[:port]。默认为 .dfs.core.windows.net。对于连接到本地模拟器(如 Azurite)很有用。

dfs_storage_schemestr, default None

要么是 http 要么是 https。默认为 https。对于连接到本地模拟器(如 Azurite)很有用。

sas_tokenstr, default None

存储账户的 SAS 令牌,用作 account_key 的替代方案。如果 sas_token 和 account_key 都是 None,将使用默认凭证。参数 account_key 和 sas_token 互斥。

tenant_idstr, default None

用于 Azure Active Directory 身份验证的租户 ID。必须与 client_idclient_secret 一起提供才能使用 ClientSecretCredential。

示例

>>> from pyarrow import fs
>>> azure_fs = fs.AzureFileSystem(account_name='myaccount')
>>> azurite_fs = fs.AzureFileSystem(
...     account_name='devstoreaccount1',
...     account_key='Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==',
...     blob_storage_authority='127.0.0.1:10000',
...     dfs_storage_authority='127.0.0.1:10000',
...     blob_storage_scheme='http',
...     dfs_storage_scheme='http',
... )

有关方法的使用,请参阅 LocalFileSystem() 的示例。

__init__(*args, **kwargs)#

方法

__init__(*args, **kwargs)

copy_file(self, src, dest)

复制文件。

create_dir(self, path, *, bool recursive=True)

创建目录及子目录。

delete_dir(self, path)

递归删除目录及其内容。

delete_dir_contents(self, path, *, ...)

递归删除目录内容。

delete_file(self, path)

删除文件。

equals(self, FileSystem other)

from_uri(uri)

从 URI 或路径创建新的 FileSystem。

get_file_info(self, paths_or_selector)

获取给定文件的信息。

move(self, src, dest)

移动/重命名文件或目录。

normalize_path(self, path)

规范化文件系统路径。

open_append_stream(self, path[, ...])

打开用于追加的输出流。

open_input_file(self, path)

打开用于随机访问读取的输入文件。

open_input_stream(self, path[, compression, ...])

打开用于顺序读取的输入流。

open_output_stream(self, path[, ...])

打开用于顺序写入的输出流。

属性

type_name

文件系统的类型名称。

copy_file(self, src, dest)#

复制文件。

如果目标已存在且是目录,则返回错误。否则,它将被替换。

参数:
srcstr

要复制的文件路径。

deststr

文件复制到的目标路径。

示例

>>> local.copy_file(path,
...                 local_path + '/pyarrow-fs-example_copy.dat')

检查文件信息

>>> local.get_file_info(local_path + '/pyarrow-fs-example_copy.dat')
<FileInfo for '/.../pyarrow-fs-example_copy.dat': type=FileType.File, size=4>
>>> local.get_file_info(path)
<FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
create_dir(self, path, *, bool recursive=True)#

创建目录及子目录。

如果目录已存在,此函数成功。

参数:
pathstr

新目录的路径。

recursivebool, default True

同时创建嵌套目录。

delete_dir(self, path)#

递归删除目录及其内容。

参数:
pathstr

要删除的目录路径。

delete_dir_contents(self, path, *, bool accept_root_dir=False, bool missing_dir_ok=False)#

递归删除目录的内容。

类似于 delete_dir,但不会删除目录本身。

参数:
pathstr

要删除的目录路径。

accept_root_dirbool, default False

允许删除根目录的内容(如果 path 为空或“/”)

missing_dir_okbool, default False

如果为 False,则如果路径不存在,将引发错误。

delete_file(self, path)#

删除文件。

参数:
pathstr

要删除的文件路径。

equals(self, FileSystem other)#
参数:
otherpyarrow.fs.FileSystem
返回:
bool
static from_uri(uri)#

从 URI 或路径创建新的 FileSystem。

可识别的 URI 方案包括“file”、“mock”、“s3fs”、“gs”、“gcs”、“hdfs”和“viewfs”。此外,参数可以是 pathlib.Path 对象,或描述绝对本地路径的字符串。

参数:
uristr

基于 URI 的路径,例如:file:///some/local/path

返回:
tuple of (FileSystem, str path)

带有 (filesystem, path) 元组,其中 path 是 FileSystem 实例内的抽象路径。

示例

从 URI 创建新的 FileSystem 子类

>>> uri = f'file:///{local_path}/pyarrow-fs-example.dat'
>>> local_new, path_new = fs.FileSystem.from_uri(uri)
>>> local_new
<pyarrow._fs.LocalFileSystem object at ...
>>> path_new
'/.../pyarrow-fs-example.dat'

或者从 s3 存储桶创建

>>> fs.FileSystem.from_uri("s3://usgs-landsat/collection02/")
(<pyarrow._s3fs.S3FileSystem object at ...>, 'usgs-landsat/collection02')

或者从 fsspec+ URI 创建

>>> fs.FileSystem.from_uri("fsspec+memory:///path/to/file")
(<pyarrow._fs.PyFileSystem object at ...>, '/path/to/file')
get_file_info(self, paths_or_selector)#

获取给定文件的信息。

任何符号链接都会自动递归解引用。不存在或不可访问的文件将返回一个 FileStat 对象,其 FileType 值为 NotFound。异常表示真正异常的情况(低级 I/O 错误等)。

参数:
paths_or_selectorFileSelector, path-like or list of path-likes

可以是选择器对象、类路径对象或类路径对象列表。选择器的基本目录不会作为结果的一部分返回,即使它存在。如果它不存在,请使用 allow_not_found

返回:
FileInfo or list of FileInfo

对于单个路径,返回单个 FileInfo 对象,否则返回 FileInfo 对象列表。

示例

>>> local
<pyarrow._fs.LocalFileSystem object at ...>
>>> local.get_file_info(f"/{local_path}/pyarrow-fs-example.dat")
<FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
move(self, src, dest)#

移动/重命名文件或目录。

如果目标存在: - 如果它是一个非空目录,则返回错误 - 否则,如果它与源具有相同类型,则替换它 - 否则,行为不确定(取决于实现)。

参数:
srcstr

要移动的文件或目录的路径。

deststr

文件或目录移动到的目标路径。

示例

创建一个包含文件的新文件夹

>>> local.create_dir('/tmp/other_dir')
>>> local.copy_file(path,'/tmp/move_example.dat')

移动文件

>>> local.move('/tmp/move_example.dat',
...            '/tmp/other_dir/move_example_2.dat')

检查文件信息

>>> local.get_file_info('/tmp/other_dir/move_example_2.dat')
<FileInfo for '/tmp/other_dir/move_example_2.dat': type=FileType.File, size=4>
>>> local.get_file_info('/tmp/move_example.dat')
<FileInfo for '/tmp/move_example.dat': type=FileType.NotFound>

删除文件夹:>>> local.delete_dir(‘/tmp/other_dir’)

normalize_path(self, path)#

规范化文件系统路径。

参数:
pathstr

要规范化的路径

返回:
normalized_pathstr

规范化的路径

open_append_stream(self, path, compression='detect', buffer_size=None, metadata=None)#

打开用于追加的输出流。

如果目标不存在,则创建新的空文件。

注意

某些文件系统实现不支持对现有文件进行高效追加,在这种情况下,此方法将引发 NotImplementedError。考虑写入多个文件(例如使用数据集层)而不是追加。

参数:
pathstr

用于写入的源。

compressionstr 可选,默认 ‘detect’

用于即时压缩的压缩算法。如果为 “detect” 且源是文件路径,则会根据文件扩展名选择压缩方式。如果为 None,则不应用压缩。否则,必须提供一个知名的算法名称(例如 “gzip”)。

buffer_sizeint optional, default None

如果为 None 或 0,则不进行缓冲。否则为临时写入缓冲区的大小。

metadatadict optional, default None

如果不是 None,则为字符串键到字符串值的映射。某些文件系统支持在文件旁存储元数据(例如“Content-Type”)。不支持的元数据键将被忽略。

返回:
streamNativeFile

示例

将新数据追加到包含非空文件的 FileSystem 子类

>>> with local.open_append_stream(path) as f:
...     f.write(b'+newly added')
12

将内容打印到文件

>>> with local.open_input_file(path) as f:
...     print(f.readall())
b'data+newly added'
open_input_file(self, path)#

打开用于随机访问读取的输入文件。

参数:
pathstr

用于读取的源。

返回:
streamNativeFile

示例

使用 open_input_file() 打印文件中的数据

>>> with local.open_input_file(path) as f:
...     print(f.readall())
b'data'
open_input_stream(self, path, compression='detect', buffer_size=None)#

打开用于顺序读取的输入流。

参数:
pathstr

用于读取的源。

compressionstr 可选,默认 ‘detect’

用于即时解压缩的压缩算法。如果为“detect”且源是文件路径,则将根据文件扩展名选择压缩。如果为None,则不应用压缩。否则,必须提供一个众所周知的算法名称(例如“gzip”)。

buffer_sizeint optional, default None

如果为None或0,则不进行缓冲。否则为临时读取缓冲区的大小。

返回:
streamNativeFile

示例

使用 open_input_stream() 打印文件中的数据

>>> with local.open_input_stream(path) as f:
...     print(f.readall())
b'data'
open_output_stream(self, path, compression='detect', buffer_size=None, metadata=None)#

打开用于顺序写入的输出流。

如果目标已存在,则截断现有数据。

参数:
pathstr

用于写入的源。

compressionstr 可选,默认 ‘detect’

用于即时压缩的压缩算法。如果为 “detect” 且源是文件路径,则会根据文件扩展名选择压缩方式。如果为 None,则不应用压缩。否则,必须提供一个知名的算法名称(例如 “gzip”)。

buffer_sizeint optional, default None

如果为 None 或 0,则不进行缓冲。否则为临时写入缓冲区的大小。

metadatadict optional, default None

如果不是 None,则为字符串键到字符串值的映射。某些文件系统支持在文件旁存储元数据(例如“Content-Type”)。不支持的元数据键将被忽略。

返回:
streamNativeFile

示例

>>> local = fs.LocalFileSystem()
>>> with local.open_output_stream(path) as stream:
...     stream.write(b'data')
4
type_name#

文件系统的类型名称。