@Override public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception { { ObjectMetadata metadata = getConfigMetadata(); if (metadata != null) { Date lastModified = metadata.getLastModified(); if (lastModified.getTime() != compareVersion) { return null; // apparently there's no atomic way to do this with S3 so this will have to // do } } } PropertyBasedInstanceConfig propertyBasedInstanceConfig = new PropertyBasedInstanceConfig(config); ByteArrayOutputStream out = new ByteArrayOutputStream(); propertyBasedInstanceConfig .getProperties() .store(out, "Auto-generated by Exhibitor " + hostname); byte[] bytes = out.toByteArray(); ObjectMetadata metadata = S3Utils.simpleUploadFile(s3Client, bytes, arguments.getBucket(), arguments.getKey()); return new LoadedInstanceConfig( propertyBasedInstanceConfig, metadata.getLastModified().getTime()); }
private PartETag uploadChunk( ByteBuffer bytes, InitiateMultipartUploadResult initResponse, int index) throws Exception { byte[] md5 = S3Utils.md5(bytes); UploadPartRequest request = new UploadPartRequest(); request.setBucketName(initResponse.getBucketName()); request.setKey(initResponse.getKey()); request.setUploadId(initResponse.getUploadId()); request.setPartNumber(index); request.setPartSize(bytes.limit()); request.setMd5Digest(S3Utils.toBase64(md5)); request.setInputStream(new ByteBufferInputStream(bytes)); UploadPartResult response = s3Client.uploadPart(request); PartETag partETag = response.getPartETag(); if (!response.getPartETag().getETag().equals(S3Utils.toHex(md5))) { throw new Exception("Unable to match MD5 for part " + index); } return partETag; }