My usecase is as follows. I want to move a file from a certain s3Location to a different s3Location, using the Java S3 SDK.
For instance, if the file is in bucket/current
, I want to move it to bucket/old
.
I currently can download a file as an S3Object, turn that object into a File (java.io, since the S3 Java client for reasons I don't understand does not allow you to upload an S3Object, the very same object you download!) and upload that file. I'm curious if there is a better approach to this.
Thanks!
We can use Files. move() API to move file from one directory to another. Following is the syntax of the move method.
You can copy an object from one bucket to another by using the AmazonS3 client's copyObject method. It takes the name of the bucket to copy from, the object to copy, and the destination bucket name. s3.
There is no direct implementation of a rename or move operation in S3. Instead, the typical solution is to copy the object to the new location and then delete the original. You can accomplish this with the AmazonS3#copyObject
and AmazonS3#deleteObject
methods of the AWS SDK for Java.
This is more efficient than the technique you described in your question of downloading the file locally and then re-uploading it under the new key. copyObject
internally makes use of S3 server-side copy provided in the S3 REST API PUT Object - Copy operation. The copy is performed on the S3 server side, so you won't have to pay the I/O costs (and real money costs if transiting out of AWS servers) compared to a local file download/upload.
Please be aware that this is much different from the rename operation as provided in a typical local file system, for multiple reasons:
You can use moveObject of the StorageService class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With