Typically, pre-signed URLs grant access to individual objects only. If you want a pre-signed URL that lists all directories in a bucket and enables querying and downloading objects under the directories, pre-sign the URL with the query parameter X-Tos-Policy. This topic describes how to use a pre-signed URL to grant access to a batch of objects within a validity period, without configuring the bucket or objects themselves.
Format of pre-signed URLs in listing scenarios
Parameters
The query parameter X-Tos-Policy value "Policy" is a JSON text encoded in UTF-8 and Base64. It declares the conditions the request using this URL must meet to validate the request's legitimacy.
Below is a sample policy.
Below are the "conditions" items supported in the policy.
"Conditions" match types
warning
When multiple key rules exist, the rules are combined with an OR relationship. If any one rule matches successfully, the request passes the validity check.
Signature calculation
This section describes the difference in signature calculation between using pre-signed URLs with the X-Tos-Policy query parameter and the Authorization header. The only difference lies in the construction of the CanonicalRequest; all other processes are the same.
- CanonicalRequest contains only CanonicalQueryString and HashedPayload.
- CanonicalQueryString contains only X-Tos-Algorithm, X-Tos-Credential, X-Tos-Date, X-Tos-Expires, X-Tos-Policy, and X-Tos-Security-Token.
- When using a temporary AK/SK for authentication, X-Tos-Security-Token must be included.
- Use UNSIGNED-PAYLOAD instead of HashedPayload, as the request content is unknown.
Sample pre-signing process
For the bucket examplebucket, to share all objects with the prefixes abc/ and aaa/abc/, as well as the two specific objects named exampleobject and exampleobject1, the following Policy can be constructed.
Below are the parameters for calculating the signature.
Policy
Below is the Base64- and URL-encoded JSON policy.
Generating the request URL
URL generation method
Concatenate the signature calculated above with CanonicalQueryString to yield the signature query parameters used to generate the URLs for ListObjects, ListObjectVersions, HeadObject, and GetObject methods. Using the signature query parameters, you can generate the respective URLs. The signature query parameters are as follows.
Concatenate the BucketName, EndPoint, Path, signature query parameters, and additional query parameters in the following manner to generate the request URL. For object listing, leave ObjectKey empty. For object querying and acquisition, ObjectKey is the object name. Additional query parameters relate to the method’s specific behavior and are explained in the next section.
warning
During URL generation, there is no strict requirement for the order of query parameters. In this document, the signature query parameters are placed before additional query parameters for illustration only.
Sample URL generation
Note:
- examplebucket is the bucket name in the policy.
- tos-cn-beijing.volces.com is the Endpoint, corresponding to the cn-beijing region used in signature calculation.
Listing objects
If other users want to list all objects shared with the prefix abc based on this signed query parameter, they can use the query parameter prefix=abc to generate the following specific URL, then access the URL via a GET request to list the objects.
Additional query parameters
Additional query parameters control certain specific behaviors of the request. For example, using the query parameter prefix=abc/abc/ lists objects with the prefix abc/abc/. Using the query parameter versions lists multiple versions of objects. For more parameter options, see the ListObjects and ListObjectVersions sections under request parameters and headers. Querying/Getting an object
The URL for querying and getting an object is the same. Sending a HEAD request to the URL queries the information about that object, while sending a GET request gets the object.
The following URL queries/gets version 123 of object "exampleobject" in bucket "examplebucket":
Here, "examplebucket" is the bucket name in the policy, and "exampleobject" is the name of the object to query/get.
Additional query parameters:
Control the specific behavior of this request. For parameter options, see the headObject and getObject sections under request parameters and headers. warning
You can control the behavior of headObject and getObject requests by carrying headers in the Header. If needed, include the corresponding headers in the request URL.