To secure uploads that use the PostObject operation, OSS requires a signature in each upload request. In a POST V1 signature, the signature is calculated by using an AccessKey secret to sign a set of request parameters, such as the upload policy and its expiration time. The application server generates a signature and provides it to the client along with the policy. The client then uses this information to construct the upload request. When OSS receives the request, it verifies the validity of the signature. OSS accepts requests with a valid signature and rejects those with an invalid one.
OSS supports the more secure V4 signature algorithm. We recommend using V4 signature for new integrations.
POST signatures
HTTP POST requests support the V1 signature algorithm. The form and policy play key roles in ensuring the security and compliance of upload requests.
Form fields
A form is a collection of fields in a POST request that transmit the object and its metadata. The following table describes the form fields that are specific to POST V1 signatures. For information about other common form fields, see PostObject form elements.
Field | Type | Description |
OSSAccessKeyId | String | The Default: None
Important |
Signature | String | The HMAC-SHA1 Default: None Important
|
Policy
The policy form field contains a JSON document that defines permissions and constraints for uploading an object to OSS using an HTML form.
A policy must contain the expiration and conditions fields.
{
"expiration": "2023-12-03T13:00:00.000Z",
"conditions": [
{"bucket": "examplebucket"},
["content-length-range", 1, 10],
["eq", "$success_action_status", "201"],
["starts-with", "$key", "user/eric/"],
["in", "$content-type", ["image/jpeg", "image/png"]],
["not-in", "$cache-control", ["no-cache"]]
]
}The following section describes the policy fields:
expiration
Specifies the expiration time of the
policyin the ISO 8601 GMT format. For example, if you set this parameter to2023-12-03T13:00:00.000Z, the POST request must be initiated before 13:00 UTC on December 3, 2023.conditions
Specifies the valid values for form fields in the POST request.
Field
Type
Required
Description
Condition matching modes
bucket
String
No
The
bucketname.bucket
content-length-range
String
No
The minimum and maximum size, in bytes, of the
objectto upload.content-length-range
success_action_status
String
No
The HTTP status code to return after a successful upload.
eq, eq-ci, starts-with, starts-with-ci, in, in-ci, not-in, not-in-ci
key
String
No
The name of the
objectto upload.eq, eq-ci, starts-with, starts-with-ci, in, in-ci, not-in, not-in-ci
content-type
String
No
The MIME type of the uploaded
object.eq, eq-ci, starts-with, starts-with-ci, in, in-ci, not-in, not-in-ci
cache-control
String
No
Specifies the caching behavior of the
object.eq, eq-ci, starts-with, starts-with-ci, in, in-ci, not-in, not-in-ci
Signature calculation process
Create a UTF-8 encoded
policy.Construct the StringToSign.
Base64-encode the
policyto create a transport-safe string. This string serves as theStringToSign.Calculate the signature.
Sign the string with your
AccessKey secretusing the following formula:Signature = base64(hmac-sha1(AccessKeySecret,base64(policy))).
Java example: POST signature calculation
The following Java example demonstrates how to calculate a POST signature based on the policy provided above.
import org.apache.commons.codec.binary.Base64;
public class Demo {
public static void main(String[] args) {
// Before you run this code example, make sure that the OSS_ACCESS_KEY_SECRET environment variable is set.
String accessKeySecret = System.getenv().get("OSS_ACCESS_KEY_SECRET");
// Step 1: Create the policy.
String policy = "{\n" +
" \"expiration\": \"2023-12-03T13:00:00.000Z\",\n" +
" \"conditions\": [\n" +
" {\"bucket\": \"examplebucket\"},\n" +
" [\"content-length-range\", 1, 10],\n" +
" [\"eq\", \"$success_action_status\", \"201\"],\n" +
" [\"starts-with\", \"$key\", \"user/eric/\"],\n" +
" [\"in\", \"$content-type\", [\"image/jpeg\", \"image/png\"]],\n" +
" [\"not-in\", \"$cache-control\", [\"no-cache\"]]\n" +
" ]\n" +
"}";
// Step 2: Construct the StringToSign.
String stringToSign = new String(Base64.encodeBase64(policy.getBytes()));
// Step 3: Calculate the signature.
String signature = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret, stringToSign);
System.out.println("signature:" + signature);
}
}Sample result:
signature:hR2cJnoG9uzrZLDAmrfOtUjtkSM=