diff --git a/apps/files_external/lib/amazons3.php b/apps/files_external/lib/amazons3.php
index 38e6371f5b9e135d107fbd2a76849039bbac1ed2..d9e3ef0526552bdfed78e4fa645eae2f1c8b3906 100644
--- a/apps/files_external/lib/amazons3.php
+++ b/apps/files_external/lib/amazons3.php
@@ -511,6 +511,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
 		$path2 = $this->normalizePath($path2);
 
 		if ($this->is_file($path1)) {
+			if ($this->is_dir($path2)) {
+				$this->rmdir($path2);
+			} else if ($this->file_exists($path2)) {
+				$this->unlink($path2);
+			}
+
 			if ($this->copy($path1, $path2) === false) {
 				return false;
 			}
@@ -520,8 +526,10 @@ class AmazonS3 extends \OC\Files\Storage\Common {
 				return false;
 			}
 		} else {
-			if ($this->file_exists($path2)) {
-				return false;
+			if ($this->is_dir($path2)) {
+				$this->rmdir($path2);
+			} else if ($this->file_exists($path2)) {
+				$this->unlink($path2);
 			}
 
 			if ($this->copy($path1, $path2) === false) {