@@ -6,7 +6,7 @@ import models.UUID
66import com .amazonaws .auth .{AWSStaticCredentialsProvider , BasicAWSCredentials }
77import com .amazonaws .client .builder .AwsClientBuilder
88import com .amazonaws .regions .Regions
9- import com .amazonaws .services .s3 .model .{GetObjectRequest , ObjectMetadata }
9+ import com .amazonaws .services .s3 .model .{HeadBucketRequest , CreateBucketRequest , GetObjectRequest , ObjectMetadata }
1010import com .amazonaws .services .s3 .{AmazonS3 , AmazonS3ClientBuilder }
1111import com .amazonaws .{AmazonClientException , ClientConfiguration }
1212import com .google .inject .Inject
@@ -46,6 +46,42 @@ object S3ByteStorageService {
4646 */
4747class S3ByteStorageService @ Inject ()() extends ByteStorageService {
4848
49+
50+ // Only run a single thread at a time when verifying bucket existence
51+ synchronized {
52+ Play .current.configuration.getString(S3ByteStorageService .BucketName ) match {
53+ case Some (bucketName) => {
54+ try {
55+ // Validate configuration by checking for bucket existence on startup
56+ this .s3Bucket.headBucket(new HeadBucketRequest (bucketName))
57+ } catch {
58+ case sdke @ (_ : AmazonClientException | _ : AmazonServiceException ) => {
59+ if (sdke.getMessage.contains(" Status Code: 404" )) {
60+ Logger .warn(" Configured S3 bucket does not exist, attempting to create it now..." )
61+ try {
62+ // Bucket does not exist - create the bucket
63+ this .s3Bucket.createBucket(new CreateBucketRequest (bucketName))
64+ } catch {
65+ // Bucket could not be created - abort
66+ case _ : Throwable => throw new RuntimeException (" Bad S3 configuration: Bucket does not exist and could not be created." )
67+ }
68+ } else if (sdke.getMessage.contains(" Status Code: 403" )) {
69+ // Bucket exists, but you do not have permission to access it
70+ throw new RuntimeException (" Bad S3 configuration: You do not have access to the configured bucket." )
71+ } else {
72+ // Unknown error - print status code for further investigation
73+ val errMsg = sdke.getLocalizedMessage
74+ Logger .error(errMsg)
75+ throw new RuntimeException (" Bad S3 configuration: an unknown error has occurred - " + errMsg)
76+ }
77+ }
78+ case _ : Throwable => handleUnknownError(_)
79+ }
80+ }
81+ case _ => throw new RuntimeException (" Bad S3 configuration: verify that you have set all configuration options." )
82+ }
83+ }
84+
4985 /**
5086 * Grabs config parameters from Clowder to return a
5187 * AmazonS3 pointing at the configured service endpoint.
0 commit comments