Uploads an object to a bucket by using an HTML form.

Usage notes

  • To upload an object to a bucket by using an HTML form, you must have the oss:PutObject permission. For more information, see Attach a custom policy to a RAM user.
  • The size of the object that is uploaded by calling the PostObject operation cannot exceed 5 GB.
  • To initiate a PostObject request to a bucket, you must have write permissions on the bucket. If the access control list (ACL) of the bucket to which you want to initiate a PostObject request is public-read-write, you do not need to sign the PostObject request. Otherwise, Object Storage Service (OSS) verifies the signature information contained in the request.
  • Unlike the PutObject operation, the PostObject operation uses an AccessKey secret to calculate the signature for the policy form field. The calculated signature string is used as the value of the Signature form field. OSS checks this value to verify the validity of the signature.
  • The URL of the submitted form is the domain name of the bucket. You do not need to specify the object that you want to upload in the URL. The request line is in the format of POST / HTTP/1.1 instead of POST /ObjectName HTTP/1.1.
  • OSS does not check the signature information that is contained in headers or URLs in PostObject requests.

Versioning

If you initiate a PostObject request to a versioning-enabled bucket, OSS generates a unique version ID for the uploaded object and returns the version ID as the x-oss-version-id header in the response.

If you initiate a PostObject request to a versioning-suspended bucket, OSS generates a version ID of null for the uploaded object and returns the version ID as the x-oss-version-id header in the response. An object can have only one version whose version ID is null.

Request syntax

POST / HTTP/1.1 
Host: BucketName.oss-cn-hangzhou.aliyuncs.com
User-Agent: browser_data
Content-Length: ContentLength
Content-Type: multipart/form-data; boundary=9431149156168
--9431149156168
Content-Disposition: form-data; name="key"
key
--9431149156168
Content-Disposition: form-data; name="success_action_redirect"
success_redirect
--9431149156168
Content-Disposition: form-data; name="Content-Disposition"
attachment;filename=oss_download.jpg
--9431149156168
Content-Disposition: form-data; name="x-oss-meta-uuid"
myuuid
--9431149156168
Content-Disposition: form-data; name="x-oss-meta-tag"
mytag
--9431149156168
Content-Disposition: form-data; name="OSSAccessKeyId"
access-key-id
--9431149156168
Content-Disposition: form-data; name="policy"
encoded_policy
--9431149156168
Content-Disposition: form-data; name="Signature"
signature
--9431149156168
Content-Disposition: form-data; name="file"; filename="MyFilename.jpg"
Content-Type: image/jpeg
file_content
--9431149156168
Content-Disposition: form-data; name="submit"
Upload to OSS
--9431149156168--

Request headers

Important
  • The message body of a PostObject request is encoded in the multipart/form-data format. In the PostObject operation, the parameters are passed as form fields in the request message body, whereas the parameters are passed as HTTP request headers in the PutObject operation.
  • You cannot add tags to objects by passing x-oss-tagging request headers during the PostObject operation. You can call the PutObjectTagging operation to add tags to objects after the PostObject operation is complete.
ParameterTypeRequiredDescription
Content-TypeString NoThe type of the object that you want to upload and the encoding format of the web page. Browsers determine how to read and encode the object based on the value of the parameter.

The form submitted in the PostObject operation must be encoded in the multipart/form-data format. The Content-Type header must be in the multipart/form-data;boundary=xxxxxx format.

In this format, boundary is a boundary string that is randomly generated by the form. You do not need to specify the boundary string. If you use an OSS SDK to create a form, the SDK also generates a random boundary string.

x-oss-object-aclString NoThe ACL of the object that you want to upload.

If you specify x-oss-object-acl in the request header and the form field, the ACL that is specified in the form field takes precedence over the ACL that is specified in the request header.

Valid values:
  • default: The ACL of the object is the same as the ACL of the bucket in which the object is stored. This is the default value.
  • private: The ACL of the object is private. Only the owner of the object and authorized users have read and write permissions on the object.
  • public-read: The ACL of the object is public-read. Only the owner of the object and authorized users have read and write permissions on the object. Other users have only read permissions on the object. Exercise caution when you set the object ACL to this value.
  • public-read-write: The ACL of the object is public-read-write. All users have read and write permissions on the object. Exercise caution when you set the object ACL to this value.

For more information about object ACLs, see Object ACL.

For more information about the common request headers contained in PostObject requests such as Host and Date, see Common request headers.

Request elements

Important
  • The last form field must be file. No particular order is required for other form fields.
  • The key of a form field cannot exceed 8 KB, and the value of a form field cannot exceed 2 MB.
ElementTypeRequiredDescription
Cache-ControlStringNoThe caching behavior of the web page when the object is downloaded. For more information, see RFC 2616.

This element is empty by default.

Content-DispositionStringNoThe name of the object when the object is downloaded. For more information, see RFC 2616.

This element is empty by default.

Content-EncodingStringNoThe content encoding format of the object when the object is downloaded. For more information, see RFC 2616.

This element is empty by default.

ExpiresString NoThe time when the request expires. For more information, see RFC 2616.

This element is empty by default.

OSSAccessKeyIdString ConditionalThe AccessKey ID of the account that owns the bucket.

This element is empty by default.

Constraint: This form field is required if the bucket ACL is not public-read-write or the policy or Signature form field is specified in the request.

policyString ConditionalThe validity of the form fields in the request. A request that does not contain the policy form field is considered an anonymous request and can be used only to access buckets whose ACLs are public-read-write.

This element is empty by default.

Constraint: This form field is required if the bucket ACL is not public-read-write or the OSSAccessKeyId or Signature form field is specified in the request.

Important The form and the policy form field must be UTF-8-encoded. The policy form field must also be Base64-encoded.
SignatureString ConditionalThe signature information that is calculated based on the AccessKey secret and the policy form field. OSS checks the signature information to verify the validity of the PostObject request. For more information, see Appendix: Signature.

This element is empty by default.

Constraint: This form field is required if the bucket ACL is not public-read-write or the OSSAccessKeyId or policy form field is specified in the request.

Note Form fields are case-insensitive, but their values are case-sensitive.
x-oss-server-side-encryption-key-idStringNoThe ID of the customer master key (CMK) that is managed by Key Management Service (KMS). You can configure this element only when x-oss-server-side-encryption is set to KMS.
x-oss-content-typeStringNoYou can add the x-oss-content-type form field to the message body of a PostObject request to specify the content type of the object that you want to upload. The content type specified by the x-oss-content-type form field takes priority over the content type specified by the file form field that is automatically generated by browsers.

This element is empty by default.

x-oss-forbid-overwriteStringNoSpecifies whether the PostObject operation overwrites the existing object that has the same name.

When versioning is enabled or suspended for the bucket to which you want to upload the object, the x-oss-forbid-overwrite header does not take effect. In this case, the object that is uploaded by calling the PostObject operation overwrites the existing object that has the same name.

  • If x-oss-forbid-overwrite is not specified or x-oss-forbid-overwrite is set to false, the object uploaded by calling the PostObject operation overwrites the existing object that has the same name.
  • If x-oss-forbid-overwrite is set to true, the object uploaded by calling the PostObject operation does not overwrite the existing object that has the same name.

If you specify the x-oss-forbid-overwrite request header, the queries per second (QPS) performance of OSS may be degraded. If you want to use the x-oss-forbid-overwrite request header to perform a large number of operations (QPS greater than 1,000), contact technical support.

x-oss-object-aclStringNoThe ACL of the object that you want to upload. You can specify the ACL of the object that you want to upload in the request header and the form field.

If you specify x-oss-object-acl in the request header and the form field, the ACL that is specified in the form field takes precedence over the ACL that is specified in the request header.

Valid values:
  • default: The ACL of the object is the same as the ACL of the bucket in which the object is stored. This is the default value.
  • private: The ACL of the object is private. Only the owner of the object and authorized users have read and write permissions on the object.
  • public-read: The ACL of the object is public-read. Only the owner of the object and authorized users have read and write permissions on the object. Other users have only read permissions on the object. Exercise caution when you set the object ACL to this value.
  • public-read-write: The ACL of the object is public-read-write. All users have read and write permissions on the object. Exercise caution when you set the object ACL to this value.

For more information about object ACLs, see Object ACL.

x-oss-storage-classStringNoThe storage class of the object.

If you specify this form field when you upload an object, the storage class of the uploaded object is the specified value regardless of the storage class of the bucket to which the object is uploaded. For example, if you set x-oss-storage-class to Standard when you upload an object to an IA bucket, the object is stored as a Standard object.

Valid values:
  • Standard
  • IA
  • Archive
  • ColdArchive
keyString YesThe name of the object that you want to upload. You do not need to encode the object name. If the object name contains a path such as destfolder/example.jpg, OSS automatically creates the corresponding directory.

This element is empty by default.

success_action_redirectString NoThe URL to which the client is redirected after the object is uploaded. If this form field is not specified, the returned result is specified by success_action_status. If the upload fails, OSS returns an error code, and the client is not redirected to a URL.

This element is empty by default.

success_action_statusString NoThe HTTP status code that is returned to the client when the success_action_redirect form field is not specified and the object is uploaded.

Valid values: 200, 201, and 204. Default value: 204.

  • If this form field is set to 200 or 204, OSS returns an empty file and HTTP status code 200 or 204.
  • If this form field is set to 201, OSS returns an XML file and HTTP status code 201.
  • If this form field is not specified or set to an invalid value, OSS returns an empty file and HTTP status code 204.
x-oss-meta-*StringNoThe user metadata that you specify for the object.

This element is empty by default.

If the request contains a form field prefixed with x-oss-meta-, the form field is considered the user metadata of the object. Example: x-oss-meta-location.
Note An object can have multiple form fields whose names contain the x-oss-meta- prefix. However, the total size of all user metadata of the object cannot exceed 8 KB.
x-oss-security-tokenString NoThe security token for temporary access authorization. If you obtain temporary access credentials from Security Token Service (STS) and use the temporary access credentials for the request, you must set this form field to the value of the security token and set OSSAccessKeyId to the value of the AccessKey ID in the temporary access credentials. You can calculate the signature for a request that uses a temporary AccessKey ID the same way you calculate the signature for a request that uses an AccessKey ID.

This element is empty by default.

For more information about how to use STS to authorize a user to access OSS, see Use temporary credentials provided by STS to access OSS. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain temporary access credentials. For more information, see STS SDK overview.

fileStringYesThe file or text content. You do not need to encode the content. Browsers automatically add the Content-Type form field based on the file type and overwrite the content type specified in the Content-Type header. You can upload only one object at a time by using a PostObject request.

This element is empty by default.

Important The last form field must be file.

Response headers

HeaderTypeDescription
x-oss-server-side-encryptionStringIf x-oss-server-side-encryption is specified in the request, the response contains this header to indicate the algorithm that is used to encrypt the object on the server side.

For more information about the common response headers contained in the response to a PostObject request, see Common response headers.

Response elements

ElementTypeDescription
PostResponseContainer The container that stores the result of the PostObject request.

Child nodes: Bucket, ETag, Key, and Location

BucketString The name of the bucket.

Parent node: PostResponse

ETagString The entity tag (ETag) that is created when the object is uploaded. If an object is created by calling the PostObject operation, the ETag value is not the MD5 hash of the object content but a unique value calculated based on a rule. The ETag value of an object can be used to check whether the object content is modified.

Parent node: PostResponse

LocationString The URL that is used to access the uploaded object.

Parent node: PostResponse

Examples

  • Sample requests
    POST / HTTP/1.1
    Host: oss-example.oss-cn-hangzhou.aliyuncs.com
    Content-Length: 344606
    Content-Type: multipart/form-data; boundary=9431149156168
    --9431149156168
    Content-Disposition: form-data; name="key"
    /user/a/objectName.txt
    --9431149156168
    Content-Disposition: form-data; name="success_action_status"
    200
    --9431149156168
    Content-Disposition: form-data; name="Content-Disposition"
    content_disposition
    --9431149156168
    Content-Disposition: form-data; name="x-oss-meta-uuid"
    uuid
    --9431149156168
    Content-Disposition: form-data; name="x-oss-meta-tag"
    metadata
    --9431149156168
    Content-Disposition: form-data; name="OSSAccessKeyId"
    44CF9590006BF252****
    --9431149156168
    Content-Disposition: form-data; name="policy"
    eyJleHBpcmF0aW9uIjoiMjAxMy0xMi0wMVQxMjowMDowMFoiLCJjb25kaXRpb25zIjpbWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzYwXSx7ImJ1Y2tldCI6ImFoYWhhIn0sIHsiQSI6ICJhIn0seyJrZXkiOiAiQUJDIn1dfQ==
    --9431149156168
    Content-Disposition: form-data; name="Signature"
    kZoYNv66bsmc10+dcGKw5x2P****
    --9431149156168
    Content-Disposition: form-data; name="file"; filename="MyFilename.txt"
    Content-Type: text/plain
    abcdefg
    --9431149156168
    Content-Disposition: form-data; name="submit"
    Upload to OSS
    --9431149156168--
  • Sample responses
    HTTP/1.1 200 OK
    x-oss-request-id: 61d2042d-1b68-6708-5906-33d81921362e 
    Date: Fri, 24 Feb 2014 06:03:28 GMT
    ETag: "5B3C1A2E053D763E1B002CC607C5****"
    Connection: keep-alive
    Content-Length: 0  
    Server: AliyunOSS

Error codes

Error codeHTTP status codeDescription
FieldItemTooLong400The error message returned because the size of the form field key exceeds 8 KB or the size of the form field value exceeds 2 MB.
InvalidArgument400The error message returned because one of the OSSAccessKeyId, policy, and Signature form fields is specified but the other two form fields are not specified in the request regardless of whether the ACL of the bucket is public-read-write.
InvalidDigest400The error message returned because the Content-MD5 value that is calculated by OSS based on the request body is different from the Content-MD5 value that is specified in the request header.
EntityTooLarge400The error message returned because the total size of the PostObject request body exceeds 5 GB.
InvalidEncryptionAlgorithmError400The error message returned because the x-oss-server-side-encryption request header is set to a valueother than AES256 or KMS. The x-oss-server-side-encryption request header can be set only to AES256 or KMS.
IncorrectNumberOfFilesInPOSTRequest400The error message returned because the number of objects in the PostObject request is invalid. A PostObject request can contain only one file form field.
FileAlreadyExists409The error message returned because the request contains the x-oss-forbid-overwrite=true header but an existing object that has the same name already exists.
KmsServiceNotEnabled403The error message returned because x-oss-server-side-encryption is set to KMS but KMS is not activated in advance.
FileImmutable409The error message returned because the data that you want to delete or modify is protected by a retention policy.

Appendix: Policy

The policy form field in a PostObject request is used to verify the validity of the request. The value of the policy form field is a JSON string that is encoded in UTF-8 and Base64. This value declares the conditions that a PostObject request must meet. The policy form field is optional when you upload an object to a bucket whose ACL is public-read-write. However, we recommend that you specify this form field to limit PostObject requests.

The following example shows the format of the policy form field:

{
  "expiration": "2014-12-01T12:00:00.000Z",
  "conditions": [
    {"bucket": "johnsmith" },    
    ["content-length-range", 1, 10],
    ["eq", "$success_action_status", "201"],
    ["starts-with", "$key", "user/eric/"],
    ["in", "$content-type", ["image/jpg", "image/png"]],
    ["not-in", "$cache-control", ["no-cache"]]
  ]
}

The policy form field must contain the expiration and conditions parameters.

  • Expiration

    The expiration parameter specifies the expiration time of the request. The time must follow the ISO 8601 standard and must be in GMT. For example, 2014-12-01T12:00:00.000Z indicates that the PostObject request must be sent before 12:00 on December 1, 2014.

  • Conditions

    The conditions parameter is a list that specifies the valid values of the form fields in the PostObject request.

    The following table describes the conditions that can be configured in the policy field.

    ParameterDescriptionCondition matching mode
    content-length-rangeThe minimum and maximum sizes of the object that you want to upload. Unit: bytes. content-length-range
    Cache-Control, Content-Type, Content-Disposition, Content-Encoding, ExpiresHTTP request headers.
    Important To prevent malicious modifications to the Content-Type header during form upload, we recommend that you include the Content-Type parameter in the policy form field. You can use one of the following methods to configure the Content-Type parameter:
    • Add x-oss-content-type to the message body of a PostObject request to specify Content-Type. Example: Content-Type: image/jpeg. For more information about the Content-Type values that are supported by OSS, see How do I specify the Content-Type header?.
    • Use the file form field to specify Content-Type. Example: Content-Type: image/png. For more information about the Content-Type values that are supported by OSS, see How do I specify the Content-Type header?.

    The content type specified by the x-oss-content-type form field takes precedence over the content type specified by the file form field.

    eq, starts-with, in, and not-in
    keyThe name of the object. eq, starts-with, in, and not-in
    success_action_redirectThe URL to which the client is redirected after the object is uploaded. eq, starts-with, in, and not-in
    success_action_statusThe HTTP status code to return after the object is uploaded if success_action_redirect is not specified. eq, starts-with, in, and not-in
    x-oss-meta-*The user metadata of the object that you want to upload.
    Note The policy field specifies form fields that must be contained in the PostObject request. If you specified ["eq", "$x-oss-meta-biedb", "biedb-test001"], you must add the key form field x-oss-meta-biedb whose value is biedb-test001 to the form field. Otherwise, an error Invalid according to Policy: Policy Condition failed: ["eq", "$x-oss-meta-biedb", "biedb-test001"] is reported.
    eq, starts-with, in, and not-in

    If the PostObject request contains additional form fields, OSS adds these form fields to the conditions of the policy and checks their validity.

  • Condition matching mode
    Condition matching modeDescription
    content-length-rangeThe size of the object that you want to upload must be within the range of allowed object sizes. For example, if the acceptable object size is 1 to 10 bytes, the condition must be ["content-length-range", 1, 10].
    eqThe value of a form field must be exactly the same as the value that is declared in the conditions. For example, if the value of the key form field must be a in the format of {"key":"a"}, the condition must be ["eq", "$key", "a"].
    starts-withThe value of a form field must start with the specified prefix. For example, if the value of the key form field starts with user/user1, the condition must be ["starts-with", "$key", "user/user1"].
    inThe elements that you want to include in verification must be specified in the in condition string. For example, if you want to verify whether JPG and PNG images can be uploaded in a PostObject request, the condition must be ["in", "$content-type", ["image/jpg", "image/png"]].
    not-inThe elements that you want to exclude from the PostObject request must be specified in the not-in condition string. For example, if you want to specify an object caching behavior and exclude no-cache, the condition must be ["not-in", "$cache-control", ["no-cache"]].

In the policy form field of a PostObject request, the dollar sign ($) indicates a variable. To describe the dollar sign ($), you must use the following escape character: \$. The following table describes the escape characters used in the JSON string of the policy form field in a PostObject request.

Escape characterDescription
\/Forward slash
\\Backslash
\"Double quotation marks
\$Dollar sign
\bSpace
\fForm feed
\nNew line
\rCarriage return
\tHorizontal tab
\uxxxxUnicode character

Appendix: Signature

To verify a PostObject request, you must include the policy and Signature form fields in the HTML form. The policy form field specifies the values that are acceptable in the request.

You can perform the following steps to calculate the value of the Signature form field:

  1. Create a policy encoded in UTF-8.
  2. Encode the policy in Base64. The result is the value of the policy form field, and this value is used as the string to sign.
  3. Use the AccessKey secret to sign the string in the following format: Signature = base64(hmac-sha1(base64(policy), AccessKeySecret)).

FAQ

What do I do if the Your proposed upload exceeds the maximum allowed size. error message is returned?

  • Cause: The size of the uploaded object is out of the size range specified for the content-length-range parameter.
  • Solution: The content-length-range parameter is used to specify the minimum and maximum sizes of the uploaded object. Unit: bytes. For example, if the size of the object that you want to upload is 5 GB, you can set the content-length-range parameter to ["content-length-range", 1, 1073741824].

References