/** * @return * the Base64-encoded MD5 hash of an object's data contents as stored in the metadata * item <code>Content-MD5</code>, or as derived from an <code>ETag</code> or * <code>md5-hash</code> hex-encoded version of the hash. Returns null if the hash value is not * available. */ public String getMd5HashAsBase64() { String md5HashBase64 = (String) getMetadata(METADATA_HEADER_CONTENT_MD5); if (md5HashBase64 == null) { // Try converting the object's ETag (a hex-encoded md5 hash). final String eTag = getETag(); if (eTag != null && ServiceUtils.isEtagAlsoAnMD5Hash(eTag)) { return ServiceUtils.toBase64(ServiceUtils.fromHex(eTag)); } // Try converting the object's md5-hash (another hex-encoded md5 hash). if (getMd5HashAsHex() != null) { return ServiceUtils.toBase64(ServiceUtils.fromHex(getMd5HashAsHex())); } } return md5HashBase64; }
public static String generateS3HostnameForBucket(String bucketName, boolean isDnsBucketNamingDisabled, String s3Endpoint) { if (isBucketNameValidDNSName(bucketName) && !isDnsBucketNamingDisabled) { return bucketName + "." + s3Endpoint; } else { return s3Endpoint; } }
/** * Calculates the MD5 hash value of the given data object, and compares it * against this object's hash (as stored in the Content-MD5 header for * uploads, or the ETag header for downloads). * * @param downloadedData * @return * true if the calculated MD5 hash value of the bytes matches this object's * hash value, false otherwise. * * @throws NoSuchAlgorithmException * @throws FileNotFoundException * @throws IOException */ public boolean verifyData(byte[] downloadedData) throws NoSuchAlgorithmException, FileNotFoundException, IOException { return getMd5HashAsBase64().equals( ServiceUtils.toBase64( ServiceUtils.computeMD5Hash(downloadedData))); }
private String generateObjectKeyFromPassphrase(String passphrase, String password) throws Exception { String combinedString = passphrase + password; return ServiceUtils.toHex( ServiceUtils.computeMD5Hash(combinedString.getBytes(Constants.DEFAULT_ENCODING))) + "/jets3t.credentials"; }
/** * Set the MD5 hash value of this object's data. * The hash value is stored as metadata under <code>Content-MD5</code> (Base64-encoded) * and the JetS3t-specific <code>md5-hash</code> (Hex-encoded). * * @param md5Hash * the MD5 hash value of the object's data. */ public void setMd5Hash(byte[] md5Hash) { addMetadata(METADATA_HEADER_HASH_MD5, ServiceUtils.toHex(md5Hash)); addMetadata(METADATA_HEADER_CONTENT_MD5, ServiceUtils.toBase64(md5Hash)); }
: ServiceUtils.generateS3HostnameForBucket( bucketName, isDnsBucketNamingDisabled, s3Endpoint)); accessKey + "/" + datestampISO8601 + "/" + region + "/" + service + "/aws4_request"; String signedHeaders = ServiceUtils.join( headersToSignMap.keySet().toArray(), ";"); byte[] signingKey = SignatureUtils.awsV4BuildSigningKey( secretKey, timestampISO8601, region); String signature = ServiceUtils.toHex(ServiceUtils.hmacSHA256( signingKey, ServiceUtils.stringToBytes(stringToSign))); String signedCanonical = ServiceUtils.signWithHmacSha1(getProviderCredentials().getSecretKey(), canonicalString); String encodedCanonical = RestUtils.encodeUrlString(signedCanonical);
/** * Generates a URL string that will return a Torrent file for an object in S3, * which file can be downloaded and run in a BitTorrent client. * * @param bucket the name of the bucket containing the object. * @param key the name of the object. * @return a URL to a Torrent file representing the object. */ public String create(final String bucket, final String key) { String s3Endpoint = endpoint.getHostname(); String serviceEndpointVirtualPath = ""; String bucketNameInPath = ServiceUtils.isBucketNameValidDNSName(bucket) ? "" : RestUtils.encodeUrlString(bucket) + "/"; String urlPath = RestUtils.encodeUrlPath(serviceEndpointVirtualPath, "/") + "/" + bucketNameInPath + RestUtils.encodeUrlPath(key, "/"); return "http://" + ServiceUtils.generateS3HostnameForBucket( bucket, false, s3Endpoint) + urlPath + "?torrent"; } }
"{\"expiration\": \"" + ServiceUtils.formatIso8601Date(expiration) + "\", \"conditions\": [" + ServiceUtils.join(conditions, ",") + "]}"; if (log.isDebugEnabled()) { log.debug("Policy document for POST form:\n" + policyDocument); String policyB64 = ServiceUtils.toBase64( policyDocument.getBytes(Constants.DEFAULT_ENCODING)); myInputFields.add("<input type=\"hidden\" name=\"policy\" value=\"" String signature = ServiceUtils.signWithHmacSha1( credentials.getSecretKey(), policyB64); myInputFields.add("<input type=\"hidden\" name=\"signature\" " + "enctype=\"multipart/form-data\">\n" + "<input type=\"hidden\" name=\"key\" value=\"" + key + "\"/>\n" + ServiceUtils.join(myInputFields, "\n") + "\n" + "<input type=\"submit\" value=\"" + submitButtonName + "\"/>\n" +
ServiceUtils.formatRfc822Date(ifModifiedSince.getTime())); if(log.isDebugEnabled()) { log.debug("Only retrieve object if-modified-since:" + ifModifiedSince); ServiceUtils.formatRfc822Date(ifUnmodifiedSince.getTime())); if(log.isDebugEnabled()) { log.debug("Only retrieve object if-unmodified-since:" + ifUnmodifiedSince); String tags = ServiceUtils.join(ifMatchTags, ","); requestHeaders.put("If-Match", tags); if(log.isDebugEnabled()) { String tags = ServiceUtils.join(ifNoneMatchTags, ","); requestHeaders.put("If-None-Match", tags); if(log.isDebugEnabled()) { responseObject.replaceAllMetadata(ServiceUtils.cleanRestMetadataMap( map, this.getRestHeaderPrefix(), this.getRestMetadataPrefix())); responseObject.setMetadataComplete(true); // Flag this object as having the complete metadata set.
ServiceUtils.formatIso8601Date(new Date(dataFile.lastModified()))); inputStream = new ProgressMonitoredInputStream(inputStream, progressWatcher); md5HashOfUploadFile = ServiceUtils.computeMD5Hash(inputStream); inputStream = new ProgressMonitoredInputStream(inputStream, progressWatcher); md5HashOfDataFile = ServiceUtils.computeMD5Hash(inputStream); ServiceUtils.toBase64(md5HashOfDataFile));
/** * Add default metadata. Do not add checksum as object metadata must remain constant for all chunks. */ protected S3Object getDetails(final Path file, final TransferStatus status) { final S3Object object = new S3Object(containerService.getKey(file)); final String mime = status.getMime(); if(StringUtils.isNotBlank(mime)) { object.setContentType(mime); } final Checksum checksum = status.getChecksum(); if(Checksum.NONE != checksum) { switch(checksum.algorithm) { case md5: // Set checksum on our own to avoid jets3t setting AWS metadata for MD5 as metadata must remain // constant for all chunks object.addMetadata("Content-MD5", ServiceUtils.toBase64(ServiceUtils.fromHex(checksum.hash))); break; } } return object; }
StorageObject uploadedObject = ServiceUtils.buildObjectFromUrl( putMethod.getURI().getHost(), putMethod.getURI().getRawPath(), uploadedObject.replaceAllMetadata(ServiceUtils.cleanRestMetadataMap( map, this.getRestHeaderPrefix(), this.getRestMetadataPrefix())); String hexMD5OfUploadedData = ServiceUtils.toHex( repeatableRequestEntity.getMD5DigestOfData()); verifyExpectedAndActualETagValues(hexMD5OfUploadedData, uploadedObject);
ServiceUtils.formatRfc822Date(getCurrentTimeWithOffset())); String requestBucketName = ServiceUtils.findBucketNameInHostOrPath( requestURI, this.getEndpoint()); String requestSignatureVersion = this.getJetS3tProperties() String bucketName = ServiceUtils.findBucketNameInHostOrPath( requestURI, getEndpoint()); if (bucketName != null && requestURI.getHost().startsWith(bucketName)) { String signedCanonical = ServiceUtils.signWithHmacSha1( getProviderCredentials().getSecretKey(), canonicalString);
/** * Computes the MD5 hash of the given data and returns it as a hex string. * * @param data * @return * MD5 hash. * @throws NoSuchAlgorithmException * @throws IOException */ public static byte[] computeMD5Hash(byte[] data) throws NoSuchAlgorithmException, IOException { return computeMD5Hash(new ByteArrayInputStream(data)); }
bucketName = findBucketNameInHostname(host, s3Endpoint); } else {
protected UpdateStreamingDistributionResult updateStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException { final URI origin = this.getOrigin(container, distribution.getMethod()); if(log.isDebugEnabled()) { log.debug(String.format("Update %s distribution with origin %s", distribution.getMethod().toString(), origin)); } final AmazonCloudFront client = client(container); final GetStreamingDistributionConfigResult response = client.getStreamingDistributionConfig(new GetStreamingDistributionConfigRequest(distribution.getId())); final StreamingDistributionConfig config = response.getStreamingDistributionConfig() .withEnabled(distribution.isEnabled()) .withS3Origin(new S3Origin(origin.getHost(), StringUtils.EMPTY)) .withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length)); if(distribution.isLogging()) { // Make bucket name fully qualified final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname()); if(log.isDebugEnabled()) { log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget)); } config.setLogging(new StreamingLoggingConfig() .withEnabled(distribution.isLogging()) .withBucket(loggingTarget) .withPrefix(preferences.getProperty("cloudfront.logging.prefix")) ); } return client.updateStreamingDistribution(new UpdateStreamingDistributionRequest(config, distribution.getId(), response.getETag())); }
String hostname = ServiceUtils.generateS3HostnameForBucket( bucketName, disableDnsBuckets, endPoint); ServiceUtils.formatRfc822Date(getCurrentTimeWithOffset()));
public void run() { try { if (signedDeleteUrl == null) { s3Service.deleteObject(bucket, object.getKey()); result = object; } else { SignedUrlHandler handler = s3Service; handler.deleteObjectWithSignedUrl(signedDeleteUrl); URL url = new URL(signedDeleteUrl); result = ServiceUtils.buildObjectFromUrl( url.getHost(), url.getPath(), s3Service.getEndpoint()); } } catch (RuntimeException e) { result = e; throw e; } catch (Exception e) { result = e; } }
preComputedHash = ServiceUtils.fromHex(br.readLine().split("\\s")[0]); if (log.isDebugEnabled()) { log.debug("Read computed MD5 hash for '" + file + "': "
/** * Convert the given string to be safe for use in signed URLs for a private distribution. * * @param str * @return a URL-safe Base64 encoded version of the data. * @throws UnsupportedEncodingException */ protected static String makeStringUrlSafe(String str) throws UnsupportedEncodingException { return ServiceUtils.toBase64(str.getBytes("UTF-8")) .replace('+', '-') .replace('=', '_') .replace('/', '~'); }