我不想在代码中给出文件路径我希望亚马逊s3中图像的字节数据

时间:2016-03-25 07:01:38

标签: java spring-mvc amazon-s3

任何人都可以帮助我。代码正常用于图片上传但是我想在文件路径中给出字节值。因为图像数据类型是CommonsMultipartFile。

@RequestMapping(value = "/saveImage", method = RequestMethod.POST)
    public @ResponseBody ImageResultVO getImage(ImageCriteriaVO imageCriteriaVO) throws O2Exception, IOException
    {               
        CommonsMultipartFile file ;
        file =(CommonsMultipartFile) imageCriteriaVO.getImage();
        AWSCredentials credentials = new BasicAWSCredentials(System.getenv("AWS_ACCESS_KEY_ID") ,System.getenv("AWS_SECRET_ACCESS_KEY"));
          String SUFFIX = "/";
          String folderName = "images";
          String existingBucketName = "o2container";
          String keyName = folderName + SUFFIX + file.getOriginalFilename();
          System.out.println("image" +keyName);       
          String filePath = "D:/bhanu/images%20(4).JPG";
          System.out.println("filePath" +filePath);
          String amazonFileUploadLocationOriginal=existingBucketName;
          System.out.println("hello");
          AmazonS3 s3Client = new AmazonS3Client(credentials);
          System.out.println("hello1");
          FileInputStream stream = new FileInputStream(filePath);
          System.out.println("hello2");
          ObjectMetadata objectMetadata = new ObjectMetadata();
          System.out.println("hello3");
          PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, stream, objectMetadata);
          System.out.println("hello4");
          PutObjectResult result = s3Client.putObject(putObjectRequest);

        System.out.println("Etag:" + result.getETag() + "-->" + result);


        return null;
}

1 个答案:

答案 0 :(得分:0)

putObject的版本接受InputStream而不是文件路径:

  

putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata)

     

在指定的存储分区和密钥名称下将指定的输入流和对象元数据上载到Amazon S3。

请参阅:putObject documentation