Skip to content

Instantly share code, notes, and snippets.

@atomsfat
Last active July 10, 2019 15:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atomsfat/6273637 to your computer and use it in GitHub Desktop.
Save atomsfat/6273637 to your computer and use it in GitHub Desktop.
Upload to S3 with Groovy
/* Script to upload files to S3.
* @author Tomas Salazar
*/
@GrabResolver(name='jets3t', root='http://www.jets3t.org/maven2', m2Compatible='true')
@Grab(group='net.java.dev.jets3t', module='jets3t', version='0.9.0')
import org.jets3t.service.impl.rest.httpclient.RestS3Service
import org.jets3t.service.security.AWSCredentials
import org.jets3t.service.model.*
accessKey = 'Cambiar'
secretKey = 'cambiar'
if(args.size() < 3){
println "at least 3 parameters"
println "bucketname foldername file1 file2 etc"
System.exit(1) // CODIGO DE ERROR
}
def bucketStr = "${args[0]}" // NOMBRE DEL BUCKET
def folder = "/${args[1]}".toString() // FOLDER DESPUES DEL BUCKET
public putS3() {}
def login = new AWSCredentials( accessKey, secretKey )
def s3 = new RestS3Service( login )
println "\nBucket $bucketStr"
def bucket = s3.getBucket( bucketStr)
if(bucket){
for(int i=2; i < args.size(); i++){
def fileName = "${args[i]}" // NOMBRE DEL ARCHIVO A SUBIR
def s3obj = new S3Object( new File( fileName ) )
println "\nUploading $fileName to ${bucket.name}$folder"
def s3objStore = s3.putObject( bucket.name + folder, s3obj )
println "Successfully upload $fileName"
}
}else{
println "the bucket $bucketStr cant be found, nothing was uploaded"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment