airflow.providers.google.suite.hooks.drive

用于 Google Drive 服务的 Hook。

GoogleDriveHook

用于 Google Drive API 的 Hook。

模块内容

class airflow.providers.google.suite.hooks.drive.GoogleDriveHook(api_version='v3', gcp_conn_id='google_cloud_default', impersonation_chain=None)[source]

基类:airflow.providers.google.common.hooks.base_google.GoogleBaseHook

用于 Google Drive API 的 Hook。

参数::
  • api_version (str) – 使用的 API 版本(例如 v3)。

  • gcp_conn_id (str) – 获取连接信息时使用的连接 ID。

  • impersonation_chain (str | collections.abc.Sequence[str] | None) – 可选的服务账号,用于使用短期凭据进行模拟,或者通过一系列账号链获取列表中最后一个账号的 access_token,并在请求中模拟该账号。如果设置为字符串,该账号必须授予发起账号 Service Account Token Creator IAM 角色。如果设置为序列,列表中的身份必须授予直接前置身份 Service Account Token Creator IAM 角色,列表中第一个账号授予发起账号该角色。

api_version = 'v3'[source]
get_conn()[source]

检索 Google Drive 连接。

返回::

Google Drive 服务对象。

返回类型::

Any

get_media_request(file_id)[source]

返回对 Google Drive 对象的 get_media http 请求。

参数::

file_id (str) – Google Drive 文件 ID

返回::

request

返回类型::

googleapiclient.http.HttpRequest

exists(folder_id, file_name, drive_id=None, *, include_trashed=True)[source]

检查 Google Drive 文件夹中是否存在文件。

参数::
  • folder_id (str) – 文件所在的 Google Drive 文件夹的 ID

  • file_name (str) – Google Drive 中的文件名

  • drive_id (str | None) – 可选。文件所在的共享 Google Drive 的 ID。

  • include_trashed (bool) – 是否包含回收站中的对象,默认为 True,与 Google API 一致。

返回::

如果文件存在则为 True,否则为 False

返回类型::

bool

get_file_id(folder_id, file_name, drive_id=None, *, include_trashed=True)[source]

返回 Google Drive 文件的文件 ID。

参数::
  • folder_id (str) – 文件所在的 Google Drive 文件夹的 ID

  • file_name (str) – Google Drive 中的文件名

  • drive_id (str | None) – 可选。文件所在的共享 Google Drive 的 ID。

  • include_trashed (bool) – 是否包含回收站中的对象,默认为 True,与 Google API 一致。

返回::

如果文件存在,则返回 Google Drive 文件 ID,否则返回 None

返回类型::

dict

upload_file(local_location, remote_location, chunk_size=100 * 1024 * 1024, resumable=False, folder_id='root', show_full_target_path=True)[source]

将本地文件上传到 Google Drive 服务。

参数::
  • local_location (str) – 文件的本地路径。

  • remote_location (str) – 文件将被发送到的路径

  • chunk_size (int) – 文件将按此大小(字节)分块上传。仅当 resumable=True 时使用。如果要将文件作为一个整体上传,请传递 -1。请注意,Google App Engine 对请求大小有限制(5MB),因此块大小不应大于 5MB,也不应设置为 -1。

  • resumable (bool) – 如果这是可恢复上传,则为 True。False 表示一次性上传。

  • folder_id (str) – remote_location 的基础/根文件夹 ID(文件夹 Drive URL 的一部分)。

  • show_full_target_path (bool) – 如果为 True,则在日志中显示完整的可用文件路径。

返回::

文件 ID

返回类型::

str

download_file(file_id, file_handle, chunk_size=100 * 1024 * 1024)[source]

从 Google Drive 下载文件。

参数::
  • file_id (str) – 文件的 ID

  • file_handle (IO) – 用于写入内容的 文件句柄

  • chunk_size (int) – 文件将按此大小(字节)分块下载。

此条目有用吗?