javagoogle-app-engineupload

Upload file in appEngine really needed


I've started a project, it's a web application to let the world listen all of my personnal songs. So far, all the songs available on the website are stored in war/songs. Then, my database only stores the path to this file and that works really nicely.

I wanted to created a web page only accessible to admin (basically me :D) to upload my songs at this location, so I don't have to deploy the entire project just to add a song ... And I am now reading that it's impossible in app engine ? I don't think storing the songs in the database is a good idea ?

Can I have you thoughts on my issue ? Thanks a lot !

Also, if app engine is not suited to my goal, I wanted to know some good place to host my java application ... that seems quite rare !

Thanks again ..


Solution

  • It is possible on GAE. Simply you have to store your files in Blobstore, not in filesystem. Instead of path you could use blob key. You can upload your files like this:

    <body>
    <form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
    

    And you can set in web.xml constraint to prevent access to your site:

    <security-constraint>
        <web-resource-collection>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
    

    And then to serve file from servlet:

    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
        blobstoreService.serve(blobKey, res);