This article mainly introduces the request signature required to create a COS interface using PHP. It is compared with the examples given in the official documents to verify the correctness of the algorithm. Friends in need can refer to it
What is COS and request signature
COS is the abbreviation and abbreviation of Tencent Cloud Object Storage. The request signature is created by a specific algorithm and needs to be provided by a third party on demand when calling COS related interfaces. A set of string information that will uniquely identify the current third-party identity and provide identification of both communicating parties. Only valid signed COS will provide services
Goal
Use PHP to create the request signature required for the COS interface, compare it with the example given in the official document, and verify the correctness of the algorithm
Understand the request signature
Come first Look at the request signature given in an official document
q-sign-algorithm=sha1&q-ak=[SecretID]&q-sign-time=[SignTime]&q-key-time=[KeyTime]&q-header-list=[SignedHeaderList]&q-url-param-list=[SignedParameterList]&q-signature=[Signature]
Summary of the characteristics of the request signature
is a string of characters
key=value key-value pair format, key is a fixed value
There are 7 pairs of key=value
sha1 is also a parameter, but As of the official release, only sha1 is supported, so you can directly assign values
SignedHeaderList, SignedParameterList, and Signature three values need to be generated through algorithms
key values For a detailed description, see the official documentation.
Requesting a signature requires a total of 7 values. Let’s explain one by one below and break each one
Signature algorithm, official Currently only sha1 is supported, so just give the value directly
The account ID, which is the user's SecretId, can be obtained on the console Cloud API Key page
The valid start and end time of the current signature, Unix timestamp format, English half-width semicolon; separated, format such as 1480932292;1481012298
Same as q-sign-time value
Personal understanding, it consists of HTTP request headers, take all or part of the request headers, and change the request in the form of key:value The key part of the item is taken out, converted to lower case, multiple keys are sorted according to the dictionary, and connected with the characters ; to finally form a string
For example, the original request header has two:
Host:bucket1-1254000000.cos.ap-beijing.myqcloud.com Content-Type:image/jpeg
key is Host and Content-Type, output after calculation content-type;host
Personal understanding, it consists of HTTP request parameters, take all or part of the request parameters, and key The key part of the request parameter in the form of =value is taken out, converted to lowercase, multiple keys are sorted according to the dictionary, connected with the characters ;, and finally formed into a string
For example, the original HTTP request is:
GET /?prefix=abc&max-keys=20
key It is prefix and max-keys. After operation, max-keys; prefix is output. If the request has no parameters such as put and post, it will be empty.
Calculated based on HTTP content Signature, algorithm is provided by COS, just give the value as required
Before starting to write the logic, first take a look at the reference value given by the official example, and the calculated The final result can be compared with the logic developed by yourself
The original HTTP request can also be understood as the HTTP request before calculating the signature or when no signature is required:
PUT /testfile2 HTTP/1.1 Host: bucket1-1254000000.cos.ap-beijing.myqcloud.com x-cos-content-sha1: 7b502c3a1f48c8609ae212cdfb639dee39673f5e x-cos-storage-class: standard Hello world
After calculating the signature The HTTP request that should be obtained:
PUT /testfile2 HTTP/1.1 Host: bucket1-1254000000.cos.ap-beijing.myqcloud.com x-cos-content-sha1: 7b502c3a1f48c8609ae212cdfb639dee39673f5e x-cos-storage-class: standard Authorization: q-sign-algorithm=sha1&q-ak=AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q&> q-sign-time=1417773892;1417853898&q-key-time=1417773892;1417853898&q-header-list=host;x-cos-content-sha1;x-cos-storage-class&q-url-param-list=&q-signature=14e6ebd7955b0c6da532151bf97045e2c5a64e10 Hello world
Conclusion: The algorithm is correct if it can get the string after Authorization
Let’s take a look (officially provided ) user information and HTTP information:
SecretId: AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q
SecretKey: BQYIM75p8x0iWVFSIgqEKwFprpRSVHlz
Signature valid start time: 1417773892
Signature valid stop time: 1417853898
HTTP original request header: It is not difficult to obtain according to the example in the previous section The HTTP original request has three contents: Host, x-cos-content-sha1 and x-cos-storage-class
HTTP request parameters: It is a PUT request, there is no ? parameter
Put all the parameters in the preparation work into the request signature rules, and you can get the results easily, as shown in the following table:
Key(key) | Value(value) | Remarks |
---|---|---|
sha1 | Currently only supports sha1 signature algorithm | |
AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q | SecretId field | |
1417773892;1417853898 | 2014/12/5 18:04:52 to 2014/12/6 16:18: 18 | |
1417773892;1417853898 | 2014/12/5 18:04:52 to 2014/12/6 16 :18:18 | |
host;x-cos-content-sha1;x-cos-storage-class | A lexicographically sorted list of HTTP header keys | |
HTTP parameter list is empty | ||
14e6ebd7955b0c6da532151bf97045e2c5a64e10 | Calculated by code |