Jerome Ansia
Jerome Ansia

Reputation: 6892

GAE - Google Cloud Storage - Upload (acl : read-public and get the public shared url)

The goal is to upload large file (video) and get the public shared url for them.

Looks like it's pretty straightforward but i spend a bit more than one day going into the documentation and i didn't find any sample of that.

I get the following code to make an upload to Google Store which works fine, but i would like to add the option in the url to make the acl of the file : "public-read". Either prior the upload in the jsp or after in the servlet.

<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%@ page import="com.google.appengine.api.blobstore.UploadOptions" %>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

String uploadUrl = blobstoreService.createUploadUrl("/ajax?act=user&act2=video_upload", UploadOptions.Builder.withGoogleStorageBucketName("vidaao"));

%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>
    <h1>Upload v3</h1>

    <form name="form1" id="form1" action="<% out.print(uploadUrl); %>" method="post" enctype="multipart/form-data" target="upload_iframe">
    <input type="hidden" name="hiddenfield1" value="ok">
    Files to upload:
    <br/>
    <input type="file" name="myFile">
    <br/>
        <button type="submit">Send</button>
    </form>
    <iframe id="upload_iframe" name="upload_iframe"></iframe>

</body>

</html>

Then in my servlet somewhere the redirect url ends there with the generation of the blobkey

public String upload(HttpServletRequest req, HttpServletResponse res) throws Exception{

    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
    BlobKey blobKey = blobs.get("myFile");

    if (blobKey == null) {

        throw new Exception("Error file not uploaded");

    }

            //TODO: HERE get the public shared url of the file

    return " blob key = " + blobKey.getKeyString();

}

And at this step i would like to have the public shared url for the Google Cloud Storage, if it's possible. (I cannot serve the file through a servlet because it might time out)

Upvotes: 2

Views: 860

Answers (1)

Stuart Langley
Stuart Langley

Reputation: 7054

By default files uploaded to bigstore using createUploadUrl are private. You would need to modify the ACL yourself to make it public.

Also, you can use serve() to return blobs of unlimited size from Google Storage from your servlet without concerns for timeout if you would prefer to do it that way rather than making the blobs public.

Upvotes: 1

Related Questions