- <dependency>
- <groupId>com.amazonaws</groupId>
- <artifactId>aws-java-sdk</artifactId>
- <version>1.9.13</version>
- </dependency>
Check out the simplest sample code https://github.com/aws/aws-sdk-java/blob/master/src/samples/AmazonS3/S3Sample.java
Before running, make sure you setup API credentials properly.Follow the instruction here to do this :
Creating a directory:-
http://voidweb.com/2013/03/how-to-create-a-folder-programmatically-in-s3-using-amazon-aws-java-sdk/ has good examples for creating a directory
Uploading a file:-
Here is an for uploading a file which will be publicly accessible:
http://mohiplanet.blogspot.com/2014/11/aws-s3-multipart-file-upload-with.html
About content type:-
Note:- You have to select appropriate content types for each of your files. S3 SDK is not gonna do it automatically for you.
Here is a sample that you can use during uploading with correct content type:-
Set content type according to your key name with:
Details and definition of getFileContentTypeFromFileName(key) are here:-
http://mohiplanet.blogspot.com/2014/12/amazon-web-services-s3-api-java-client.html
More examples:-
https://github.com/aws/aws-sdk-java/tree/master/src/samples/AmazonS3
Here is an for uploading a file which will be publicly accessible:
http://mohiplanet.blogspot.com/2014/11/aws-s3-multipart-file-upload-with.html
About content type:-
Note:- You have to select appropriate content types for each of your files. S3 SDK is not gonna do it automatically for you.
Here is a sample that you can use during uploading with correct content type:-
- public ObjectMetadata generateCorrectContentTypeObjMetaFromKeyName(String key) throws InvalidContentTypeException {
- ObjectMetadata metadata = new ObjectMetadata();
- String contentType = getFileContentTypeFromFileName(key);
- if (contentType.length() == 0) {
- throw new InvalidContentTypeException();
- }
- LOG.log(Level.INFO, "Setting content type : " + contentType);
- metadata.setContentType(contentType);
- return metadata;
- }
Set content type according to your key name with:
- initRequest.setObjectMetadata(generateCorrectContentTypeObjMetaFromKeyName(keyName));
Details and definition of getFileContentTypeFromFileName(key) are here:-
http://mohiplanet.blogspot.com/2014/12/amazon-web-services-s3-api-java-client.html
More examples:-
https://github.com/aws/aws-sdk-java/tree/master/src/samples/AmazonS3
Command line tools for working fast:-
http://s3tools.org/s3cmd
http://s3tools.org/s3cmd
No comments:
Post a Comment