How to rename a file in Amazon S3 Bucket?

I am trying to rename a file in S3 Bucket. Here is a snippet of code

S3Object s3Obj = getS3Client().getObject(new GetObjectRequest(getBucketName(), fileName)); //Error in Above Line itself getS3Client().putObject(getBucketName(), newFileName, s3Obj.getObjectContent(), s3Obj.getObjectMetadata()); private AmazonS3 getS3Client(){ AWSCredentials myCredentials = new BasicAWSCredentials(AccessKey,SecretKey); AmazonS3 s3client = new AmazonS3Client(myCredentials); return s3client; } 

So, I get this error,

DEBUG [main] request.handleErrorResponse (748) | Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: Status code: 403, AWS: null, AWS Request ID: AD2F31F1133A650E, AWS Error code: AccessDenied.

I can not get s3object myself. Any suggestions or ideas on how I get the S3 object and rename it. Thank you for your help.

+6
source share
2 answers

Direct renaming of S3 objects is not possible

Rename objects by copying them and deleting the original

You can copy and delete using, for example,

 CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, keyName, bucketName, destinationKeyName); s3client.copyObject(copyObjRequest); s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName)); 
+14
source

As High6 replied, directly renaming s3 is not possible, but its code will not copy all the contents

I added a code snippet that will copy all the content

only works add your passkey and secret key

here is what i did in the code

-> copy the contents of the source folder (sub child element and folders) and paste into the destination folder

-> when copying is complete, delete the original folder

 package com.bighalf.doc.amazon; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.CopyObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.S3ObjectSummary; public class Test { public static boolean renameAwsFolder(String bucketName,String keyName,String newName) { boolean result = false; try { AmazonS3 s3client = getAmazonS3ClientObject(); List<S3ObjectSummary> fileList = s3client.listObjects(bucketName, keyName).getObjectSummaries(); //some meta data to create empty folders start ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); InputStream emptyContent = new ByteArrayInputStream(new byte[0]); //some meta data to create empty folders end //final location is the locaiton where the child folder contents of the existing folder should go String finalLocation = keyName.substring(0,keyName.lastIndexOf('/')+1)+newName; for (S3ObjectSummary file : fileList) { String key = file.getKey(); //updating child folder location with the newlocation String destinationKeyName = key.replace(keyName,finalLocation); if(key.charAt(key.length()-1)=='/'){ //if name ends with suffix (/) means its a folders PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, destinationKeyName, emptyContent, metadata); s3client.putObject(putObjectRequest); }else{ //if name doesnot ends with suffix (/) means its a file CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, file.getKey(), bucketName, destinationKeyName); s3client.copyObject(copyObjRequest); } } boolean isFodlerDeleted = deleteFolderFromAws(bucketName, keyName); return isFodlerDeleted; } catch (Exception e) { e.printStackTrace(); } return result; } public static boolean deleteFolderFromAws(String bucketName, String keyName) { boolean result = false; try { AmazonS3 s3client = getAmazonS3ClientObject(); //deleting folder children List<S3ObjectSummary> fileList = s3client.listObjects(bucketName, keyName).getObjectSummaries(); for (S3ObjectSummary file : fileList) { s3client.deleteObject(bucketName, file.getKey()); } //deleting actual passed folder s3client.deleteObject(bucketName, keyName); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void main(String[] args) { intializeAmazonObjects(); boolean result = renameAwsFolder(bucketName, keyName, newName); System.out.println(result); } private static AWSCredentials credentials = null; private static AmazonS3 amazonS3Client = null; private static final String ACCESS_KEY = ""; private static final String SECRET_ACCESS_KEY = ""; private static final String bucketName = ""; private static final String keyName = ""; //renaming folder c to x from key name private static final String newName = ""; public static void intializeAmazonObjects() { credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_ACCESS_KEY); amazonS3Client = new AmazonS3Client(credentials); } public static AmazonS3 getAmazonS3ClientObject() { return amazonS3Client; } 

}

0
source

All Articles