Skip to content

Commit

Permalink
Calculate correct S3 Transfer key with or without prefix (fixes aws#653)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acoulton committed Jul 10, 2015
1 parent c5ac2e4 commit ada16a6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/S3/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ada16a6

Please sign in to comment.