Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CopyObject fails for file starting with "/" #1241

Closed
TommyN opened this issue Apr 1, 2019 · 5 comments
Closed

CopyObject fails for file starting with "/" #1241

TommyN opened this issue Apr 1, 2019 · 5 comments
Labels
bug This issue is a bug. duplicate This issue is a duplicate. s3

Comments

@TommyN
Copy link

TommyN commented Apr 1, 2019

Copying files starting with "/" using CopyObject fails with the following message:

Message: Amazon.S3.AmazonS3Exception : The specified key does not exist.
---- Amazon.Runtime.Internal.HttpErrorResponseException : Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.

Expected Behavior

The file should copy correctly.

Note: Uploading the file with PutObjectAsync and reading the file with GetObjectRequest works as expected.

Steps to Reproduce (for bugs)

Here is a simple unit test that fails

        [Fact]
        public async Task TestS3WithFileKeyStartingWithForwardSlash()
        {
            IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.EUWest1);
            
            var bucketName = "lambda-filenametest-".ToLower() + DateTime.Now.Ticks;
            var key = "/test.xml";
            var copyKey = "copy of test.xml";
            var content = "This is a test!";

            // Create a bucket and object to setup test data.
            await s3Client.PutBucketAsync(bucketName);
            try
            {
                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = key,
                    ContentBody = content
                });

                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = key
                };


                // Read back the file content
                string data;

                using (var response = await s3Client.GetObjectAsync(request))
                using (var responseStream = response.ResponseStream)
                using (var reader = new StreamReader(responseStream))
                {
                    data = reader.ReadToEnd();
                }
                
                Assert.Equal(content, data);

                // Copy file to a new file
                CopyObjectRequest copyRequest = new CopyObjectRequest
                {
                    SourceBucket = bucketName,
                    SourceKey = key,
                    DestinationBucket = bucketName,
                    DestinationKey = copyKey
                };
                var resp = await s3Client.CopyObjectAsync(copyRequest);

                Assert.Equal(HttpStatusCode.OK, resp.HttpStatusCode);

            }
            finally
            {
                // Clean up the test data
                await AmazonS3Util.DeleteS3BucketWithObjectsAsync(s3Client, bucketName);
            }
        }

Context

We're exchanging files with a 3rd party using S3 and for some reason they have prefixed their files with "/". We're writing a Lambda function that will trigger and process each file that is uploaded to the S3 bucket, and a part of the processing is copying the file to another bucket.

Your Environment

Installing AWSSDK.Core 3.3.100.3.
Installing AWSSDK.S3 3.3.101.1.
Installing NuGet package AWSSDK.S3 3.3.101.1.

@klaytaybai klaytaybai added the bug This issue is a bug. label Apr 1, 2019
@klaytaybai
Copy link
Contributor

Thanks for reporting this

@github-actions
Copy link

We have noticed this issue has not recieved attention in a year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jul 10, 2020
@TommyN
Copy link
Author

TommyN commented Jul 10, 2020

I think this should be fixed or closed by someone other than the bot ;)

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jul 11, 2020
@mwhouser
Copy link

This problem is bigger than just a failure to copy objects starting with "/". There's 3 possible "wrong" things that can happen:

Incorrect Behaviour 1

Command: Copy "/abc" to "/def"
Objects: "/abc" exists, "abc" does not
Behaviour: Exception is thrown (original problem reported)

Incorrect Behaviour 2

Command: Copy "/abc" to "/def"
Objects: "/abc" exists, "abc" also exists
Behaviour: "abc" is copied to "def" (wrong file is copied)

Incorrect Behaviour 3

Command: Copy "/abc" to "/def"
Objects: "/abc" does not exist, "abc" exists
Behaviour: "abc" is copied to "def" (expected to throw exception)

Final Notes

So when the source key starts with a '/' character, then the wrong file will be copied.

When the destination key starts with a '/' character, then the file is copied to the wrong location, possibly destroying existing data in the destination bucket.

Since CopyObject is a tool used for Disaster Recovery, this bug needs to be addressed immediately.

@ashishdhingra ashishdhingra added the duplicate This issue is a duplicate. label Oct 15, 2020
@ashishdhingra
Copy link
Contributor

Closing as duplicate of the new issue #1720.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. duplicate This issue is a duplicate. s3
Projects
None yet
Development

No branches or pull requests

4 participants