Get list of all Buckets

GET Service operation is used to fetch the list of all the buckets in root directory.

Get list of all Buckets using S3 Compatible REST API

The code given below fetches the list of all the top level buckets in HDFS using S3 compatible APIs.

java.net.HttpURLConnection's object is used to create a GET request for the serverURL. Authorization token is provided for authentication.

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpStatus;

public class ServiceOperations {
	
		/*
	 	 * This program fetches list of all buckets
		 * @param serverURL: URL of S3 Compatible REST server(http://<S3 server IP> : <S3 server port> /queryio/). For example: http://192.168.0.1:5667/queryio/
		 * @param token: authorization token.
		 */
	public void getService(String serverURL, String token) throws Exception {
		URL url = null;
		HttpURLConnection httpCon = null;
		InputStream is = null;
		try {
			/* append "/" at end of serverURL */
			if (!serverURL.endsWith("/")) {
				serverURL = serverURL + "/";
			}	
			url = new URL(serverURL);	//URL object with serverURL

			//Returns a URLConnection object that represents a connection to the remote object referred to by the URL.
			httpCon = (HttpURLConnection) url.openConnection();

			httpCon.setDoOutput(true);	// to use the URL connection for output
			httpCon.setRequestMethod("GET");	//GET request is used.

			httpCon.setRequestProperty("authorization", token);	//Provides token for authorization

			httpCon.connect();	//Opens a communications link to the resource reference by the URL

			if (httpCon.getResponseCode() == HttpStatus.SC_OK) {	//Check for OK response code
				is = httpCon.getInputStream();
				// Process response here
				StringWriter writer = new StringWriter();                 
				IOUtils.copy(is, writer, "UTF-8");                 
				System.out.println(writer.toString());	//Displaying response
			}
		} finally {
			try {
				if (is != null)
					is.close();	//close InputStream
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (httpCon != null) {
				httpCon.disconnect();	//disconnect connection	
			}
		}
	}
}
	
	


Copyright ©
  • Contact Us
  • Contact Us
  • 2018 QueryIO Corporation. All Rights Reserved.

    QueryIO, "Big Data Intelligence" and the QueryIO Logo are trademarks of QueryIO Corporation. Apache, Hadoop and HDFS are trademarks of The Apache Software Foundation.