lk.startsWith(AMAZON_HEADER_PREFIX)) { List s=(List)headers.get(hashKey); interestingHeaders.put(lk, concatenateList(s));
public URL getURL(boolean isSecure, String server, int port, String bucket, String key, Map pathArgs) throws MalformedURLException { if(bucket == null || bucket.length() == 0) { //The bucket is null, this is listAllBuckets request String pathArguments=Utils.convertPathArgsHashToString(pathArgs); return new URL(isSecure? "https" : "http", server, port, "/" + pathArguments); } else { String serverToUse=getServer(server, bucket); String pathBase=getPathBase(bucket, key); String pathArguments=Utils.convertPathArgsHashToString(pathArgs); return new URL(isSecure? "https" : "http", serverToUse, port, pathBase + pathArguments); } } }
public ListBucketResponse(HttpURLConnection connection) throws IOException { super(connection); if(connection.getResponseCode() < 400) { try { XMLReader xr=Utils.createXMLReader(); ListBucketHandler handler=new ListBucketHandler(); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(connection.getInputStream())); this.name=handler.getName(); this.prefix=handler.getPrefix(); this.marker=handler.getMarker(); this.delimiter=handler.getDelimiter(); this.maxKeys=handler.getMaxKeys(); this.isTruncated=handler.getIsTruncated(); this.nextMarker=handler.getNextMarker(); this.entries=handler.getKeyEntries(); this.commonPrefixEntries=handler.getCommonPrefixEntries(); } catch(SAXException e) { throw new RuntimeException("Unexpected error parsing ListBucket xml", e); } } }
lk.startsWith(AMAZON_HEADER_PREFIX)) { List s=(List)headers.get(hashKey); interestingHeaders.put(lk, concatenateList(s));
headers.put("x-amz-acl", Collections.singletonList("public-read")); return Utils.generateQueryStringAuthentication(awsAccessKey, awsSecretAccessKey, method, bucket, key, new HashMap(), headers, expirationDate);
return urlencode(b64);
S3Object object) throws IOException { CallingFormat format=Utils.getCallingFormatForBucket(this.callingFormat, bucket); if(isSecure && format != CallingFormat.getPathCallingFormat() && bucket.contains(".")) { System.err.println("You are making an SSL connection, however, the bucket contains periods and the wildcard certificate will not match by default. Please consider using HTTP.");
/** * Parse the response to a ?location query. */ public LocationResponse(HttpURLConnection connection) throws IOException { super(connection); if(connection.getResponseCode() < 400) { try { XMLReader xr=Utils.createXMLReader(); ; LocationResponseHandler handler=new LocationResponseHandler(); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(connection.getInputStream())); this.location=handler.loc; } catch(SAXException e) { throw new RuntimeException("Unexpected error parsing ListAllMyBuckets xml", e); } } else { this.location="<error>"; } }
public ListBucketResponse(HttpURLConnection connection) throws IOException { super(connection); if(connection.getResponseCode() < 400) { try { XMLReader xr=Utils.createXMLReader(); ListBucketHandler handler=new ListBucketHandler(); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(connection.getInputStream())); this.name=handler.getName(); this.prefix=handler.getPrefix(); this.marker=handler.getMarker(); this.delimiter=handler.getDelimiter(); this.maxKeys=handler.getMaxKeys(); this.isTruncated=handler.getIsTruncated(); this.nextMarker=handler.getNextMarker(); this.entries=handler.getKeyEntries(); this.commonPrefixEntries=handler.getCommonPrefixEntries(); } catch(SAXException e) { throw new RuntimeException("Unexpected error parsing ListBucket xml", e); } } }
/** * Add the appropriate Authorization header to the HttpURLConnection. * @param connection The HttpURLConnection to which the header will be added. * @param method The HTTP method to use (GET, PUT, DELETE) * @param bucket the bucket name this request is for * @param key the key this request is for * @param pathArgs path arguments which are part of this request */ private void addAuthHeader(HttpURLConnection connection, String method, String bucket, String key, Map pathArgs) { if(connection.getRequestProperty("Date") == null) { connection.setRequestProperty("Date", httpDate()); } if(connection.getRequestProperty("Content-Type") == null) { connection.setRequestProperty("Content-Type", ""); } if(this.awsAccessKeyId != null && this.awsSecretAccessKey != null) { String canonicalString= Utils.makeCanonicalString(method, bucket, key, pathArgs, connection.getRequestProperties()); String encodedCanonical=Utils.encode(this.awsSecretAccessKey, canonicalString, false); connection.setRequestProperty("Authorization", "AWS " + this.awsAccessKeyId + ":" + encodedCanonical); } }
/** * Write a new ACL for a given object * @param aclXMLDoc The xml representation of the ACL as a String * @param bucket The name of the bucket where the object lives. * @param key The name of the key to use. * @param headers A Map of String to List of Strings representing the http * headers to pass (can be null). */ public Response putACL(String bucket, String key, String aclXMLDoc, Map headers) throws IOException { S3Object object=new S3Object(aclXMLDoc.getBytes(), null); Map pathArgs=new HashMap(); pathArgs.put("acl", null); HttpURLConnection request= makeRequest("PUT", bucket, Utils.urlencode(key), pathArgs, headers, object); request.setDoOutput(true); request.getOutputStream().write(object.data == null? new byte[]{} : object.data); return new Response(request); }
/** * Lists the contents of a bucket. * @param bucket The name of the bucket to list. * @param prefix All returned keys will start with this string (can be null). * @param marker All returned keys will be lexographically greater than * this string (can be null). * @param maxKeys The maximum number of keys to return (can be null). * @param delimiter Keys that contain a string between the prefix and the first * occurrence of the delimiter will be rolled up into a single element. * @param headers A Map of String to List of Strings representing the http * headers to pass (can be null). */ public ListBucketResponse listBucket(String bucket, String prefix, String marker, Integer maxKeys, String delimiter, Map headers) throws IOException { Map pathArgs=Utils.paramsForListOptions(prefix, marker, maxKeys, delimiter); return new ListBucketResponse(makeRequest("GET", bucket, "", pathArgs, headers)); }
/** * Validate bucket-name */ static boolean validateBucketName(String bucketName, CallingFormat callingFormat) { if(callingFormat == CallingFormat.getPathCallingFormat()) { final int MIN_BUCKET_LENGTH=3; final int MAX_BUCKET_LENGTH=255; final String BUCKET_NAME_REGEX="^[0-9A-Za-z\\.\\-_]*$"; return null != bucketName && bucketName.length() >= MIN_BUCKET_LENGTH && bucketName.length() <= MAX_BUCKET_LENGTH && bucketName.matches(BUCKET_NAME_REGEX); } else { return isValidSubdomainBucketName(bucketName); } }
public ListAllMyBucketsResponse(HttpURLConnection connection) throws IOException { super(connection); if(connection.getResponseCode() < 400) { try { XMLReader xr=Utils.createXMLReader(); ; ListAllMyBucketsHandler handler=new ListAllMyBucketsHandler(); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(connection.getInputStream())); this.entries=handler.getEntries(); } catch(SAXException e) { throw new RuntimeException("Unexpected error parsing ListAllMyBuckets xml", e); } } }
/** * Get the ACL for a given object (or bucket, if key is null). * @param bucket The name of the bucket where the object lives. * @param key The name of the key to use. * @param headers A Map of String to List of Strings representing the http * headers to pass (can be null). */ public GetResponse getACL(String bucket, String key, Map headers) throws IOException { if(key == null) key=""; Map pathArgs=new HashMap(); pathArgs.put("acl", null); return new GetResponse( makeRequest("GET", bucket, Utils.urlencode(key), pathArgs, headers) ); }
/** * Writes an object to S3. * @param bucket The name of the bucket to which the object will be added. * @param key The name of the key to use. * @param object An S3Object containing the data to write. * @param headers A Map of String to List of Strings representing the http * headers to pass (can be null). */ public Response put(String bucket, String key, S3Object object, Map headers) throws IOException { HttpURLConnection request= makeRequest("PUT", bucket, Utils.urlencode(key), null, headers, object); request.setDoOutput(true); request.getOutputStream().write(object.data == null? new byte[]{} : object.data); return new Response(request); }
public URL getURL(boolean isSecure, String server, int port, String bucket, String key, Map pathArgs) throws MalformedURLException { if(bucket == null || bucket.length() == 0) { //The bucket is null, this is listAllBuckets request String pathArguments=Utils.convertPathArgsHashToString(pathArgs); return new URL(isSecure? "https" : "http", server, port, "/" + pathArguments); } else { String serverToUse=getServer(server, bucket); String pathBase=getPathBase(bucket, key); String pathArguments=Utils.convertPathArgsHashToString(pathArgs); return new URL(isSecure? "https" : "http", serverToUse, port, pathBase + pathArguments); } } }
static Map paramsForListOptions(String prefix, String marker, Integer maxKeys) { return paramsForListOptions(prefix, marker, maxKeys, null); }
static String makeCanonicalString(String method, String bucket, String key, Map pathArgs, Map headers) { return makeCanonicalString(method, bucket, key, pathArgs, headers, null); }
static Map paramsForListOptions(String prefix, String marker, Integer maxKeys, String delimiter) { Map argParams=new HashMap(); // these three params must be url encoded if(prefix != null) argParams.put("prefix", urlencode(prefix)); if(marker != null) argParams.put("marker", urlencode(marker)); if(delimiter != null) argParams.put("delimiter", urlencode(delimiter)); if(maxKeys != null) argParams.put("max-keys", Integer.toString(maxKeys.intValue())); return argParams; }