Skip to content

[Python] pyarrow.fs.copy_files doesn't work if filenames contain special characters #33468

Closed

Description

@asfimport

I can't upload a file called spam=ham to a filesystem that emulates an S3 API. I can workaround the issue by renaming the file spam-ham. 

To reproduce, run a filesystem that emulates an S3 API:

docker run -p 9444:9000 scireum/s3-ninja:latest

Authenticate with the filesystem:

export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

 Then run this Python script:

import os
import tempfile

import pyarrow.fs

source = tempfile.mkdtemp()
file_path = os.path.join(source, "spam=ham")
open(file_path, "w").close()

filesystem, path = pyarrow.fs.FileSystem.from_uri(
"s3://bucket?scheme=http&endpoint_override=localhost:9444"
)
pyarrow.fs.copy_files(source, path, destination_filesystem=filesystem)

You'll get the error

OSError: When initiating multiple part upload for key 'spam=ham' in bucket 'bucket': AWS Error [code 22]: The computed request signature does not match the one provided. Check login credentials. (Expected: e70ab9efb620f744abd43d13e8e6846c585a41da543bfb5da67d2fe1ccfd1aaa, Found: 648456e3441dad5a014b2981c71b6e69ccac9732bdcdbe2d363d95105d914340)

This issue is motivated by ray-project/ray#29845.

Reporter: Balaji Veeramani
Assignee: Antoine Pitrou / @pitrou

PRs and other links:

Note: This issue was originally created as ARROW-18290. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Miles Granger / @milesgranger:
    Hmm, seems like one way for Arrow to fix this is to do something similar as described in this issue [aws-sdk-cpp #1224 | https://github.com/aws/aws-sdk-cpp/issues/1224], subclassing our own client which does the URI::URLEncodePath as suggested:

    I've found one thing that DOES work. I'm not suggesting that this is the fix, but it does seem to give the desired behavior, at least for this test case. In URI::GetURIString(), m_path is escaped using URLEncodePathRFC3986() which explicitly checks for and does not percent encode the "=". If this call is replaced with a similar one that does escape the "=" (and only done here. URLEncodePathRFC3986() is called in a few places), the resulting URL becomes this:

    And [the comment about a new client class | https://github.com/aws/aws-sdk-cpp/issues/1224#issuecomment-525061745] to circumvent this issue.

    What do you think, @pitrou?

  2. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Hmm... that is weird to say the least. What does the S3-emulating filesystem expect exactly? Can you upload a file "spam=ham" using their native API?

  3. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    I'll note that the GH comment quoted above seems outdated: in a recent version of the AWS SDK (1.9.259), URLEncodePathRFC3986 is still defined but isn't used anywhere. URI::GetURLEncodedPathRFC3986 is used via URI::GetURIString, though.

  4. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    I also don't know how we would override that behavior. We would have to reimplement every URI::GetURIString-calling function, such as CurlHttpClient::MakeRequest.

  5. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Miles Granger / @milesgranger:
    Ok, I suppose the main thing is this works when a real S3 bucket is used ( just tried it now). I'll experiment a bit with the native API, but even if that doesn't work, I suppose supporting 'all' s3 like systems isn't within scope.

  6. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Well, we would like our S3 support to extend to non-AWS backends, as a best effort policy, but we also can't introduce unreasonable hacks to that purpose. This particular issue seems difficult to solve without extended surgery and a hairy maintenance story.

  7. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    That said, I'm still interested to know: if you manage to create "spam=ham" using the native API, what does it look like when you list files using the S3 API?

  8. asfimport commented on Nov 14, 2022

    @asfimport
    Author

    Miles Granger / @milesgranger:
    Seems like it works fine:
    (using the same docker emulator as OP)

    In [2]: import boto3
    
    In [3]: session = boto3.session.Session()
    
    In [4]: client = session.client(service_name="s3", aws_access_key_id="AKIAIOSFODNN7EXAMPLE", a
       ...: ws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", endpoint_url="http://
       ...: localhost:9444")
    
    In [5]: with open('spam=ham', 'wb') as f:
       ...:     f.write(b'hello')
       ...:
    
    In [6]: client.upload_file('./spam=ham', 'bucket', 'spam=ham')
    
    In [7]: client.list_objects_v2(Bucket='bucket')
    Out[7]:
    {'ResponseMetadata': {'HTTPStatusCode': 200,
      'HTTPHeaders': {'transfer-encoding': 'chunked',
       'content-type': 'application/xml',
       'cache-control': 'no-cache, max-age=0',
       'last-modified': 'Mon, 14 Nov 2022 13:49:53 GMT',
       'connection': 'keep-alive',
       'server': 'e72f66e1b191 (scireum SIRIUS - powered by Netty)',
       'p3p': 'CP="This site does not have a p3p policy."',
       'vary': 'origin'},
      'RetryAttempts': 0},
     'IsTruncated': False,
     'Contents': [{'Key': 'spam=ham',
       'LastModified': datetime.datetime(2022, 11, 14, 13, 48, 54, tzinfo=tzutc()),
       'ETag': '5d41402abc4b2a76b9719d911017c592',
       'Size': 5,
       'StorageClass': 'STANDARD'}],
     'Name': 'bucket',
     'Prefix': '',
     'MaxKeys': 1000}
  9. asfimport commented on Nov 15, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Ok, it seems this should be solvable with a new enough AWS SDK.

  10. asfimport commented on Nov 15, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Issue resolved by pull request 14645
    #14645

  11. added this to the 11.0.0 milestone on Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions