0
votes

I am trying to copy data from one s3 folder to another within same bucket. I am using copyObject function from AmazonS3 class. I don't see any errors or exceptions and I do get result also. But the file is not copied. I would at least except some error if there is any failure. What I am doing wrong? How do I know the actual error?

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); CopyObjectRequest copyObjRequest = new CopyObjectRequest( sourceURI.getBucket(), sourceURI.getKey(), destinationURI.getBucket(), destinationURI.getKey());

CopyObjectResult copyResult = s3client.copyObject(copyObjRequest);

I have proper values in source and destination URI. Is it because of credentials? If in case it is not working because of missing credentials I expect an error from this code.

1
Without seeing your code there is no way to tell. - stdunbar
I updated the question with code as well. - passionate
How are you determining that the target file is not present? - jarmod
I am able to find in S3 that folder is empty and the requested file is not moved. - passionate
Here is a demo for java from AWS. - Hearen

1 Answers

1
votes

I suspect that you have incorrectly specified the destination of the copy.

The destination is a complete bucket and key, for example if you are copying dogs/beagle.png to smalldogs/beagle.png then it is not sufficient to specify the destination key as smalldogs/. That's how copies work on a regular file system like NTFS or NFS, but not how they work in object storage. What that will result in is an object named smalldogs/ and it will appear to be a folder, but it's actually a copy of the beagle image.

So, delete the errant object that you created and then specify the destination fully and correctly.

Note that the operation here is copy. If you want 'move' then you need to delete the source afterwards.

Here is some code based on yours that runs fine for me and results in the correct copied file:

String bkt = "my-bucket";
String src = "dogs/beagle.png";
String dst = "smalldogs/beagle.png";

AmazonS3Client s3 = new AmazonS3Client();
CopyObjectRequest req = new CopyObjectRequest(bkt, src, bkt, dst);
CopyObjectResult res = s3.copyObject(req);