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.

Comments

  1. how to read files from google storage cloud using java servlet on google app engine????

    ReplyDelete
  2. See this,
    http://knowledge-serve.blogspot.in/2013/03/read-file-from-google-cloud-storage.html

    ReplyDelete
  3. thanks....
    i got another error while importing org.apache.commons.fileupload in TestCloudStorageServlet.java
    and i m using eclipse juno version...
    plz help....

    ReplyDelete
  4. Can we upload files other than images and videos say like web pages and host our website on Google drive.

    ReplyDelete
    Replies
    1. set response as a text\html.

      resp.setContentType("text/html");

      Delete
    2. This comment has been removed by the author.

      Delete
  5. hi i am getting error while deploying to cloud as
    Error: Server Error

    The server encountered an error and could not complete your request.
    If the problem persists, please report your problem and mention this error message and the query that caused it.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Add common.io.* and common.fileupload.* in web-inf/lib

      Delete
  6. What should I do if I want to upload a .zip file?

    ReplyDelete
    Replies
    1. See this
      http://knowledge-serve.blogspot.in/2014/04/writing-zip-file-on-google-app-engine.html

      Delete
  7. how to read images,pdf ,videos using java servlet in google app engine ?

    ReplyDelete
  8. I am getting the folllowing error when i implemented the above code:
    Error: Server Error

    The server encountered an error and could not complete your request.
    Please try again in 30 seconds.

    ReplyDelete
    Replies
    1. You can share your screen shots that can explain better.

      Delete
    2. Hi, this is old code, I can share you latest code if you are still facing this issue.

      Delete
  9. Hi.. i got a success msg when i tried running the code.. but when i checked in my console..no bucket has been created n no file has been uploaded.. where am i going wrong?

    ReplyDelete
  10. Have you done cloud storage authentication from app engine console properly?

    ReplyDelete
  11. how to read an image file?

    ReplyDelete

Post a Comment

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Struts 2 - struts tag with boolean property