如何将多部分文件作为输入流直接流式传输到Java中的AWS S3?

时间:2017-10-30 21:00:39

标签: 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 = 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服务器中。因此整个图像上传失败。

我尝试了本文Converting MultipartFile to java.io.File without copying to local machine中提供的解决方案并错过了运气。

有人可以指导我直接将多部分文件流式传输到S3吗?

1 个答案:

答案 0 :(得分:1)

在jsp的form标签中,请确保编写enctype =" multipart / form-data"。在servlet中,在

之后声明下面的代码
  

@WebServlet(name =" ServletName",urlPatterns = {" / ServletName"})

 @MultipartConfig(fileSizeThreshold=1024*1024*10,  // 10 MB (You can specify according to your choice)
                 maxFileSize=1024*1024*50,       // 50 MB  (You can specify according to your choice)
                 maxRequestSize=1024*1024*100)

您可以在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());
}