Airflow LocalFilesystemToGCSOperator标记任务成功,但未上传文件

问题描述

我正在尝试将文件从本地计算机上传到GCS,并且正在使用LocalFilesystemToGCSOperator。我正在遵循此方法https://airflow.readthedocs.io/en/latest/howto/operator/google/transfer/local_to_gcs.html#prerequisite-tasks。我已经建立了到GCP的连接,并带有指向json文件的路径。这是DAG代码

import os

from airflow import models
from airflow.providers.google.cloud.transfers.local_to_gcs import LocalFilesystemToGCSOperator
from airflow.utils import dates

BUCKET_NAME = 'bucket-name'
PATH_TO_UPLOAD_FILE = '...path-to/airflow/dags/example-text.txt'
DESTINATION_FILE_LOCATION = '/test-dir-input/example-text.txt'

with models.DAG(
    'example_local_to_gcs',default_args=dict(start_date=dates.days_ago(1)),schedule_interval=None,) as dag:
    upload_file = LocalFilesystemToGCSOperator(
        gcp_conn_id='custom_gcp_connection',task_id="upload_file",src=PATH_TO_UPLOAD_FILE,dst=DESTINATION_FILE_LOCATION,bucket=BUCKET_NAME,mime_type='text/plain'
    )

当我触发DAG时,它被标记为成功,但是文件不在存储桶中

解决方法

您的path_to_uploaddestination_file_location好像有问题。

下面的separate post可以为您提供帮助,它可以为您提供帮助。例如,这样声明与您类似的相关参数:

src='/Users/john/Documents/tmp',dst='gs://constantine-bucket',bucket='constantine-bucket',

您应删除...,并确保destination_file_location引用您的存储桶名称或其中的文件夹,如下所示:

BUCKET_NAME = 'bucket-name'
PATH_TO_UPLOAD_FILE = '/path-to/airflow/dags/example-text.txt'
DESTINATION_FILE_LOCATION = 'gs://bucket-name/example-text.txt'

# Or in a folder on your bucket
# DESTINATION_FILE_LOCATION = 'gs://bucket-name/folder/example-text.txt'