From ada16a6ba7b0941e58b392b8f76acdd48a6ecd76 Mon Sep 17 00:00:00 2001 From: Andrew Coulton Date: Fri, 10 Jul 2015 16:01:25 +0100 Subject: [PATCH 1/2] Calculate correct S3 Transfer key with or without prefix (fixes #653) Resolve an issue where S3 Transfer was attempting to upload objects with an empty key when uploading to the root of an S3 bucket. Also ensures that there is no `//` between prefix and path, to avoid uploading objects one level deeper than expected inside a directory with empty name. --- src/S3/Transfer.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/S3/Transfer.php b/src/S3/Transfer.php index 83a6cbe7af..5807803073 100644 --- a/src/S3/Transfer.php +++ b/src/S3/Transfer.php @@ -198,7 +198,7 @@ private function determineScheme($path) } /** - * Normalize a path so that it has a trailing slash. + * Normalize a path so that it has UNIX-style directory separators and no trailing / * * @param string $path * @@ -317,15 +317,16 @@ private function uploadMultipart($filename) private function createS3Key($filename) { - if (!isset($this->s3Args['Key'])) { - return ''; + $relative_file_path = ltrim( + preg_replace('#^' . preg_quote($this->source['path']) . '#', '', $filename), + '/' + ); + + if (isset($this->s3Args['Key'])) { + return rtrim($this->s3Args['Key']), '/').'/'.$relative_file_path; + } else { + return $relative_file_path; } - - $args = $this->s3Args; - $args['Key'] = rtrim($args['Key'], '/'); - $args['Key'] .= preg_replace('#^' . preg_quote($this->source['path']) . '#', '', $filename); - - return $args['Key']; } private function addDebugToBefore($debug) From 39466edcf8cb3840ef88f9139cb802378b3b8797 Mon Sep 17 00:00:00 2001 From: Andrew Coulton Date: Fri, 10 Jul 2015 16:09:18 +0100 Subject: [PATCH 2/2] Fix parse error in ada16a6 --- src/S3/Transfer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/S3/Transfer.php b/src/S3/Transfer.php index 5807803073..87ce9b9d67 100644 --- a/src/S3/Transfer.php +++ b/src/S3/Transfer.php @@ -323,7 +323,7 @@ private function createS3Key($filename) ); if (isset($this->s3Args['Key'])) { - return rtrim($this->s3Args['Key']), '/').'/'.$relative_file_path; + return rtrim($this->s3Args['Key'], '/').'/'.$relative_file_path; } else { return $relative_file_path; }