Created
April 5, 2013 18:31
-
-
Save csokol/5321535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class S3FileProvider { | |
private final AmazonS3Client amazonS3Client; | |
public S3FileProvider(AmazonS3Client amazonS3Client) { | |
this.amazonS3Client = amazonS3Client; | |
} | |
public URL store(File file, String dir, String key) { | |
PutObjectRequest putObjectRequest = new PutObjectRequest(dir, key, file) | |
.withCannedAcl(CannedAccessControlList.PublicRead); | |
amazonS3Client.putObject(putObjectRequest); | |
return urlFor(dir, key); | |
} | |
public URL store(InputStream is, String dir, String key, String mimeType) { | |
ObjectMetadata metadata = new ObjectMetadata(); | |
metadata.setContentType(mimeType); | |
PutObjectRequest putObjectRequest = new PutObjectRequest(dir, key, is, metadata) | |
.withCannedAcl(CannedAccessControlList.PublicRead); | |
amazonS3Client.putObject(putObjectRequest); | |
return urlFor(dir, key); | |
} | |
public void mkdir(String dir) { | |
amazonS3Client.createBucket(dir); | |
} | |
private URL urlFor(String dir, String key) { | |
try { | |
return new URL("http://" + dir + ".s3.amazonaws.com/" + key); | |
} catch (MalformedURLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment