如何将多部分文件作为inputstream直接流式传输到Java中的AWS S3?
现在,我的代码获取MultipartFile并将其转换为java.util.File并上传到S3如何将多部分文件作为inputstream直接流式传输到Java中的AWS S3?,java,amazon-web-services,tomcat,amazon-s3,amazon-ec2,Java,Amazon Web Services,Tomcat,Amazon S3,Amazon Ec2,现在,我的代码获取MultipartFile并将其转换为java.util.File并上传到S3 try { final ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType(file.getContentType()); byte[] contentBytes = null; try { final InputStream is =
try {
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
byte[] contentBytes = null;
try {
final InputStream is = file.getInputStream();
contentBytes = IOUtils.toByteArray(is);
} catch (final IOException e) {
log.error("Failed while reading bytes from %s" + e.getMessage());
}
final Long contentLength = Long.valueOf(contentBytes.length);
objectMetadata.setContentLength(contentLength);
objectMetadata.setHeader("filename", fileNameWithExtn);
/*
* Reobtain the tmp uploaded file as input stream
*/
final InputStream inputStream = file.getInputStream();
final File convFile = new File(fileNameWithExtn);
file.transferTo(convFile);
FileUtils.copyInputStreamToFile(inputStream, convFile);
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, convFile))
.getVersionId();
} catch (final AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which"
+ " means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (final AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"
+ " the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}
我不想这样做的原因是,在我的桌面上,我在浏览器中上传的图像暂时停留在tomcat服务器中,然后上传到S3
但当我的webapp在Ec2中运行时,由于权限问题(或其他原因),从浏览器上传的图像似乎不会留在tomcat服务器中。因此,整个图像上传失败
我尝试了本文提供的解决方案,但运气不好
有人能用代码指导我将一个多部分文件直接流式传输到S3吗?在jsp的表单标记中,确保编写enctype=“multipart/form data”。然后在servlet中声明下面的代码 @WebServlet(name=“ServletName”,urlPatterns={”/ServletName}) 您可以在servlet中编写以下代码,以inputStream的形式获取文件并将其上载到S3中
Part filePart= request.getPart("FileName"); // The name of <input type=file name="FileName"> in jsp form. Here, FileName
try {
InputStream inputStream=null;
if(filePart!=null){
inputStream=filePart.getInputStream(); //get file in form of inputstream
}
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(filePart.getSize());
md.setContentType(filePart.getContentType());
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, inputStream,md))
.getVersionId();
} catch (final AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which"
+ " means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (final AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"
+ " the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}
partfilepart=request.getPart(“文件名”);//以jsp形式显示的名称。这里,文件名
试一试{
InputStream InputStream=null;
if(filePart!=null){
inputStream=filePart.getInputStream();//以inputStream的形式获取文件
}
ObjectMetadata md=新的ObjectMetadata();
md.setContentLength(filePart.getSize());
md.setContentType(filePart.getContentType());
试一试{
最终AmazonS3 s3Client=AmazonS3ClientBuilder.standard().build();
versionId=s3Client.putObject(新的PutObjectRequest(“bucketName”,name,inputStream,md))
.getVersionId();
}捕获(最终AmazonServiceException ase){
System.out.println(“捕获到一个AmazonServiceException,它”
+“意味着是你提出的请求”
+“发送到AmazonS3,但由于某种原因被拒绝,错误响应为“+”);
System.out.println(“错误消息:+ase.getMessage());
System.out.println(“HTTP状态代码:+ase.getStatusCode());
System.out.println(“AWS错误代码:+ase.getErrorCode());
System.out.println(“错误类型:+ase.getErrorType());
System.out.println(“请求ID:+ase.getRequestId());
}捕获(最终AmazonClientException ace){
System.out.println(“捕获了AmazonClientException,这意味着”
+客户端在尝试执行时遇到“+”内部错误
+“与S3通信,”+“例如无法访问网络。”);
System.out.println(“错误消息:+ace.getMessage());
}
}捕获(最终异常e){
log.error(“您未能上载”+name+“=>”+e.getMessage());
}
我认为没有一个PutObjectRequest构造函数有三个参数
Part filePart= request.getPart("FileName"); // The name of <input type=file name="FileName"> in jsp form. Here, FileName
try {
InputStream inputStream=null;
if(filePart!=null){
inputStream=filePart.getInputStream(); //get file in form of inputstream
}
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(filePart.getSize());
md.setContentType(filePart.getContentType());
try {
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, inputStream,md))
.getVersionId();
} catch (final AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which"
+ " means your request made it "
+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (final AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means"
+ " the client encountered " + "an internal error while trying to "
+ "communicate with S3, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
} catch (final Exception e) {
log.error("You failed to upload " + name + " => " + e.getMessage());
}