Skip to content

Commit

Permalink
Revert "Fix GCSToGCSOperator behavior difference for moving single …
Browse files Browse the repository at this point in the history
…object (apache#40162)" (apache#40577)

This reverts commit 2f2796f.
  • Loading branch information
MaksYermak authored and romsharon98 committed Jul 26, 2024
1 parent 86e0d47 commit 454b6bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 52 deletions.
18 changes: 12 additions & 6 deletions airflow/providers/google/cloud/transfers/gcs_to_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,20 @@ def _copy_source_without_wildcard(self, hook, prefix):
msg = f"{prefix} does not exist in bucket {self.source_bucket}"
self.log.warning(msg)
raise AirflowException(msg)
if len(objects) == 1 and objects[0][-1] != "/":
self._copy_file(hook=hook, source_object=objects[0])
elif len(objects):
self._copy_multiple_objects(hook=hook, source_objects=objects, prefix=prefix)

def _copy_file(self, hook, source_object):
destination_object = self.destination_object or source_object
if self.destination_object and self.destination_object[-1] == "/":
file_name = source_object.split("/")[-1]
destination_object += file_name
self._copy_single_object(
hook=hook, source_object=source_object, destination_object=destination_object
)

def _copy_multiple_objects(self, hook, source_objects, prefix):
# Check whether the prefix is a root directory for all the rest of objects.
_pref = prefix.rstrip("/")
Expand All @@ -430,12 +441,7 @@ def _copy_multiple_objects(self, hook, source_objects, prefix):
destination_object = source_obj
else:
file_name_postfix = source_obj.replace(base_path, "", 1)

destination_object = (
self.destination_object.rstrip("/")[0 : self.destination_object.rfind("/")]
+ "/"
+ file_name_postfix
)
destination_object = self.destination_object.rstrip("/") + "/" + file_name_postfix

self._copy_single_object(
hook=hook, source_object=source_obj, destination_object=destination_object
Expand Down
46 changes: 0 additions & 46 deletions tests/providers/google/cloud/transfers/test_gcs_to_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,52 +742,6 @@ def test_execute_source_object_required_flag_true(self, mock_hook):
["source/foo.txt"],
["{prefix}/foo.txt"],
),
(
["source/sub1/sub2/sub3/file.txt"],
"source/",
None,
False,
["source/sub1/sub2/sub3/file.txt"],
["{prefix}/sub1/sub2/sub3/file.txt"],
),
(
["source/sub1/sub2/sub3/file.txt", "source/sub1/sub2/sub3/file2.txt"],
"source/",
None,
False,
["source/sub1/sub2/sub3/file.txt", "source/sub1/sub2/sub3/file2.txt"],
["{prefix}/sub1/sub2/sub3/file.txt", "{prefix}/sub1/sub2/sub3/file2.txt"],
),
(
[f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt"],
f"{DESTINATION_OBJECT_PREFIX}",
None,
False,
[f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt"],
["{prefix}/sub1/sub2/sub3/file.txt"],
),
(
[f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt"],
f"{DESTINATION_OBJECT_PREFIX}/",
None,
False,
[f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt"],
["{prefix}/sub1/sub2/sub3/file.txt"],
),
(
[
f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt",
f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file2.txt",
],
f"{DESTINATION_OBJECT_PREFIX}/",
None,
False,
[
f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file.txt",
f"{DESTINATION_OBJECT_PREFIX}/sub1/sub2/sub3/file2.txt",
],
["{prefix}/sub1/sub2/sub3/file.txt", "{prefix}/sub1/sub2/sub3/file2.txt"],
),
(
["source/foo.txt", "source/foo.txt.abc", "source/foo.txt/subfolder/file.txt"],
"source/foo.txt",
Expand Down

0 comments on commit 454b6bb

Please sign in to comment.