简体   繁体   English

使用 Java 将 JSON 存储到 Amazon S3 中

[英]Storing JSON into Amazon S3 with Java

Is it possible to store JSON data into Amazon S3?是否可以将 JSON 数据存储到 Amazon S3 中? Lets say that I want to store this JSON data:假设我想存储这个 JSON 数据:

{
    "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.在这里说这是可能的,但后来它使用了 jQuery,但我在 Java 中找不到相应的东西。 Even if it is possible, in what form will the JSON be stored?即使有可能,JSON 将以什么形式存储? Will it be dumped into a file?它会被转储到一个文件中吗?

Yes.是的。

Just use putObject(String bucketName, String key, String content) , passing your JSON String for content .只需使用putObject(String bucketName, String key, String content) ,将您的 JSON String 传递给content

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).在您的 Java 应用程序中,将您拥有的任何对象转换为字节,然后直接将其流式传输(或先写入文件,然后上传)。 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/ Java 客户端可以在这里找到: https : //aws.amazon.com/sdk-for-java/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM