Reputation: 1215
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?
Upvotes: 10
Views: 34917
Reputation: 4509
Yes.
Just use putObject(String bucketName, String key, String content), passing your JSON String for content
.
Upvotes: 11
Reputation: 3544
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/
Upvotes: 10