Hongmu Notes
Home Summary of pitfalls After enabling the Private Space feature in Qiniu Cloud Object Storage and subsequently activating the Timestamp-based Anti-Link Hijacking feature, how can you retrieve a download link using PHP?
Summary of pitfalls

After enabling the Private Space feature in Qiniu Cloud Object Storage and subsequently activating the Timestamp-based Anti-Link Hijacking feature, how can you retrieve a download link using PHP?

After enabling the Private Space feature in Qiniu Cloud Object Storage and subsequently activating the Timestamp-based Anti-Link Hijacking feature, how can you retrieve a download link using PHP?

20231103164134244-图片

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:

  1. 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.

  1. 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:

  1. First, proceed. require 'vendor/autoload.php'; Import the required dependency files.

  2. Use namespace use Qiniu\Auth;use \Qiniu\Cdn\CdnManager; Import the `Auth` class and the `CdnManager` class.

  3. Defines an Access Key and a Secret Key; these should be replaced with actual values.

  4. Create an Auth object and pass in the Access Key and Secret Key for authentication.

  5. 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.

  6. 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.

  7. By invoking $auth->privateDownloadUrl($url2) Method: Generate a download link for private space files signedUrl1This link has not been protected against URL劫持 using a timestamp.

  8. Defined expiration time durationInSecondsThe unit is seconds, indicating the validity period of the timestamp-based防盗 linking link.

  9. Call CdnManager::createTimestampAntiLeechUrl($signedUrl1, $encryptKey, $durationInSeconds) Method: Provide a download link for private space files. signedUrl1Timestamp Anti-Link Hijacking Key encryptKey Valid until date durationInSeconds Pass this parameter as input to generate a download link with a timestamp-based anti-link-hijacking feature. signedUrl

  10. 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.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

How to upload files using Qiniu Cloud Object Storage with PHP? How to compress images during file upload?

How to upload files using Qiniu Cloud Object Storage with PHP? How to compress images during file upload? Summary of pitfalls

Recently, I came across an image-sharing platform and, out of necessity, decided to use Object Storage. After reviewing the official documentation, I finally managed to implement the functionality I needed – so, here's a summary! How do you upload files to Qiniu Cloud Object Storage? First, include the official SDK file in your project. Then, in PHP, simply write the code; due to the confidentiality requirements of my business, I will only share the code for the file-upload portion! <?php require'./vendor/a...
👁 173
Qiniu Cloud Object Storage enables simple PHP file uploads.

Qiniu Cloud Object Storage enables simple PHP file uploads. Summary of pitfalls

Qiniu Cloud Object Storage supports simple file uploads; the official documentation also provides upload code examples. The following code can be used to test file uploads—simply replace the placeholders with your own access key and storage bucket name. <?php require 'vendor/autoload.php'; use Qiniu\Auth; // Import the upload class use Qiniu\Storage\Uploa...
👁 193
How can I download files from my private storage using Qiniu Cloud Object Storage with PHP?

How can I download files from my private storage using Qiniu Cloud Object Storage with PHP? Summary of pitfalls

To download a file from a Qiniu Cloud Private Space, you first need to obtain its download link. You can follow these steps to do so: 1. Obtain the download domain name for your private space: -In the Qiniu Cloud backend, select the corresponding storage space. -Navigate to the "Domain Settings" page. -Locate the download domain name for your private space (e.g., http://<domain>); note this address for later use. 2. Generate a download link: Use the Qiniu Cloud PHP SDK...
👁 179
Qiniu Cloud – Are there any ways to prevent unauthorized transactions on my object storage account?

Qiniu Cloud – Are there any ways to prevent unauthorized transactions on my object storage account? Summary of pitfalls

The CDN supports various防盗链配置 options; different防盗链 mechanisms are suitable for different application scenarios. It is recommended to configure the following in the "CDN> Domain Name Management> Configuration> Access Control" section: 1. Referer-based防盗链: Only HTTP requests that include the corresponding Referer HTTP header can access the resource. However, from a technical perspective, the Referer header can be forged...
👁 375
What does "CDN return traffic" mean?

What does "CDN return traffic" mean? Summary of pitfalls

CDN origin traffic refers to the data transfer volume that occurs when a user requests content hosted on a CDN node; if the requested content is not cached at that node or if the cached content has expired, the CDN will send a request to the origin server to retrieve the original content, then transmit that content back to the CDN node over the network, and finally deliver it to the user. The amount of data transferred during this process is known as origin traffic. The primary purpose of a CDN is to cache content at nodes located as close as possible to the end users...
👁 342
What does outbound traffic to the external network mean in Object Storage?

What does outbound traffic to the external network mean in Object Storage? Summary of pitfalls

In object storage, external outbound traffic refers to the volume of data transmitted from the data center within the object storage service to the public internet. External outbound traffic is generated whenever a user downloads or accesses an object stored in the object storage service. The calculation of external outbound traffic is typically based on the total amount of data transferred, measured in bytes. This includes the amount of data transmitted to users over protocols such as HTTP, HTTPS, FTP, and others. The generation of external outbound traffic is primarily...
👁 476

Recommended reading

Detailed explanation of efficiency considerations between `curl` and `file_get_contents`

Detailed explanation of efficiency considerations between `curl` and `file_get_contents` Language Notes PHP

Both `curl` and `file_get_contents` can be used to retrieve web page content, but their implementation approaches and efficiency differ slightly. `curl` is a powerful network request library that supports various protocols and authentication methods, and allows for the customization of request headers, request bodies, and other parameters. When fetching web page content, `curl` can simulate browser behavior by configuring parameters such as request headers, request bodies, and cookies, thereby...
👁 227
(PC+WAP) Blue Marketing-oriented Hardware & Mechanical Equipment Website – pbootCMS Template; Motor & Fan Mechanical Equipment Website Source Code Download – 0113

(PC+WAP) Blue Marketing-oriented Hardware & Mechanical Equipment Website – pbootCMS Template; Motor & Fan Mechanical Equipment Website Source Code Download – 0113 Practical Collection pbootcms Template

This PbootCMS template features a blue marketing style and is ideal for the hardware and electromechanical, motor and fan, as well as mechanical equipment industries; it supports both PC and mobile devices. The page design focuses on product showcasing and technical specifications, effectively attracting customers in the industrial manufacturing sector. It helps electromechanical equipment companies enhance their brand awareness and increase their sales conversion rates online. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 46
(Adaptive mobile version) Responsive SEO-optimized website development and ranking improvement services – Company website using PBootCMS template; Download source code for company website development services – 0753

(Adaptive mobile version) Responsive SEO-optimized website development and ranking improvement services – Company website using PBootCMS template; Download source code for company website development services – 0753 Practical Collection pbootcms Template

A PbootCMS website template designed by a professional company specializing in responsive SEO optimization and website development and ranking – compatible with both PC and WAP devices. Its modern, tech-driven design effectively showcases SEO services, website development projects, and optimization results. This template helps digital marketing agencies demonstrate their expertise online and attract business clients seeking promotional solutions. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 73
Photography – Portraits & Landscape Websites Template 0226

Photography – Portraits & Landscape Websites Template 0226 Practical Collection Yiyou template

An EyouCMS template designed for photography, portrait, and landscape photography websites. Its visual art-style design is ideal for showcasing photographic works, photo albums, landscape images, and photography services. It helps photography studios display their portfolio online and attract clients. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 45
Responsive furniture and sofa customization company website template – 0895

Responsive furniture and sofa customization company website template – 0895 Practical Collection Yiyou template

An eyouCMS responsive website template designed for furniture and sofa customization companies. Its modern and comfortable design is ideal for showcasing sofa products, customization services, home decor solutions, and brand stories. This template helps furniture brands effectively present their products online and attract both residential and commercial clients. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCMS...
👁 56