IT Certificaions

Google Cloud Platform

Wednesday, September 5, 2012

Upload file on Google cloud storage using Java servlet on google app engine



Hopefully you have configured all jars and settings to run and deploy your project on google app engine using eclipse,

if not then visit google app engine-->google cloud storage java api link..
https://developers.google.com/appengine/docs/java/googlestorage/overview

-----------------------------------------------------------------------------
1) index.html
------------------------------------------------------------------------------

<html>
<body>
   <div>
      <form action="testStorage.do" method="post" enctype="multipart/form-data">
         <p> Please write a file to test cloud storage... </p>
         <input type="file" name="cloudFile" />
         <input type="submit" value="Write a  file on cloud Storage">
      </form>
   </div>
</body>
</html>

--------------------------------------------------------------------------------
2) web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

   <servlet>
<servlet-name>CloudStorageServlet</servlet-name>
<servlet-class>com.vicwalks.web.servlet.TestCloudStorageServlet</servlet-class>
</servlet>
  
<servlet-mapping>
<servlet-name>CloudStorageServlet</servlet-name>
<url-pattern>/testStorage.do</url-pattern>
</servlet-mapping>
  
<welcome-file-list>
   <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
-------------------------------------------------------

3) appengine-web.xml


<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>your-app-engine-id</application>
  <version>1</version>



  <threadsafe>true</threadsafe>
  <sessions-enabled>true</sessions-enabled>

  <!-- Configure java.util.logging -->
  <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  </system-properties>

  <!--
    HTTP Sessions are disabled by default. To enable HTTP sessions specify:

      <sessions-enabled>true</sessions-enabled>

    It's possible to reduce request latency by configuring your application to
    asynchronously write HTTP session data to the datastore:

      <async-session-persistence enabled="true" />

    With this feature enabled, there is a very small chance your app will see
    stale session data. For details, see
    http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions
  -->

</appengine-web-app>



=====================================================================

4) TestCloudStorageServlet.java

------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;


import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class TestCloudStorageServlet extends HttpServlet{

private static final long serialVersionUID = 1L;
private StorageService storage = new StorageService();
private static final int BUFFER_SIZE = 1024 * 1024;
private static final Logger log = Logger.getLogger(TestCloudStorageServlet.class.getName()); 
@Override
 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   log.info(this.getServletInfo()+" Servlets called....");
   resp.setContentType("text/plain");
   resp.getWriter().println("Now see here your file content, that you have uploaded on storage..");
   
   ServletFileUpload upload = new ServletFileUpload();
   FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
       FileItemStream item = iter.next();
       String fileName = item.getName();
       String mime = item.getContentType();
       
       storage.init(fileName, mime);
       InputStream is = item.openStream();

       byte[] b = new byte[BUFFER_SIZE];
       int readBytes = is.read(b, 0, BUFFER_SIZE);
       while (readBytes != -1) {
           storage.storeFile(b, readBytes);
           readBytes = is.read(b, 0, readBytes);
       }

       is.close();
       storage.destroy();
       
resp.getWriter().println("File uploading done");

       //resp.getWriter().println("READ:" + storage.readTextFileOnly(fileName));
       
                         log.info(this.getServletName()+" ended....");        

}
} catch (FileUploadException e) {
System.out.println("FileUploadException::"+e.getMessage());
log.severe(this.getServletName()+":FileUploadException::"+e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.severe(this.getServletName()+":Exception::"+e.getMessage());
System.out.println("Exception::"+e.getMessage());
e.printStackTrace();
}
}

}

========================================================================

5) StorageService.java

--------------------------------------------------------------------------
import com.google.appengine.api.files.*;
import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;
import java.io.*;
import java.nio.channels.Channels;
import java.util.logging.Logger;

public class StorageService {

public static final String BUCKET_NAME = "mybucket";  

private FileWriteChannel writeChannel = null;
FileService fileService = FileServiceFactory.getFileService();
private OutputStream os = null;
private static final Logger log = Logger.getLogger(StorageService.class.getName());

public void init(String fileName, String mime) throws Exception {
System.out.println("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
log.info("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
    
    GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
            .setAcl("public_read")
            .setBucket(BUCKET_NAME)
            .setKey(fileName)
            .setMimeType(mime);
    AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
    boolean lock = true;
    writeChannel = fileService.openWriteChannel(writableFile, lock);
    os = Channels.newOutputStream(writeChannel);
}

public void storeFile(byte[] b, int readSize) throws Exception {
    os.write(b, 0, readSize);
    os.flush();
}



// Only to read uploaded text files

public String readTextFileOnly(String fileName) throws Exception{  
log.info("Reading the txt file from google cloud storage...........");
String filename = "/gs/" + BUCKET_NAME + "/" + fileName;
    AppEngineFile readableFile = new AppEngineFile(filename);
    FileReadChannel readChannel = fileService.openReadChannel(readableFile, false);
    BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
    String line = reader.readLine();
    readChannel.close();
    return line;

}
public void destroy() throws Exception {
log.info("Storage service: destroy() method");
    os.close();
    writeChannel.closeFinally();
}
}
========================================================================

Now deploy your project on app engine and upload file.