1. Introduction
In this tutorial, we’ll discover just how to relabel an item (data or folder) in an Amazon.com S3 container utilizing Java.
Amazon.com Simple Storage Space Solution (Amazon.com S3) is a prominent cloud storage space solution. It permits individuals to keep and also fetch information in the cloud with high sturdiness, accessibility, and also scalability. We’ll engage with it in the complying with phases utilizing the AWS SDK for Java.
2. Requirements
To relabel things in an S3 container, we can use the S3Client course given by the AWS SDK for Java.
2.1. Wizard Dependencies
First Off, we require to proclaim the AWS S3 SDK dependence in our task’s pom.xml:
<< dependence>>.
<< groupId>> software.amazon.awssdk<.
<< artifactId>> s3<.
<< variation>> 2.20.123<.
<.
2.2. AWS Qualifications
We additionally require an AWS account established, mount AWS CLI, and also configure it with our AWS qualifications ( AWS_ACCESS_KEY_ID and also AWS_SECERET_ACCESS_KEY) to be able to access the AWS sources programmatically. We can discover all the actions to achieve this in the AWS documents
2.3. Booting Up the S3 Customer
Currently, we'll develop the customer to take care of all the interaction with the S3 solution. To develop an S3 customer, we have to offer our AWS account developed with the qualifications from the previous action and also set up the AWS area:
S3Client s3Client = S3Client.builder()
. area( US_EAST_1)
. credentialsProvider( ProfileCredentialsProvider.create(" default"))
. construct();
We develop the customer utilizing the Building contractor Style Pattern. This is a Development Style Pattern that will certainly assist us develop those intricate things. In our instance, we'll develop our container in the US_EAST_1 area. We can discover all the areas in the main documents if we wish to alter our recommended area.
3. Relabeling Things Utilizing Replicate and also Remove
Presently, the only means to relabel an item utilizing the SDK is to replicate the things with a various name and afterwards remove the initial things. We'll additionally exhibit this in this area, discussing each action.
3.1. Replicate S3 Item
In this action, we'll make use of the customer developed in the previous indicate call the AWS API.
First Off, we'll specify the criteria for our demand. Allow's claim we have actually a container called baeldung-s3-bucket and also a CSV data called simpleCSVFile.csv We wish to relabel the data to renamedFile.csv Allow's start by describing the criteria for our duplicate demand:
String bucketName="baeldung-s3-bucket";
String keyName="simpleCSVFile.csv";
String destinationKeyName="renamedFile.csv";
After specifying the criteria, we can build the CopyObjectRequest that will certainly be sent out to the AWS API:
CopyObjectRequest copyObjRequest = CopyObjectRequest.builder()
. sourceBucket( bucketName)
. sourceKey( keyName)
. destinationBucket( destinationKeyName)
. destinationKey( bucketName)
. construct();
Currently we can replicate de things utilizing the AmazonS3 customer and also the demand:
s3Client.copyObject( copyRequest);
If we quit below and also run our code, we'll see that we currently have 2 documents, one with the brand-new preferred name and also the initial one.
3.2. Erasing S3 Item
We additionally require to remove the initial things after replicating to complete the relabeling procedure. We'll specify the DeleteObjectRequest with the criteria from the previous factor:
DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder()
. container( bucketName)
. trick( keyName)
. construct();
And also we'll call the S3 Customer again to remove the initial things:
s3Client.deleteObject( deleteRequest);
4. Relabeling Folders
The strategy from the previous factor just functions well for relabeling easy things. However when we need to relabel a folder, points alter a little Relabeling a whole folder in Amazon.com S3 includes repeating via all the things within the folder and also relabeling every one separately.
4.1. Checklist All Things from the Resource Folder
Allowed's beginning by providing all things from an offered folder:
ListObjectsV2Request listRequest = ListObjectsV2Request.builder()
. container( bucketName)
. prefix( sourceFolderKey)
. construct();
ListObjectsV2Response listResponse = s3Client.listObjectsV2( listRequest);
Checklist<< S3Object> > things = listResponse.contents();
We boot up the ListObjectsV2Request with the container name and also the prefix. The things in a folder are in fact all the things whose crucial prefix is the name of the particular folder.
4.2. Relabel All Item Keys from Folder
Since we have the code that notes all things from our folder, all we need to do is replicate every one of them to the brand-new location and also remove the initial things:
for (S3Object s3Object: things) {
String newKey = destinationFolderKey + s3Object.key(). substring( sourceFolderKey.length());
// Replicate challenge location folder.
CopyObjectRequest copyRequest = CopyObjectRequest.builder()
. sourceBucket( bucketName)
. sourceKey( s3Object.key())
. destinationBucket( bucketName)
. destinationKey( newKey)
. construct();.
s3Client.copyObject( copyRequest);.
// Remove things from resource folder.
DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder()
. container( bucketName)
. crucial( s3Object.key())
. construct();.
s3Client.deleteObject( deleteRequest);.
}
We begin by repeating via the listing of things, and also for every single thing, we will certainly create a brand-new trick by changing the old name of the folder with the preferred brand-new one. After obtaining the brand-new trick, all we need to do is to replicate the things at the brand-new location and also remove the initial one.
5. Verdict
In this write-up, we have actually checked out means to relabel documents and also folders in an S3 container utilizing the AWS SDK for Java. We checked out 2 various scenarios which make use of the very same idea for relabeling the things, to replicate them with a brand-new name and also remove the initial.
As constantly, the total code of this write-up is offered over on GitHub