使用Google云端存储HTML表单的POST / PUT对象上传文件 [英] Upload file using POST/PUT Object of Google Cloud Storage HTML Form

查看:220
本文介绍了使用Google云端存储HTML表单的POST / PUT对象上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Eclipse的Google-App-Engine Plugin开发一个非常简单的Web应用程序。
我刚刚在我的home.html的HTML中编写了这段代码。



这里的home.html是:

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/ TR / HTML4 / loose.dtd> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = US-ASCII>
< title>在此插入标题< / title>
< / head>
< body>
< form action =http://storage.googleapis.com/myBucketNamemethod =postname =putFileid =putFile
enctype =multipart / form-data >
< input type =hiddenname =keyvalue =我不知道这里会发生什么,因为我无法猜测我要上传哪个文件/>
< input type =hiddenname =success_action_statusvalue =201/>
< input type =filename =fileid =file>
< input type =submitvalue =上传>
< / form>
< / body>
< / html>

这是一个非常简单的表单,没有任何形式的授权, ,您可以看到: https://developers.google.com/storage/ docs / reference-methods#postobject (我已经记住了)。
$ b 我有不同的来自Google(XML格式)的响应错误:

<1>


 <错误> 
<代码> InvalidArgument< / Code>
< Message>无效参数< / Message>
<详细>缺少文件部分< /详细>
< / Error>



<2>


 <错误> 
<代码> InvalidArgument< / Code>
< Message>无效参数< / Message>
<详细信息>无法使用POST创建桶。< / Details>
< / Error>



<3>

 <错误> 
<代码> InvalidArgument< / Code>
< Message>无效参数< / Message>
<详情>
无效的查询参数:[file,success_action_status,key]
< / Details>
< / Error>

任何人都可以帮我填写这张表格吗?

具体来说:
它适用于这种形式:

 < form action = http://storage.googleapis.com/bucketNamemethod =postname =putFileid =putFile
enctype =multipart / form-data>
< input type =hiddenname =bucketvalue =bucketName>
< input type =hiddenname =keyvalue =fileUploadingName/>
< input type =hiddenname =success_action_statusvalue =201/>
< input type =filename =file>
< input type =submitvalue =上传>
< / form>

fileUploadingName是例如:sample.txt

但问题依然存在,因为我需要弄清正确的密钥,即我要上传的文件的名称。

解决方案

这是一个工作表单的例子:

 < form action =http://storage.googleapis.com/bucketName
method =postenctype =multipart / form-data>
< input type =hiddenname =keyvalue =$ {filename}/>
< input type =hiddenname =success_action_statusvalue =201/>
< input type =filename =file>
< input type =submitvalue =上传>
< / form>

请注意,没有隐藏存储桶字段,因为存储桶名称已经包含在表单POST URL中。



关键参数可以是 $ {filename} which将采取正在上传的文件的原始名称。但是,如果您尝试上传两个同名的文件,则会被另一个文件覆盖。



另外,不要忘记您的存储桶需要具有PUBLIC_WRITE ACL。你可以用gsutil来设置它,例如:

  gsutil acl set public-read-write gs:// bucketName 


I'm developing a VERY simple Web-App with Google-App-Engine Plugin for Eclipse. I've just written this snippet of code in HTML of my "home.html".

Here home.html is:

<!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=US-ASCII">
<title>Insert title here</title>
</head>
<body>
<form action="http://storage.googleapis.com/myBucketName" method="post" name="putFile" id="putFile"
            enctype="multipart/form-data">
            <input type="hidden" name="key" value="I don't know what is going here because i cannot guess which file i will upload" />
            <input type="hidden" name="success_action_status" value="201" />
            <input type="file" name="file" id="file">
            <input type="submit" value="Upload">
        </form>
</body>
</html>

This is a very simple Form without any kind of Authorization or whatever.. For Info, you could see this: https://developers.google.com/storage/docs/reference-methods#postobject (that i've already memorized).

I have had different Response Error from Google (in XML Format) like:

1)

<Error>
<Code>InvalidArgument</Code>
<Message>Invalid argument.</Message>
<Details>Missing file part</Details>
</Error>

2)

<Error>
<Code>InvalidArgument</Code>
<Message>Invalid argument.</Message>
<Details>Cannot create buckets using a POST.</Details>
</Error>

3)

<Error>
<Code>InvalidArgument</Code>
<Message>Invalid argument.</Message>
<Details>
Invalid query parameter(s): [file, success_action_status, key]
</Details>
</Error>

Can anyone help me to fill in this form, please?

To be more specific: It works with this form:

<form action="http://storage.googleapis.com/bucketName" method="post" name="putFile" id="putFile"
            enctype="multipart/form-data">
            <input type="hidden" name="bucket" value="bucketName">
            <input type="hidden" name="key" value="fileUploadingName" />
            <input type="hidden" name="success_action_status" value="201" />
            <input type="file" name="file">
            <input type="submit" value="Upload">
        </form>

The fileUploadingName is for example: sample.txt

but the problem persists because i need to fiil the correct "key" that is the name of the file that i'm going to Upload. How can i fix it?

解决方案

Here's a working form example:

<form action="http://storage.googleapis.com/bucketName" 
      method="post" enctype="multipart/form-data">
    <input type="hidden" name="key" value="${filename}" />
    <input type="hidden" name="success_action_status" value="201" />
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>

Note that there's no hidden bucket field as the bucket name is already included in the form POST URL.

The key parameter can be ${filename} which will take the original name of the file being uploaded. Though if you try to upload two files with the same name one will get overwritten by the other.

Also, don't forget that your bucket needs to have PUBLIC_WRITE acl. You can set it with gsutil, e.g.:

gsutil acl set public-read-write gs://bucketName

这篇关于使用Google云端存储HTML表单的POST / PUT对象上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆