Skip to content

AsyncAws S3: URL-encode the CopySource argument #1862

Merged
frankdejonge merged 2 commits intothephpleague:3.xfrom
packagist:url-encode-copy-source-header
May 21, 2025
Merged

AsyncAws S3: URL-encode the CopySource argument #1862
frankdejonge merged 2 commits intothephpleague:3.xfrom
packagist:url-encode-copy-source-header

Conversation

@stevenrombauts
Copy link
Copy Markdown
Contributor

We ran into an issue moving objects that have non-ASCII characters in their name. AWS S3 expects the CopySource argument to be URL-encoded. For example:

$client = new AsyncAws\S3\S3Client([
    'accessKeyId' => '...',
    'accessKeySecret' => '...',
    'region' => 'eu-west-1',
]);

$adapter = new League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, 'urlencode-test');
$filesystem = new League\Flysystem\Filesystem($adapter);

$copyFrom = 'buıld_Çöü.json';
$filesystem->write($copyFrom, '{"foo": "bar"}');
$filesystem->copy($copyFrom, 'buıld_Çöü-copy.json');

will result in this error:

PHP Fatal error:  Uncaught AsyncAws\Core\Exception\Http\ClientException: HTTP 404 returned for "https://urlencode-test.s3.eu-west-3.amazonaws.com/bu%C4%B1ld_%C3%87%C3%B6%C3%BC-copy.json".

Code:    NoSuchKey
Message: The specified key does not exist.

URL-encoding the CopySource argument makes it work fine.

According to the CopyObject docs the x-amz-copy-source header, which is set to the value of the CopySource argument in the S3 client, must be URL-encoded.

Note: this older PR previously removed the rawurlencode() method, so maybe URL-encoding should be made configurable instead?

@frankdejonge
Copy link
Copy Markdown
Member

@stevenrombauts could you add a test case with those character? That way we can prevent a regression.

@stevenrombauts
Copy link
Copy Markdown
Contributor Author

@frankdejonge Certainly, I've added that test now.

@stevenrombauts stevenrombauts force-pushed the url-encode-copy-source-header branch from 28dfc3c to 215fab2 Compare April 14, 2025 08:06
@stevenrombauts stevenrombauts force-pushed the url-encode-copy-source-header branch from 215fab2 to 9c0e129 Compare April 14, 2025 08:06
@cfoehrdes
Copy link
Copy Markdown

We ran into the same issue today where we call copy for an S3 file with some special chars in the source path (Zwei Compañeros/some-file.jpg) and it fails with a 404 call to the source object. I applied the fix from this PR locally it also solved the issue for us.

@frankdejonge frankdejonge merged commit 3b8d678 into thephpleague:3.x May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants