【问题标题】:Uploading to BlobStore in Google App Engine from a Command Line Java Application从命令行 Java 应用程序上传到 Google App Engine 中的 BlobStore
【发布时间】:2011-02-18 15:03:34
【问题描述】:

这是我的 blobstore 上传命令行应用程序的服务器端代码:

public class UploadServlet extends HttpServlet
{
    private static final Logger log = Logger.getLogger(UploadServlet.class.getName());

    private final BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();

    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
    {
        final Map<String, BlobKey> blobs = bs.getUploadedBlobs(request);
        final BlobKey blobKey = blobs.get("blob");

        if (blobKey == null)
        {
            log.severe("BlobKey was null!");
            response.sendRedirect("/error.html");
        }
        else
        {
            response.sendRedirect("/image?blob-key=" + blobKey.getKeyString());
        }
    }

    /**
     * Generates the custom single use blobstore URL's that are needed to upload to the blobstore programmatically. */
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
    {
        final String uploadURL = bs.createUploadUrl("/upload");
        final PrintWriter pw = response.getWriter();
        response.setContentType("text/plain");
        pw.write(uploadURL);
    }
}

我已经获得了以下代码来针对我的本地开发模式服务器工作,没有身份验证代码,所以我知道 multipart/form 代码工作正常,使用身份验证代码,它失败了:

    r = opener.open(request)
  File "C:\Python26\lib\urllib2.py", line 397, in open
    response = meth(req, response)
  File "C:\Python26\lib\urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python26\lib\urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 302: Found

命令行客户端从 Java 迁移到 Python:

f = urllib2.urlopen('http://myapp.appspot.com/upload')
bloburl = f.read(1024)
print bloburl
print

image = file('120.jpg', 'r')
form = MultiPartForm()
form.add_file('blob', 'blob', image, 'image/jpeg')

request = urllib2.Request(bloburl)
body = str(form)
request.add_header('Content-type', form.get_content_type())
request.add_header('Content-length', len(body))
request.add_data(body)

opener = auth.get_auth_opener('myapp', 'username', 'password')
r = opener.open(request)
data = r.read()
print data

我只想要一个简单的命令行工具,它可以获取一个文件并将其发布到 BlobStore。我在 Internet 上的任何地方都找不到一个 COMPLETE 示例。有很多示例可以在 GAE 上完成所有工作,但没有一个是命令行客户端,它们从单独的客户端执行 POSTFORM

【问题讨论】:

  • 您是否尝试过转储客户端代码的输出以验证它是否包含正确的标头?
  • 我在哪里可以获得有关正确标题应该是什么的信息?
  • 我也想要这个。我很确定 POST 没有任何特定要求(就标题而言),因为它可以通过正常的浏览器表单上传来完成;它只需要一个由BlobstoreService.createUploadURL 创建的URL。我看到的问题是您的应用可能需要用户登录才能执行上传,因此命令行应用需要获取 Google cookie。

标签: java python google-app-engine command-line blobstore


【解决方案1】:

Since version 1.4.3 There is an experimental API 直接写入 blobstore。
它使您无需使用 POST 上传到 blobstore。

【讨论】:

    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2015-02-07
    • 2013-09-23
    • 1970-01-01
    • 2013-06-27
    • 2019-05-16
    相关资源
    最近更新 更多