Created
September 10, 2012 02:56
-
-
Save tedder/3688572 to your computer and use it in GitHub Desktop.
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
package net.inervo; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FilenameFilter; | |
import java.io.StringWriter; | |
import org.apache.commons.io.IOUtils; | |
import com.amazonaws.AmazonClientException; | |
import com.amazonaws.AmazonServiceException; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
import com.amazonaws.services.glacier.AmazonGlacier; | |
import com.amazonaws.services.glacier.AmazonGlacierClient; | |
import com.amazonaws.services.glacier.model.DescribeVaultOutput; | |
import com.amazonaws.services.glacier.model.GetJobOutputRequest; | |
import com.amazonaws.services.glacier.model.GetJobOutputResult; | |
import com.amazonaws.services.glacier.model.GlacierJobDescription; | |
import com.amazonaws.services.glacier.model.InitiateJobRequest; | |
import com.amazonaws.services.glacier.model.InitiateJobResult; | |
import com.amazonaws.services.glacier.model.JobParameters; | |
import com.amazonaws.services.glacier.model.ListJobsRequest; | |
import com.amazonaws.services.glacier.model.ListJobsResult; | |
import com.amazonaws.services.glacier.model.ListVaultsRequest; | |
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager; | |
import com.amazonaws.services.glacier.transfer.UploadResult; | |
public class S3 { | |
private static final BasicAWSCredentials creds = new BasicAWSCredentials( | |
"key", "secret"); | |
private static final String VAULT = "vaultname"; | |
public static void main(String[] args) throws Exception { | |
// AmazonS3 s3 = new AmazonS3Client(creds); | |
AmazonGlacier g = new AmazonGlacierClient(creds); | |
ListJobsResult ljr = g.listJobs(new ListJobsRequest(VAULT)); | |
// ListJobsResult ljr = g.listJobs(new ListJobsRequest()); | |
for (GlacierJobDescription jobs : ljr.getJobList()) { | |
System.out.println("job id: " + jobs.getJobId() + "\naction: " | |
+ jobs.getAction() + "\ncompleted: " + jobs.getCompleted() | |
+ "\narchiveid: " + jobs.getArchiveId() + "\nisize: " | |
+ jobs.getInventorySizeInBytes() + "\njob desc: " | |
+ jobs.getJobDescription() + "\nstatus code: " | |
+ jobs.getStatusCode() + "\nstatus msg: " | |
+ jobs.getStatusMessage() + "\n\n"); | |
GetJobOutputResult jobOutputResult = g | |
.getJobOutput(new GetJobOutputRequest() | |
.withVaultName(VAULT).withJobId(jobs.getJobId())); | |
StringWriter writer = new StringWriter(); | |
IOUtils.copy(jobOutputResult.getBody(), writer, "UTF-8"); | |
String theString = writer.toString(); | |
System.out.println(theString + "\n\n"); | |
} | |
// InitiateJobRequest ij = new | |
// InitiateJobRequest(result.getVaultName() new JobParameters()); | |
// atm.upload("vaultname", archiveDescription, file) | |
// UploadArchiveRequest uar = new UploadArchiveRequest(); | |
// uar. | |
// g.uploadArchive(uar) | |
// getJob("maJNtEpTmgEnBTe0bfYv-ABzq0Zx2WxoRPscyNh8HwK81fiC152WFp_AtWHBBGIVqVk52miq3Hmw_2-u_can2WllRfHI"); | |
} | |
public static void getJob(String jobid) { | |
ArchiveTransferManager atm = new ArchiveTransferManager(creds); | |
// atm.download(accountId, vaultName, archiveId, file) | |
atm.download(VAULT, jobid, new File("/tmp/ff")); | |
} | |
public static void requestInventory() { | |
AmazonGlacier g = new AmazonGlacierClient(creds); | |
InitiateJobRequest ij = new InitiateJobRequest() | |
.withVaultName("jump-time-test") | |
.withJobParameters( | |
new JobParameters() | |
.withType("inventory-retrieval") | |
.withSNSTopic( | |
"arn:aws:sns:us-east-1:857235605438:TedTestOutput")); | |
InitiateJobResult ijr = g.initiateJob(ij); | |
String jobid = ijr.getJobId(); | |
System.out.println("job id: " + jobid); | |
} | |
public static void listVaults() { | |
AmazonGlacier g = new AmazonGlacierClient(creds); | |
for (DescribeVaultOutput result : g.listVaults(new ListVaultsRequest()) | |
.getVaultList()) { | |
System.out.print("\nVaultName: " + result.getVaultName() | |
+ "\n CreationDate: " + result.getCreationDate() | |
+ "\n LastInventoryDate: " + result.getLastInventoryDate() | |
+ "\n NumberOfArchives: " + result.getNumberOfArchives() | |
+ "\n SizeInBytes: " + result.getSizeInBytes() | |
+ "\n VaultARN: " + result.getVaultARN() + "\n"); | |
} | |
} | |
public static void uploadDir() throws Exception { | |
ArchiveTransferManager atm = new ArchiveTransferManager(creds); | |
File dir = new File("/mnt/readynas/pictures/la-pix/greg on ach/"); | |
for (File f : dir.listFiles(new JpegAcceptor())) { | |
System.out.println("name: " + f.getName()); | |
UploadResult result = atm.upload("jump-time-test", f.toString(), f); | |
System.out.println("done, id: " + result.getArchiveId()); | |
} | |
} | |
} | |
class FlatDirStructureAcceptor implements FilenameFilter { | |
@Override | |
public boolean accept(File arg0, String arg1) { | |
File thisfile = new File(arg0, arg1); | |
if (!thisfile.isFile()) | |
return false; | |
if (arg1.startsWith(".")) | |
return false; | |
return true; | |
} | |
} | |
class JpegAcceptor implements FilenameFilter { | |
@Override | |
public boolean accept(File arg0, String arg1) { | |
File thisfile = new File(arg0, arg1); | |
if (!thisfile.isFile()) | |
return false; | |
if (arg1.endsWith("jpg")) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment