Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created August 22, 2012 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uemuraj/3421772 to your computer and use it in GitHub Desktop.
Save uemuraj/3421772 to your computer and use it in GitHub Desktop.
簡単な S3 用スクリプト
#!/usr/bin/env groovy
@Grab('com.amazonaws:aws-java-sdk:*')
import com.amazonaws.*
import com.amazonaws.auth.*
import com.amazonaws.services.identitymanagement.*
import com.amazonaws.services.identitymanagement.model.*
import com.amazonaws.services.s3.*
import com.amazonaws.services.s3.model.*
def options = new CliBuilder().with {
b(longOpt:'bucketname', argName:'bucketname', required:true, args:1, 'bucket name')
g(longOpt:'get', argName:'get', 'get Object')
p(longOpt:'put', argName:'put', 'put Object')
c(longOpt:'copy', argName:'copy', 'copy Object')
d(longOpt:'delete', argName:'delete', 'delete Object')
return it.parse(args)
}
if (options) {
AWSCredentials credentials = new DefaultAWSCredentialsProviderChain().getCredentials()
ClientConfiguration config = new ClientConfiguration()
String proxyHost = System.getProperty('http.proxyHost')
String proxyPort = System.getProperty('http.proxyPort')
if (proxyHost != null && proxyPort != null) {
config.proxyHost = proxyHost
config.proxyPort = proxyPort.toInteger()
}
AmazonS3 s3 = new AmazonS3Client(credentials, config)
if (options.delete) {
options.arguments().each {
File file = new File(it)
s3.deleteObject(options.bucketname, file.name)
}
return
}
if (options.put) {
options.arguments().each {
File file = new File(it)
s3.putObject(new PutObjectRequest(options.bucketname, file.name, file))
}
return
}
if (options.get) {
options.arguments().each {
File file = new File(it)
s3.getObject(new GetObjectRequest(options.bucketname, file.name), file)
}
return
}
if (options.copy) {
def arguments = options.arguments()
if (arguments.size() == 2) {
String source = arguments.first()
String destination = arguments.last()
s3.copyObject(options.bucketname, source, options.bucketname, destination)
}
return
}
long total = 0
ObjectListing listing = s3.listObjects(options.bucketname)
listing.objectSummaries.each {
printf('%1$tY-%1$tm-%1$td %2$,18d %3$s\n', it.lastModified, it.size, it.key)
total += it.size
}
printf('total: %,d\n', total)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment