
This thing is really handy!
After enabling the Private Space feature in Qiniu Cloud Object Storage and subsequently activating the Timestamp-based Anti-Link Hijacking feature, how can I obtain a download link?
When timestamp authentication is enabled for a private space in Qiniu Cloud Object Storage, a timestamp parameter must be included when generating a temporary download link. The specific steps are as follows:
- Get a private space download domain name
In the Qiniu Cloud backend, select the appropriate storage space, then navigate to the "Domain Name Settings" page and locate the download domain name for your private space (e.g.:http(s)://<domain>), note down this address for future reference.
- Generate download link
Use the `Auth` class from the Qiniu Cloud PHP SDK to generate a temporary download link with a timestamp, based on parameters such as the expiration time of the download link, the file name, and the timestamp. The example code is as follows:
<?php
require 'vendor/autoload.php';
use Qiniu\Auth;
use \Qiniu\Cdn\CdnManager;
$accessKey = '';
$secretKey = '';
// 构建Auth对象
$auth = new Auth($accessKey, $secretKey);
//创建时间戳防盗链
//时间戳防盗链密钥,后台获取
$encryptKey = '';
//带访问协议的域名
$url2 = 'http://***.4s5.cn/test/370009-2.png';
//有效期时间(单位秒)
$signedUrl1 = $auth->privateDownloadUrl($url2);
$durationInSeconds = 60;
$signedUrl = CdnManager::createTimestampAntiLeechUrl($signedUrl1, $encryptKey, $durationInSeconds);
print($signedUrl);This code implements a timestamp-based anti-URL forgery feature using the Qiniu Cloud PHP SDK. Below is an analysis of the code:
First, proceed.
require 'vendor/autoload.php';Import the required dependency files.Use namespace
use Qiniu\Auth;和use \Qiniu\Cdn\CdnManager;Import the `Auth` class and the `CdnManager` class.Defines an Access Key and a Secret Key; these should be replaced with actual values.
Create an Auth object and pass in the Access Key and Secret Key for authentication.
Defines the key for timestamp-based link防盗acking.
encryptKeyThe value needs to be retrieved from the Qiniu Cloud backend and replaced with the actual value.Defines a domain name with an access protocol.
url2This value needs to be replaced with the actual value. This domain name serves as the access address for the private space files.By invoking
$auth->privateDownloadUrl($url2)Method: Generate a download link for private space filessignedUrl1This link has not been protected against URL劫持 using a timestamp.Defined expiration time
durationInSecondsThe unit is seconds, indicating the validity period of the timestamp-based防盗 linking link.Call
CdnManager::createTimestampAntiLeechUrl($signedUrl1, $encryptKey, $durationInSeconds)Method: Provide a download link for private space files.signedUrl1Timestamp Anti-Link Hijacking KeyencryptKeyValid until datedurationInSecondsPass this parameter as input to generate a download link with a timestamp-based anti-link-hijacking feature.signedUrl。Use
print($signedUrl);A timestamped download link generated for printed output.
Note that the Access Key, Secret Key, encryptKey, and url2 in this code snippet must be replaced with actual values. If any dependency libraries are missing, please ensure that the required dependencies are installed and that the correct files have been imported.