


PHP generates the relevant content of the request signature required by Tencent Cloud COS interface
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]
Request signature feature summary
-
is a key-value pair format of a string
key=value, the key is a fixed value
There are 7 pairs of keys in total =value
sha1 is also a parameter, but as of the official release, only sha1 is supported, so you can directly assign values to
SignedHeaderList, SignedParameterList, and Signature. value needs to be generated through an algorithm
For detailed description of key-value pairs, please refer to 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 lowercase, 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. After operation, content-type;host# is output.
##q-url-param-listPersonal understanding, it consists of HTTP request parameters, take all or part of the request parameters, take out the key part of the request parameter in the form of key=value, and convert it to lowercase. Multiple keys are sorted by dictionary, connected by characters ;, and finally formed into a string For example, the original HTTP request is:GET /?prefix=abc&max-keys=20key 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
Calculate the signature based on the HTTP content. The algorithm is provided by COS. Just give the value as requiredOfficial examples and reference resultsBefore starting to write logic, take a look at the official examples. The reference value, as well as the calculated result, in order to compare the result with the logic developed by yourselfHTTP original request can also be understood as the HTTP request before calculating the signature or when no signature is required:
PUT /testfile2 HTTP/1.1The HTTP request you should get after calculating the signature:Host: bucket1-1254000000.cos.ap-beijing.myqcloud.com
Hello world
x-cos-content-sha1: 7b502c3a1f48c8609ae212cdfb639dee39673f5e
x-cos-storage -class: standard
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=14e6ebd7955b0c6da532151bf97045e2c5a64e10Hello world
Conclusion: If the algorithm can get the one after Authorization The string string is correct
Preparation work
Let’s take a look at the (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: According to the example in the previous section, it is not difficult to get that the HTTP original request has three contents: Host, x-cos-content-sha1 and x-cos-storage-class
- HTTP request parameters: Is it a PUT request, no? Parameters
But where did q-signature come from?
As mentioned just now, q-signature also needs to be calculated by a specific algorithm. The following explains how to calculate it
Calculate the request signature
Look at the code first :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
For testing, this method should have more parameters than needed. The first six parameters have been given and come from the user, so directly Assign a value to get the following string:
$authorization = "q-sign-algorithm=sha1&q-ak=$secretId&q-sign-time=$qSignTime&q-key-time=$qKeyTime...
$header_list This value must comply with the q-header-list
rules and therefore needs to be calculated. The logic is as described above, which is to extract keys from the established request items to form an orderly String, the code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
$url-param-list As mentioned above, this value is an HTTP request parameter. There is no ? parameter for the PUT method, naturally. The value is empty, so the code is "lazy" and directly gives the empty string.
Signature calculation and things to be careful about
The official has given a complete Algorithm, PHP and even written code, I should be very happy (but! I was dizzy after reading the official document, so I will explain it later), first take a look at the "format" of signature:
SignKey = HMAC-SHA1(SecretKey,"[q-key-time]")
HttpString = [HttpMethod]\n[HttpURI]\n[HttpParameters]\n[HttpHeaders]\n
StringToSign = [q-sign-algorithm]\n[q-sign-time]\nSHA1-HASH(HttpString)\n
Signature = HMAC-SHA1(SignKey,StringToSign)
again Take a look at the complete algorithm of Signature:
$signTime = $qSignTime;
$signKey = hash_hmac('sha1', $signTime, $secretKey);
$httpString = "$httpMethod \n$httpUri\n$httpParameters\n$headerString\n";
$sha1edHttpString = sha1($httpString);
$stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";
$signature = hash_hmac('sha1', $stringToSign, $signKey);
$signTime: very simple, a string consisting of start and end time, just use it from above
$ signKey: HMAC-SHA1 algorithm can be calculated directly
$httpString: The four parts need to be said separately
1, $httpMethod: HTTP request method, lowercase, such as put, get
2. $httpUri: The URI part of the HTTP request, starting from the "/" virtual root, such as /testfile means creating a file called testfile in the root directory of the bucket, /image/face1.jpg means creating a file called testfile in the root directory/image directory. Create a file called face1.jpg. As for whether it is an image file or not, it doesn’t matter
3, $httpParameters: This is the first place to be careful. It consists of HTTP original request parameters, that is, the part after ? in the request URI. This example calls the PUT Object interface, so it is empty. If it is not empty, you need to convert the key and value of each item of the request parameter to lowercase. Multiple pairs of key=value are sorted by dictionary and connected with &
4. $headerString: This is the second place to be careful. , consisting of HTTP original request headers. According to the request headers, select all or part of the request headers, convert the keys of each item to lowercase, convert the values to URLEncode, change the format of each item to key=value, and then proceed according to the key. Dictionary sorting, and finally use the connector & to form a string. This is the logic I compiled. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Why should you be careful?
HTTP original request headers and request parameters are used in four places, namely q-header-list in the request signature and HttpHeaders in the Signature - both use the HTTP original request header; request signature q-url-param-list in Signature and HttpParameters in Signature - both use HTTP request parameters. Be sure to ensure that the number of HTTP request headers and request parameters selected is consistent with the object
: the number and members of the HTTP request headers generated by q-header-list must be the same as those used to generate HttpHeaders. The number and members of the HTTP request parameters generated by q-url-param-list must be the same as those generated by HttpParameters.
is different: q-header-list and q-url-param-list only take In the key part, HttpHeaders and HttpParameters take the key and value parts
Output results and verification
At this point, there are 7 values in the request signature Some of them come from user information, and some need to be calculated. All the calculation methods and personal understanding of why they are calculated are also given above. Finally, you only need to output according to the official requirements. take a look
sha1 | Currently only supported 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 | lexicographically sorted list of HTTP header keys | |
HTTP parameter list is empty |
||
14e6ebd7955b0c6da532151bf97045e2c5a64e10 | Calculated by code |
The above is the detailed content of PHP generates the relevant content of the request signature required by Tencent Cloud COS interface. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
