首页 > 解决方案 > Amazon S3 通过 Java API 上传:InputStream Sources

问题描述

我正在测试使用“aws-java-sdk-s3”将小对象上传到 S3 的不同方法。作为小对象,我使用默认的 api(大对象的传输 API ......)

  1. 上传文件作为源,完美!

     File file = ....
     s3Client.putObject(new PutObjectRequest(bucket, key, file));
    
  2. 上传ByteArrayInputStream,完美

    InputStream  stream = new ByteArrayInputStream("How are you?".getBytes()))
    s3Client.putObject(new PutObjectRequest(bucket, key, stream  ));
    
  3. 资源作为流上传,问题。!

    InputStream stream  = this.getClass().getResourceAsStream("myFile.data");
    s3Client.putObject(new PutObjectRequest(bucket, key, stream  ));
    

例外:

com.amazonaws.ResetException: The request to the service failed with a retryable reason, but resetting the request input stream has failed.
 See exception.getExtraInfo or debug-level logging for the original failure that caused this retry.;  
If the request involves an input stream, the maximum stream buffer size can be configured via request.getRequestClientOptions().setReadLimit(int)

Caused by: java.io.IOException: Resetting to invalid mark
    at java.io.BufferedInputStream.reset(BufferedInputStream.java:448)
    at com.amazonaws.internal.SdkFilterInputStream.reset(SdkFilterInputStream.java:112)
    at com.amazonaws.internal.SdkFilterInputStream.reset(SdkFilterInputStream.java:112)
    at com.amazonaws.util.LengthCheckInputStream.reset(LengthCheckInputStream.java:126)
    at com.amazonaws.internal.SdkFilterInputStream.reset(SdkFilterInputStream.java:112)

我可以使用 som Apache File Utils 将类路径资源转换为文件对象,但它有点狗屎......

  1. 我是否必须根据 Stream 的类型配置 ReadLimit ?¿?
  2. 推荐什么值¿?

API 版本 M "aws-java-sdk-s3" rev="1.11.442"

标签: javaamazon-web-servicesamazon-s3

解决方案


我已经实现了一个与您的非常相似的用例(尽管不完全)。我必须将一些数据写入 JSON 文件(压缩格式)并将其存储在 S3 中。数据在哈希映射中可用。因此,哈希映射的内容将被复制到 JSON 文件中。如果没有帮助,请随意忽略。另外,我从未在任何地方设置任何类型的限制。

public void serializeResults(AmazonS3Client s3, Map<String, Object> dm, String environment)
        throws IOException {
    logger.info("start writeZipToS3");
    Gson gson = new GsonBuilder().create();
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ZipOutputStream zout = new ZipOutputStream(byteOut);

        ZipEntry ze = new ZipEntry(String.format("results-%s.json", environment));
        zout.putNextEntry(ze);
        String json = gson.toJson(dm);
        zout.write(json.getBytes());
        zout.closeEntry();
        zout.close();
        byte[] bites = byteOut.toByteArray();
        ObjectMetadata om = new ObjectMetadata();
        om.setContentLength(bites.length);
        PutObjectRequest por = new PutObjectRequest("home",
                String.format("zc-service/results-%s.zip", environment),
                new ByteArrayInputStream(bites), om);
        s3.putObject(por);

    } catch (IOException e) {
        e.printStackTrace();
    }
    logger.info("stop writeZipToS3");
}

我希望这对你有帮助。

问候


推荐阅读