PHP generates the request signature required by Tencent Cloud COS interface
不言
Release: 2023-03-29 06:08:01
Original
1789 people have browsed it
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
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.
Breakdown one by one
Requesting a signature requires a total of 7 values. Let’s explain one by one below and break each one
q-sign-algorithm
Signature algorithm, official Currently only sha1 is supported, so just give the value directly
q-ak
The account ID, which is the user's SecretId, can be obtained on the console Cloud API Key page
q-sign-time
The valid start and end time of the current signature, Unix timestamp format, English half-width semicolon; separated, format such as 1480932292;1481012298
q-key-time
Same as q-sign-time value
q-header-list
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
key is Host and Content-Type, output after calculation content-type;host
q-url-param-list
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
Copy after login
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.
q-signature
Calculated based on HTTP content Signature, algorithm is provided by COS, just give the value as required
Official example and reference result
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
Copy after login
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
Copy after login
Conclusion: The algorithm is correct if it can get the string after Authorization
Preparation work
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
Calculate signature
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
##q-sign-algorithm
sha1
Currently only supports sha1 signature algorithm
q-ak
AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q
SecretId field
q-sign-time
1417773892;1417853898
2014/12/5 18:04:52 to 2014/12/6 16:18: 18
##q-key-time
1417773892;1417853898
2014/12/5 18:04:52 to 2014/12/6 16 :18:18
q-header-list
host;x-cos-content-sha1;x-cos-storage-class
A lexicographically sorted list of HTTP header keys
The above is the detailed content of PHP generates the request signature required by Tencent Cloud COS interface. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn