Amar Singh
Amar Singh

Reputation: 31

How to parse an Amazon S3 bucket name and object key from the ARN value by using Java?

I am working with Amazon S3 and I need to parse the Amazon S3 bucket name and object key from the object ARN by using Java. Is there any built-in library for it? if not can you help me with the java code for it.

Upvotes: 1

Views: 2564

Answers (1)

smac2020
smac2020

Reputation: 10704

The Amazon S3 API does not have an API method for this. You can use Java logic to obtain the bucket name and object name from an Object ARN. Here is your solution.

    String arn = "arn:aws:s3:::bucketmay10002/book.pdf";
    String bucketObject = "";


    String[] value1 = arn.split(":::");
    for (String t : value1){
        bucketObject = t ;
        System.out.println(bucketObject);
}

    String[] value3 = bucketObject.split("/");
    for (String t : value3)
        System.out.println(t);
}

Output:

  • arn:aws:s3
  • bucketmay10002/book.pdf
  • bucketmay10002
  • book.pdf

As you can see, you get both the bucket name and object name from the given object ARN.

Upvotes: 1

Related Questions