Created
August 22, 2012 02:47
-
-
Save uemuraj/3421772 to your computer and use it in GitHub Desktop.
簡単な S3 用スクリプト
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
#!/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