Put Bucket operation creates a new bucket in QueryIO cluster. To create a new bucket, you must be a registered user and must authenticate to QueryIO server.
Following code creates a new bucket using S3 compatible api's.
A HttpURLConnection object is used to create a PUT request to create new bucket. Java URL is created by appending the bucket name at the end of serverURL and assigned to HttpURLConnection's object.
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
public class BucketOperations {
/**
* This program creates a new bucket.
* @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.
* @bucketName: Name of the bucket to be created.
*/
public void putBucket(String serverURL, String token, String bucketName)
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 + bucketName); //creates a URL with appending bucket name at end
//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("PUT"); //PUT 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(); //response of PUT bucket operation
// Process response here
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
System.out.println(writer.toString()); //Displaying response
}
} finally { //close all streams
try {
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (httpCon != null) {
httpCon.disconnect();
}
}
}
}