


PHP generates 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]
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
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
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
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
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
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 |
---|---|---|
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 |
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!

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
