This article I’ll show you how to create program with spring and upload file to amazon S3.
Access Key ID & Secret Access Key
Before we start, we need 2 pieces of information from your aws console. You can obtain this from your amazon dashboard.
From the top right, click your name, select security and credentials and click continue to security credentials. Expand theĀ Access Keys (Access Key ID and Secret Access Key). Click create new access key and will be download a csv file called rootkey.csv.
Create a bucket
You can create a bucket from the coding, but in this demo I’ll focus to upload file. Therefore I’ll create bucket name it as yourfoldername.
POM
In order to upload file into S3, you’ll need to add the amazon sdk into your dependency.
<!-- AWS --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.10.12</version> </dependency>
ApplicationContext (JavaBased-Config)
Let’s spring manage AWS Object and it’s lifecycle. In this application context I’ve define 2 beans, which is AWSCredentials and AmazonS3 object.
@Configuration @ComponentScan(basePackageClasses = Application.class, excludeFilters = @Filter({Controller.class, Configuration.class})) public class ApplicationConfig { @Value("${aws_access_key_id}") private String awsId; @Value("${aws_secret_access_key}") private String awsKey; @Bean public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setLocations(new Resource[] { new ClassPathResource("/amazon.properties") }); return ppc; } @Bean public AWSCredentials credential() { return new BasicAWSCredentials(awsId, awsKey); } @Bean public AmazonS3 s3client() { return new AmazonS3Client(credential()); } }
Properties file
Create a amazon.properties file and put it under your classpath. Copy the previous download content from your rootkey.csv into this properties file.
aws_access_key_id=AAAAAAAAAAAAA aws_secret_access_key=+B+B+B+B+B+B+B+B+B+B aws_namecard_bucket=yourfoldername
Create a AswS3Service
Create a service interface with the following methods
public interface AwsS3Service { public void uploadFile(String uploadFile, String fileName); }
Create Service Implementation
@Service public class AwsS3ServiceImpl implements AwsS3Service { @Autowired private AmazonS3 s3client; @Value("${aws_namecard_bucket}") private String nameCardBucket; /* * upload file to folder and set it to public */ public void uploadFile(String uploadFile, String filename) { String fileNameInS3 = filename; s3client.putObject( new PutObjectRequest(nameCardBucket, fileNameInS3, new File(uploadFile)) .withCannedAcl(CannedAccessControlList.PublicRead)); } }
Test Your Code
public class S3Test { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ApplicationConfig.class); ctx.refresh(); AwsS3Service service = ctx.getBean(AwsS3Service.class); String uploadFile = "/Users/mingch/Documents/test2.txt"; String fileName = "test2.txt"; service.uploadFile(uploadFile, fileName); ctx.close(); } }
Results
Enhancement
The code show you a simple way to upload file to S3, but you can enhance it like upload into a folder within the bucket or set security level and etc.
Hi,
Thank you very much for you post.
Would you please provide the source code of the project.
Thank you.
Source code added.
Thanks lot you mad my day.. Your post amazing and well structured