本文介绍了使用AWS开发工具包Java上传S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的设置是这样

  AmazonS3 s3Client = new AmazonS3Client();

  InputStream stream = new URL(filePath).openStream();

  ObjectMetadata objectMetadata = new ObjectMetadata();
  PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, stream, objectMetadata);

  PutObjectResult result = s3Client.putObject(putObjectRequest);

我总是收到这个错误

java.lang.IllegalStateException: Content has been consumed

我知道这是由于打电话引起的

And I know it is caused by calling

 HttpEntity.getContent()

多次

但是我似乎无法在多次调用/调试的地方找到

But I can't seem to debug/find where it is being called multiple times

推荐答案

由于您是从文件上传的,因此可以使用 PutObjectRequest的构造函数:

Since you are uploading from a file, you could use this constructor for the PutObjectRequest:

 File theFile = new File(filePath);
 PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, theFile);
 putObjectRequest.withMetadata(objectMetadata);

这篇关于使用AWS开发工具包Java上传S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:36