Created
August 24, 2012 03:07
-
-
Save sh0tt/3445050 to your computer and use it in GitHub Desktop.
Glacier + DynamoDB upload sample
This file contains hidden or 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 workshop.glacier; | |
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Arrays; | |
import java.util.Date; | |
import util.AWSUtil; | |
import com.amazonaws.services.dynamodb.AmazonDynamoDB; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBAttribute; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBHashKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBRangeKey; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBTable; | |
import com.amazonaws.services.dynamodb.datamodeling.DynamoDBVersionAttribute; | |
import com.amazonaws.services.glacier.AmazonGlacier; | |
import com.amazonaws.services.glacier.TreeHashGenerator; | |
import com.amazonaws.services.glacier.model.UploadArchiveRequest; | |
import com.amazonaws.services.glacier.model.UploadArchiveResult; | |
public class GlacierUploadSample { | |
public static void main(String[] args) { | |
args = new String[1]; | |
args[0] = "C:\\picture.jpg"; | |
AmazonGlacier glacier = AWSUtil.createGlacierClient(); | |
AmazonDynamoDB dynamodb = AWSUtil.createDynamoDBClient(); | |
new GlacierUploadSample().execute(glacier, dynamodb, args); | |
} | |
public void execute(AmazonGlacier glacier, AmazonDynamoDB client, | |
String... args) { | |
// upload file | |
String archiveId = null; | |
String location = null; | |
String checksum = null; | |
String originalFilePath = args[0]; | |
try { | |
File f = new File(originalFilePath); | |
byte[] body = asByteArray(f); | |
final String calculateTreeHash = TreeHashGenerator | |
.calculateTreeHash(f); | |
UploadArchiveRequest uploadArchiveRequest = new UploadArchiveRequest() | |
.withVaultName("myvault").withBody(asStream(body)) | |
.withChecksum(calculateTreeHash) | |
.withContentLength(f.length()); | |
UploadArchiveResult result = glacier | |
.uploadArchive(uploadArchiveRequest); | |
archiveId = result.getArchiveId(); | |
location = result.getLocation(); | |
checksum = result.getChecksum(); | |
// check checksum | |
validateChecksum(calculateTreeHash, checksum); | |
System.out.println("upload done:" + archiveId); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} finally { | |
System.out.println("starting store meta data"); | |
if (archiveId != null && location != null) { | |
// make entity | |
GlacierUploadRecord record = new GlacierUploadRecord(); | |
{ | |
record.setUser("shot6"); | |
record.setDate(createDate()); | |
record.setOriginalFilePath(originalFilePath); | |
record.setArchiveId(archiveId.getBytes()); | |
record.setLocation(location.getBytes()); | |
record.setChecksum(checksum); | |
} | |
DynamoDBMapper mapper = new DynamoDBMapper(client); | |
// put entity | |
mapper.save(record); | |
System.out.println("done :" + record.toString()); | |
} | |
} | |
} | |
private byte[] asByteArray(File f) { | |
try { | |
FileInputStream fis = new FileInputStream(f); | |
byte[] body = new byte[(int) f.length()]; | |
fis.read(body); | |
return body; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private String createDate() { | |
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss"); | |
return f.format(new Date(System.currentTimeMillis())); | |
} | |
private void validateChecksum(String calculateTreeHash, String checksum) { | |
if (calculateTreeHash.equals(checksum) == false) { | |
System.err.println("checksum failed:" + calculateTreeHash + ", " | |
+ checksum); | |
} | |
} | |
private InputStream asStream(byte[] body) { | |
return new ByteArrayInputStream(body); | |
} | |
@DynamoDBTable(tableName = "UploadRecord") | |
public static class GlacierUploadRecord { | |
String user; | |
String date; | |
String originalFilePath; | |
byte[] archiveId; | |
byte[] location; | |
String checksum; | |
Long version; | |
@DynamoDBHashKey | |
public String getUser() { | |
return user; | |
} | |
public void setUser(String user) { | |
this.user = user; | |
} | |
@DynamoDBRangeKey | |
public String getDate() { | |
return date; | |
} | |
public void setDate(String date) { | |
this.date = date; | |
} | |
@DynamoDBAttribute | |
public byte[] getArchiveId() { | |
return archiveId; | |
} | |
public void setArchiveId(byte[] archiveId) { | |
this.archiveId = archiveId; | |
} | |
@DynamoDBAttribute | |
public byte[] getLocation() { | |
return location; | |
} | |
public void setLocation(byte[] location) { | |
this.location = location; | |
} | |
@DynamoDBVersionAttribute | |
public Long getVersion() { | |
return version; | |
} | |
public void setVersion(Long version) { | |
this.version = version; | |
} | |
@DynamoDBAttribute | |
public String getChecksum() { | |
return checksum; | |
} | |
public void setChecksum(String checksum) { | |
this.checksum = checksum; | |
} | |
@DynamoDBAttribute | |
public String getOriginalFilePath() { | |
return originalFilePath; | |
} | |
public void setOriginalFilePath(String originalFilePath) { | |
this.originalFilePath = originalFilePath; | |
} | |
@Override | |
public String toString() { | |
return "GlacierUploadRecord [user=" + user + ", date=" + date | |
+ ", originalFilePath=" + originalFilePath + ", archiveId=" | |
+ Arrays.toString(archiveId) + ", location=" | |
+ Arrays.toString(location) + ", checksum=" + checksum | |
+ ", version=" + version + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment