-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Feature(airbyte-cdk): File Transfer implementation (#47686)
- Loading branch information
1 parent
711dfaa
commit d166bc6
Showing
19 changed files
with
622 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
airbyte-cdk/python/airbyte_cdk/models/file_transfer_record_message.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, Dict, Optional | ||
|
||
|
||
@dataclass | ||
class AirbyteFileTransferRecordMessage: | ||
stream: str | ||
file: Dict[str, Any] | ||
emitted_at: int | ||
namespace: Optional[str] = None | ||
data: Optional[Dict[str, Any]] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
airbyte-cdk/python/airbyte_cdk/sources/file_based/file_types/blob_transfer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
import logging | ||
from io import IOBase | ||
from typing import Any, Dict, Generator, Iterable, Optional, Tuple | ||
|
||
from airbyte_cdk.sources.file_based.config.file_based_stream_config import FileBasedStreamConfig | ||
from airbyte_cdk.sources.file_based.file_based_stream_reader import AbstractFileBasedStreamReader, FileReadMode | ||
from airbyte_cdk.sources.file_based.remote_file import RemoteFile | ||
from airbyte_cdk.sources.file_based.writers.local_file_client import LocalFileTransferClient | ||
|
||
|
||
class _FileReader: | ||
def read_data( | ||
self, | ||
config: FileBasedStreamConfig, | ||
file: RemoteFile, | ||
stream_reader: AbstractFileBasedStreamReader, | ||
logger: logging.Logger, | ||
file_read_mode: FileReadMode, | ||
) -> Generator[Tuple[IOBase, int], None, None]: | ||
|
||
try: | ||
file_size = stream_reader.file_size(file) | ||
with stream_reader.open_file(file, file_read_mode, "UTF-8", logger) as fp: | ||
yield fp, file_size | ||
|
||
except Exception as ex: | ||
logger.error("An error has occurred while reading file: %s", str(ex)) | ||
|
||
|
||
class BlobTransfer: | ||
def __init__(self, file_reader: Optional[_FileReader] = None): | ||
self._file_reader = file_reader if file_reader else _FileReader() | ||
|
||
def write_streams( | ||
self, | ||
config: FileBasedStreamConfig, | ||
file: RemoteFile, | ||
stream_reader: AbstractFileBasedStreamReader, | ||
logger: logging.Logger, | ||
) -> Iterable[Dict[str, Any]]: | ||
file_no = 0 | ||
try: | ||
data_generator = self._file_reader.read_data(config, file, stream_reader, logger, self.file_read_mode) | ||
local_writer = LocalFileTransferClient() | ||
for file_opened, file_size in data_generator: | ||
yield local_writer.write(file.uri, file_opened, file_size, logger) | ||
file_no += 1 | ||
except Exception as ex: | ||
logger.error("An error has occurred while writing file: %s", str(ex)) | ||
raise ex | ||
finally: | ||
data_generator.close() | ||
|
||
@property | ||
def file_read_mode(self) -> FileReadMode: | ||
return FileReadMode.READ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.