Delete Object

Following sample deletes an object from the bucket.

Delete object using S3 Compatible REST API

DELETE object operation is supported by S3 compatible server using java.net.HttpURLConnection. A DELETE request is created for the object using URL(ObjectName appended to it).

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

import org.apache.http.HttpStatus;

public class ObjectOperations {
	
		/*	
	 	 * This program will delete a object from the 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.
		 * @param bucketName: Name of the bucket from which object will be deleted
		 * @param objectName: Name of the object to be deleted
		 */
	public void deleteObject(String serverURL, String token, String bucketName,
			String objectName) throws Exception {
		URL url = null;
		HttpURLConnection httpCon = null;

		try {
			/* append "/" at end of serverURL */
			if (!serverURL.endsWith("/")) {
				serverURL = serverURL + "/";
			}
			url = new URL(serverURL + bucketName + "/" + objectName);	//creates a URL with appending bucket name and objectName 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("DELETE");	//DELETE 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_NO_CONTENT) {	//Object deleted if respone code is HttpStatus.SC_NO_CONTENT
				// Object deleted successfully
				System.out.println("\nObject deleted successfully");
			}
			else
			{
				System.out.println("\nFailed to delete object");
			}
		} finally {
			if (httpCon != null) {
				httpCon.disconnect();
			}
		}
	}
}
	


Copyright 2017 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.