Upload file using POST/PUT Object of Google Cloud

2019-08-04 13:07发布

问题:

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?

回答1:

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


回答2:

It is possible to upload files without allowing public write. I have found it is easier to use Google Cloud Platform's AWS S3 Interoperability for this kind of thing, because it uses a simpler authentication scheme than the typical GCloud OAuth.

To use S3 interop, go to Bucket Settings in the Google Cloud Platform Developer Console and click Interoperability. Enable interoperability and generate a key.

At this point, you can user any of the common upload scenarios for S3, while substituting these gcloud interop credentials, and changing the endpoint to storage.googleapis.com.

  • AWS official docs on browser uploads to S3
  • AWS browser JavaScript SDK examples
  • S3 uploading using minimal HTML and JavaScript - TJ Holowaychuk


回答3:

<Details>Cannot create buckets using a POST.</Details>

Can mean :

  • File parameter must be at the end.
  • Content-type must be multipart/form-data
  • only one file allowed
  • key or filename (in file form-field) should be filled