[Java] Access the signed URL of s3 (signed version 2)

I will follow the official page of aws. https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/RESTAuthentication.html

In the authentication header Authorization: AWS AWSAccessKeyId:Signature However, it is quite troublesome to make this Signature.

I happened to use it in a Java project, so I'll post it instead of a memo.

This time, https://[BUCKET].[ENDPOINT]/[FILE_NAME] I am accessing with a certificate.

  protected void getFile() throws NoSuchAlgorithmException, InvalidKeyException, ClientProtocolException, IOException {

    String resource= "/" + BUCKET + "/" +FILE_NAME;
    String contentType = "application/octet-stream";
    String md5 = "";
    String daHeader = "x-amz-meta-user:" + USER_ID;
    
  // 
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    df.setTimeZone(c.getTimeZone());
    
    //Creating a StringToSign
    String now = df.format(c.getTime());
    String stringToSign="GET" + "\n" + md5 + "\n" + contentType + "\n" + now + "\n" + daHeader + "\n" + resource;

    //Creating a signature
    SecretKeySpec sk = new SecretKeySpec(S3_SECRET.getBytes(), "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sk);
    byte[] dataBytes = stringToSign.getBytes("UTF-8");
    byte[] signatureBytes = mac.doFinal(dataBytes);
    String signature = new String(Base64.encodeBase64(signatureBytes), "UTF-8");
        

    //Get request
    HttpClient client = HttpClientBuilder.create().build(); 
    HttpGet request = new HttpGet("https://" + BUCKET + '.' + END_POINT + "/" + FILE_NAME);
    request.setHeader("Host", BUCKET + '.' + END_POINT);
    request.setHeader("Date", now.toString());
    request.setHeader("Content-Type", contentType);
    request.setHeader("Authorization", "AWS" + " " + S3_KEY + ':' + signature);
    request.setHeader("x-amz-meta-user", USER_ID);        
    
    HttpResponse response = client.execute(request);      
  }

Recommended Posts

[Java] Access the signed URL of s3 (signed version 2)
[Java version] The story of serialization
Try the free version of Progate [Java II]
Try the free version of Progate [Java I]
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
[Java] Check the JDK version of the built war file
Replace only part of the URL host with java
Switch the version of bundler
Access the network interface in Java
[Java] Delete the elements of List
The origin of Java lambda expressions
Switch the version of java installed by SDKMAN when moving directories
Get the public URL of a private Flickr file in Java
Is the version of Elasticsearch you are using compatible with Java 11?
Get the result of POST in Java
About the version of Docker's Node.js image
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
Installation of OMC APM Agent --Java version-
Compare the elements of an array (Java)
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
How to find out the Java version of a compiled class file
Looking back on the basics of Java
How to install the legacy version [Java]
Output of the book "Introduction to Java"
The story of writing Java in Emacs
Check the version of the standard Web software.
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
Java vs. JavaScript: What ’s the Difference?
Code that deletes all files of the specified prefix in AWS S3 (Java)
Check the version of the JDK installed and the version of the JDK enabled
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the idea of anonymous classes in Java
The order of Java method modifiers is fixed
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
Manage the version of Ruby itself with rbenv
[Java] Be careful of the key type of Map
Feel the passage of time even in Java
Change the URL of the application of Eclipse project (Maven).
Set Java version 11 of Docker Minecraft Paper server
Calculate the similarity score of strings with JAVA
Install the latest version of Jenkins on Ubuntu 16
[Java / Kotlin] Resize considering the orientation of the image
I touched on the new features of Java 15
The date time of java8 has been updated
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java