Thursday, June 7, 2018

spring boot file upload to amazon s3 on ubuntu

  1. https://medium.com/oril/uploading-files-to-aws-s3-bucket-using-spring-boot-483fcb6f8646 
  2. first sign up or login to your amazon account and create bucket in s3. then create a user and give permission ie programmatic access 
  3. @Configuration
    public class S3Config {

        @Value("${userid.aws.access_key_id}")
        private String awsId;

        @Value("${userid.aws.secret_access_key}")
        private String awsKey;
       
        @Value("${userid.s3.region}")
        private String region;

        @Bean
        public AmazonS3 xyz(){
            System.out.println("Inside s3Client of S3Config ");
            BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsId, awsKey);
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                                    .withRegion(Regions.fromName(region))
                                    .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                    .build();
            return s3Client;
          }


    }
  4. @Controller
    public class AController {
       
        @Autowired
        private S3Service s3service;
      
              @PostMapping("/uploadFile")
             
            
              public String uploadFile(@RequestPart(value = "xyz") MultipartFile multipartFile,Model map,HttpServletRequest request) {
                 
                  System.out.println("Inside upload file of bucket controller ");

                  try
                  {
                      String filePath = request.getServletContext().getRealPath("/");
                                     
                      System.out.println("filePath is "+filePath);
                      File f1=new File(filePath+"/"+multipartFile.getOriginalFilename());
                      multipartFile.transferTo(f1);
                      System.out.println("f1 is "+f1);                 
                     
                      String fileName=f1.getName();

                   String fileUrl=s3service.uploadFile(fileName, f1);
                 
                  /**
                   * we should be sure that the file has indeed been uploaded 
                   */
                    map.addAttribute("fileUrl", fileUrl);                         
                   f1.delete();
                  
                 /* for rest request
                   return fileUrl;
                   */              
                  
                   return "uploadSuccess";
                  
                  }
                  catch(IOException e)
                  {
                      e.printStackTrace();
                      return e.toString();
                  }
                 
            }
  5. @Service
    public class S3ServicesImpl implements S3Service {
        private Logger logger = LoggerFactory.getLogger(S3ServicesImpl.class);
        /* (non-Javadoc)
         * @see com.epilen.androidPatient.service.aws.s3.S3Service#downloadFile(java.lang.String)
         */
       

       
        @Autowired
        private AmazonS3 s3client;

        @Value("${userid.s3.bucket}")
        private String bucketName;
       
        @Value("${s3.endpointUrl}")
        private String endpointUrl;
       
       
        /**
         * convert multipart to file
         * @param file
         * @return
         * @throws IOException
         */
       
       
        @Override
        public void downloadFile(String keyName) {
           
            try {
               
               System.out.println("Downloading an object");
               S3Object s3object = s3client.getObject(new GetObjectRequest(bucketName, keyName));
               System.out.println("Content-Type: "  + s3object.getObjectMetadata().getContentType());
               Utility.displayText(s3object.getObjectContent());
               logger.info("===================== Import File - Done! =====================");
               
            } catch (AmazonServiceException ase) {
                logger.info("Caught an AmazonServiceException from GET requests, rejected reasons:");
                logger.info("Error Message:    " + ase.getMessage());
                logger.info("HTTP Status Code: " + ase.getStatusCode());
                logger.info("AWS Error Code:   " + ase.getErrorCode());
                logger.info("Error Type:       " + ase.getErrorType());
                logger.info("Request ID:       " + ase.getRequestId());
            } catch (AmazonClientException ace) {
                logger.info("Caught an AmazonClientException: ");
                logger.info("Error Message: " + ace.getMessage());
            } catch (IOException ioe) {
                logger.info("IOE Error Message: " + ioe.getMessage());
            }
        }

        /**
         * this method will return the fileurl
         */
        @Override
        public String uploadFile(String fileName, File file) {
            System.out.println("Inside upload file of S3ServiceImpl");
            try {
                   
                s3client.putObject(new PutObjectRequest(bucketName, fileName, file));
                logger.info("===================== Upload File - Done! =====================");
               
                String fileUrl = endpointUrl + "/" + bucketName + "/" + fileName;
                return fileUrl;
               
            } catch (AmazonServiceException ase) {
                logger.info("Caught an AmazonServiceException from PUT requests, rejected reasons:");
                logger.info("Error Message:    " + ase.getMessage());
                logger.info("HTTP Status Code: " + ase.getStatusCode());
                logger.info("AWS Error Code:   " + ase.getErrorCode());
                logger.info("Error Type:       " + ase.getErrorType());
                logger.info("Request ID:       " + ase.getRequestId());
                return ase.toString();
               
            } catch (AmazonClientException ace) {
                logger.info("Caught an AmazonClientException: ");
                logger.info("Error Message: " + ace.getMessage());
                return ace.toString();
            }
        }

       
    }
  6. public interface S3Service {
            public String uploadFile(String fileName, File file);
    }

  7. pom.xml entry->.   
        <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-java-sdk-bom</artifactId>
          <version>1.11.327</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>

      <dependencies>
     
       <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
      </dependency>
     
       <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.5.2</version> 
     </dependency>
     
      the above reduces the jar size from 90 mb to 35 mb and downloads only s3 dependencies
  8. application.properties entries                                                                          userid.aws.access_key_id=  userid.aws.secret_access_key=                                                                             userid.s3.bucket=
    userid.s3.region=
    s3.endpointUrl=

No comments:

Post a Comment