So, uploading to S3 is not always as simple as it should be. Here's a completely self-contained Groovy script that can upload files to your S3 bucket, plus provide a signed link to access the uploaded file.
The script uses Groovy Grab
macro to download the JetS3t library which does the heavy lifting.
#!/opt/groovy/bin/groovy /* Script to manipulate S3 objects. * @author Tom Nichols * @see http://blog.thomnichols.org * @see http://jets3t.s3.amazonaws.com/api/org/jets3t/service/impl/rest/httpclient/RestS3Service.html */ import org.jets3t.service.impl.rest.httpclient.RestS3Service import org.jets3t.service.security.AWSCredentials import org.jets3t.service.model.* bucketName = 'CHANGEME' accessKey = 'CHANGEME' secretKey = 'CHANGEME' folder = 'CHANGEME' // optional folder name before the file @Grab(group='net.java.dev.jets3t',module='jets3t',version='[0.6.1,)') public putS3() {} def login = new AWSCredentials( accessKey, secretKey ) def expiry = new GregorianCalendar( 2011,0,1 ).time def s3 = new RestS3Service( login ) def bucket = new S3Bucket( bucketName ) args.each { fileName -> def key = "$folder/$fileName" // s3.deleteObject bucketName, key def s3obj = new S3Object( bucket, new File( fileName ) ) s3obj.key = key println "\nUploading $fileName to $bucketName/$key" s3obj = s3.putObject( bucket, s3obj ) def link = s3.createSignedGetUrl( bucketName, key, login, expiry, false ) println "$fileName : $link" }Just change the bucket name, key and folder at the top of the script, or optionally you could pull them in via system properties or environment variables. Enjoy.
No comments:
Post a Comment