Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing JSON into Amazon S3 with Java

Is it possible to store JSON data into Amazon S3? Lets say that I want to store this JSON data:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

I checked here says it is possible but then it is using jQuery but I could not find the corrresponding thing in Java. Even if it is possible, in what form will the JSON be stored? Will it be dumped into a file?

like image 693
Ihsan Haikal Avatar asked Dec 07 '16 10:12

Ihsan Haikal


People also ask

Can you store JSON in S3?

Now run the client app. And now click on the Upload File button, this will call our lambda function and put the file on our S3 bucket. Congrats! You have successfully done the process of uploading JSON files in S3 using AWS Lambda.

Is JSON compatible with Java?

The Java API for JSON Processing provides portable APIs to parse, generate, transform, and query JSON. JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write.

Can you convert JSON to Java?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.


2 Answers

Yes.

Just use putObject(String bucketName, String key, String content), passing your JSON String for content.

like image 185
cellepo Avatar answered Sep 19 '22 16:09

cellepo


Yes, you're pretty much dealing with bytes here so whatever format these bytes represent has no impact at all.

In your java app, convert whatever object you have into bytes then stream that out directly (or write to a file first, then upload). Sample code:

 ObjectMapper objectMapper = new ObjectMapper(); 
 byte[] bytesToWrite = objectMapper.writeValueAsBytes(yourObject)

 ObjectMetadata omd = new ObjectMetadata();
 omd.setContentLength(bytesToWrite.length);
 transferManager.upload(bucketName, filename, new ByteArrayInputStream(bytesToWrite), omd);

The java client can be found here: https://aws.amazon.com/sdk-for-java/

like image 33
wwadge Avatar answered Sep 23 '22 16:09

wwadge