2

目标是上传大文件(视频)并为它们获取公共共享 url。

看起来这很简单,但我花了一天多的时间查看文档,但我没有找到任何示例。

我得到以下代码来上传到谷歌商店,它工作正常,但我想在 url 中添加选项来制作文件的 acl:“public-read”。在 jsp 中上传之前或在 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>

然后在我的 servlet 某处重定向 url 以生成 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();

}

在这一步,如果可能的话,我想拥有谷歌云存储的公共共享 URL。(我无法通过 servlet 提供文件,因为它可能会超时)

4

1 回答 1

1

默认情况下,使用 createUploadUrl 上传到 bigstore 的文件是私有的。您需要自己修改 ACL 才能将其公开。

此外,您可以使用 serve() 从您的 servlet 从 Google 存储返回无限大小的 blob,如果您希望这样做而不是公开 blob,则无需担心超时。

于 2012-10-25T01:21:01.463 回答