Uploading Objects Through Forms

Last Updated At: 2025-10-16 14:37:56

Description

The POST Object API is used to upload files (objects) to a specified bucket by using a form. This operation requires the requester to have the write permission on the bucket. All API parameters carried by HTTP headers are requested using form fields.

Notes

  1. Write permissions for the bucket are required.
  2. If the Object being added already exists with the same name, the newly uploaded file will overwrite the original file, and a successful response will return 200 OK.

Request

Request Example

POST / HTTP/1.1
Host: <BucketName-APPID>.<Endpoint>
Content-Length: length
Headers
Form

Request Header

Common Headers

The implementation of this request operation uses common request headers. For details on common request headers, please refer to the Common Request Headers document.

Non-common Headers

The following table describes required request headers for this request operation.

Name Description Type Required
Content-Length Length (bytes) of HTTP request content as defined in RFC 2616. String Yes

Form Field

Name Description Type Required
acl Defines the ACL attribute of an object.
Valid values: private, public-read, and default.
Default value: default (inheriting bucket permissions).
Note: Up to 1000 access policy entries are supported. If you do not need to set an ACL for the object, set this parameter to default or leave it empty. In this way, the object will inherit the permissions of the bucket it is stored in.
String No
Cache-Control, Content-Type, Content-Disposition, Content-Encoding, Expires Header defined in RFC 2616. For details, see . String No
file File content, which is used as the last field of the form. String Yes
key Name of the file after upload. If ${filename} is used, the file name is replaced, for example, a/b/${filename}. If the photo.jpg file is uploaded, the final upload path is a/b/photo.jpg. String Yes
x-cos-meta-* Includes user-defined header suffix and user-defined header information, which will be returned as Object metadata, with a size limit of 2 KB.
Note: User-defined header information supports underscores. However, the user-defined header suffix does not support underscores.
String No
policy Base64 encoding, which is used for request check. If the request content does not match the conditions specified by Policy, 403 AccessDenied is returned. String No

Signature Protection

When an HTTP POST request for form upload is initiated and signature protection is required, the form should include form-data in the following content structure:

Form Field Description
policy Policy content encrypted using Base64, which will be used to check the request content. If the request content does not meet the specified conditions of the policy, the request will be rejected.
q-sign-algorithm Algorithm used for calculating the signature. COS supports SHA1, and sha1 in lowercase is entered here.
q-ak ID of the account key, that is, SecretId.
q-key-time Effective start and end time of the key used for request signature, described by Unix timestamps, in seconds, in [start-seconds];[end-seconds] format, for example, 1480932292;1481012298.
q-signature Request signature calculated by using the preceding elements. COS verifies the form elements and signature content. If the signature does not match the signed content, the request is rejected.

Signature Calculation

The calculation of the signature q-signature is classified into three steps:

  1. Use the key content to encrypt the value of q-key-time to obtain SignKey.
  2. Create a POST request policy and encrypt the content using SHA1 to obtain StringToSign.
  3. Use SignKey to encrypt StringToSign to generate the signature.

Policy

The following example is a complete policy.

{ "expiration": "2007-12-01T12:00:00.000Z",
  "conditions": [
    {"acl": "public-read" },
    {"bucket": "examplebucket-1250000000" },
    ["starts-with", "$key", "user/eric/"],
    {"q-sign-algorithm": "sha1" },
    {"q-ak": "AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
    {"q-sign-time": "1480932292;1481012298" }
  ]
}

Expiration
Sets the timeout time of POST Policy, in ISO8601 GMT time, for example, 2017-12-01T12:00:00.000Z.

Conditions rules

Type Description
Exact match Expressed using {"key": "value"} or ["eq", "$key", "value"]
Prefix match Expressed using ["starts-with", "$key", "value"], where the value can be left blank
Range match Only used for ["content-length-range", int1, int2], and the file byte count should be within the range of int1 to int2.

Conditions parameters
All parameters are optional. If a parameter is not specified, it does not need to be verified.

Name Description Matching Method
acl ACL attribute of the object, which is optional. Exact match or prefix match
bucket Specified bucket to which data is uploaded. Exact match
content-length-range Specified size range of the uploaded file. Scope
key Storage path of an object. Exact match or prefix match
x-cos-meta-* Includes the user-defined header suffix and user-defined header information, which are returned as object metadata with a size limit of 2 KB.
Note: User-defined header information supports underscores, but user-defined header suffix does not support underscores.
Exact match or prefix match
x-cos-* Other COS headers that need to be signed. Exact match

Response

Response Headers

Common Response Headers

This response uses common response headers. For details on common response headers, see Public Response Headers.

Response Parameters

Name Description Type
ETag Returns the MD5 checksum value of the file. The value of ETag can be used to check whether the object was corrupted during the upload process. String

Response Body

The response body is returned as application/xml data, and the content containing the complete node data is shown below.

<PostResponse>
        <Location>http://examplebucket-1250000000.<Endpoint>/photo.jpg</Location>
        <Bucket>examplebucket-1250000000</Bucket>
        <Key>photo.jpg</Key>
        <ETag>d41d8cd98f00b204e9800998ecf8427e</ETag>
</PostResponse>

The data are described as follows:

Node Name (Keyword) Parent Node Description Type Required
PostResponse No Container for storing the results of the POST Object operation. Container Yes

Content of the PostResponse node of the container type:

Node Name (Keyword) Parent Node Description Type Required
Location PostResponse Full path of the object. String Yes
Bucket PostResponse Bucket where the object is located. String Yes
Key PostResponse Key name of the object. String Yes
ETag PostResponse Content of ETag. String Yes

Error Code

The following table describes some special and common errors that may occur with this request. For details on error codes related to COS, see Error Codes.

Error Code HTTP Status Code Description
InvalidDigest 400 Bad Request If the user uploads a file with the Content-MD5 header, COS checks whether the Md5 in the body is consistent with the user-provided MD5. If not, COS returns an InvalidDigest error.
KeyTooLong 400 Bad Request Custom header started with x-cos-meta carried while a file is uploaded. The key and value of each user-defined header cannot exceed 4 KB. Otherwise, a KeyTooLong error is returned.
MissingContentLength 411 Length Required If the Content-Length header is not added when a file is uploaded, this error code is returned.
NoSuchBucket 404 Not Found If the bucket containing the object you are trying to add does not exist, a 404 Not Found error is returned, and the error code is NoSuchBucket.
EntityTooLarge 400 Bad Request If the size of the file to be added exceeds 5 GB, an EntityTooLarge error is returned, along with the message "Your proposed upload exceeds the maximum allowed object size".
InvalidURI 400 Bad Request The object key length is limited to 850 characters. If it exceeds 850 characters, an 'InvalidURI' error is returned.

Practical Case

Request

POST / HTTP/1.1
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.12.4
Host: <BucketName-APPID>.<Endpoint>
Content-Length: 1352
Content-Type: multipart/form-data; boundary=e07f2a7876ae4755ae18d300807ad879

--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="key"

a/${filename}
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="Acl"

public-read
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="Signature"

q-sign-algorithm=sha1&q-ak=AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&q-sign-time=1512983814;1512984814&q-key-time=1512983814;1512984814&q-url-param-list=&q-header-list=host&q-signature=2ffd2ae714e7445a8da000ec5d51771ff5056500
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="policy"

eyJjb25kaXRpb25zIjogW3siYnVja2V0IjogImtpdG1hbnMzdGVzdDEifSwgWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwMDAwMDAwXSwgWyJzdGFydHMtd2l0aCIsICJ4LWNvcy1tZXRhLWJiIiwgIjEyIl1dLCAiZXhwaXJhdGlvbiI6ICIyMDQ3LTEyLTAxVDEyOjAwOjAwLjAwMFoifQ==
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="x-Cos-meta-bb"

124
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="key1"

1
--e07f2a7876ae4755ae18d300807ad879
Content-Disposition: form-data; name="file"; filename="empty:a"

--e07f2a7876ae4755ae18d300807ad879--

Response

HTTP/1.1 204
Content-Type: application/xml
Content-Length: 232
Connection: keep-alive
Date: Mon, 11 Dec 2017 09:16:56 GMT
ETag: "d41d8cd98f00b204e9800998ecf8427e"
Location: http://examplebucket-1250000000.cos.ap-[Region].myqcloud.com/photo.jpg
x-cos-request-id: NWEyZTRkMDZfMjQ4OGY3MGFfNTE4Yl81