To download a file from a private space in Qiniu Cloud Object Storage, you first need to obtain the download link for that file. You can follow these steps to do so:
- 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 based on parameters such as the expiration time of the download link and the file name. The example code is as follows:
require 'vendor/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
$accessKey = '<AccessKey>';
$secretKey = '<SecretKey>';
$bucket = '<BucketName>';
// 初始化签权对象
$auth = new Auth($accessKey, $secretKey);
// 下载链接有效期为1小时
$expires = 3600;
// 文件名
$key = 'test.jpg';
// 生成私有空间文件的下载链接
$baseUrl = 'http(s)://<Domain>/<Path>';
$downloadUrl = $auth->privateDownloadUrl($baseUrl, $expires);among,`baseUrl` is the access URL for files in a private space, i.e.http(s)://<Domain>/<Path>/<FileName>,`Expires` specifies the validity period of the download link (in seconds); `$downloadUrl` is the generated download link.
- Use the download link to download the file.
Simply use the generated download link $downloadUrl to download the file via your web browser or a download utility.
Please note that the file download link for a private space can only be accessed once and has an expiration date. If you need to access the file multiple times, you can use the persistent storage feature in Qiniu Cloud Object Storage to migrate the file to a public space and generate a permanent link for that public space.