View Javadoc

1   package org.cyclopsgroup.kaufman.aws;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   import org.springframework.beans.factory.DisposableBean;
9   import org.springframework.beans.factory.FactoryBean;
10  
11  import com.amazonaws.auth.AWSCredentials;
12  import com.amazonaws.auth.AWSCredentialsProvider;
13  import com.amazonaws.auth.InstanceProfileCredentialsProvider;
14  import com.amazonaws.internal.StaticCredentialsProvider;
15  import com.amazonaws.services.s3.AmazonS3;
16  import com.amazonaws.services.s3.AmazonS3Client;
17  import com.amazonaws.services.s3.model.GetObjectRequest;
18  import com.amazonaws.services.s3.model.ObjectMetadata;
19  
20  /**
21   * A spring bean that downloads S3 object into a local file, return the file and
22   * delete it when Spring context disposes.
23   */
24  public class S3FileFactoryBean
25      implements FactoryBean<File>, DisposableBean
26  {
27      private static final Log LOG = LogFactory.getLog( S3FileFactoryBean.class );
28  
29      private final String bucketName, objectKey;
30  
31      private File localFile;
32  
33      private final AmazonS3 s3Client;
34  
35      public S3FileFactoryBean( AmazonS3 s3Client, String bucketName,
36                                String objectKey )
37      {
38          this.s3Client = s3Client;
39          this.bucketName = bucketName;
40          this.objectKey = objectKey;
41      }
42  
43      public S3FileFactoryBean( AWSCredentials creds, String bucketName,
44                                String objectKey )
45      {
46          this( new StaticCredentialsProvider( creds ), bucketName, objectKey );
47      }
48  
49      public S3FileFactoryBean( AWSCredentialsProvider creds, String bucketName,
50                                String objectKey )
51      {
52          this( new AmazonS3Client( creds ), bucketName, objectKey );
53      }
54  
55      public S3FileFactoryBean( String bucketName, String objectKey )
56      {
57          this( new InstanceProfileCredentialsProvider(), bucketName, objectKey );
58      }
59  
60      @Override
61      public void destroy()
62      {
63          if ( localFile.exists() )
64          {
65              localFile.delete();
66              LOG.info( "Local file " + localFile + " is deleted" );
67          }
68      }
69  
70      @Override
71      public File getObject()
72          throws IOException
73      {
74          if ( localFile == null )
75          {
76              localFile =
77                  File.createTempFile( S3FileFactoryBean.class.getSimpleName(),
78                                       ".tmp" );
79              LOG.info( "Using default local file " + localFile );
80          }
81          LOG.info( String.format( "Getting object %s:%s into local file %s",
82                                   bucketName, objectKey,
83                                   localFile.getAbsolutePath() ) );
84          ObjectMetadata result =
85              s3Client.getObject( new GetObjectRequest( bucketName, objectKey ),
86                                  localFile );
87          LOG.info( "Call to S3 succeeded and returned " + result );
88          return localFile;
89      }
90  
91      @Override
92      public Class<File> getObjectType()
93      {
94          return File.class;
95      }
96  
97      @Override
98      public boolean isSingleton()
99      {
100         return true;
101     }
102 
103     public void setLocalFile( File localFile )
104     {
105         this.localFile = localFile;
106     }
107 }