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

Qiniu Cloud Object Storage enables simple PHP file uploads.

Qiniu Cloud Object Storage enables simple PHP file uploads.

Qiniu Cloud Object Storage enables simple file uploads.

The official documentation also includes sample code; simply replace the placeholder for your API key and storage bucket name to test file uploading.

<?php
 require 'vendor/autoload.php';
 use Qiniu\Auth;
 // 引入上传类
use Qiniu\Storage\UploadManager;
  // 用于签名的公钥和私钥
$accessKey = '';
$secretKey = '';
$bucket = '';
  // 初始化签权对象
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);

// 要上传文件的本地路径
$filePath = './370009-2.png';
// 上传到存储后保存的文件名
$key = 'test/370009-2.png';
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
echo "\n====> putFile result: \n";
if ($err !== null) {
    var_dump($err);
} else {
    var_dump($ret);
}

Code Interpretation

This code snippet implements file uploading using the PHP SDK for Qiniu Cloud Object Storage Service. The following is an analysis of the code:

  1. Import dependency libraries

First, the dependency library 'vendor/autoload.php' required for the Qiniu PHP SDK is introduced; since no specific path is provided, it is evident that the SDK was installed using Composer within the project's current directory.

  1. Key value and Bucket name

Assign the values of accessKey, secretKey, and bucket to respective variables. accessKey、`secretKey` and `$bucket`: these values are derived from the Access Key and Secret Key created in the Qiniu Cloud backend account, as well as the name of the storage space (Bucket) to be uploaded to.

  1. Initialize signing object

Use `accessKey` and `secretKey` to initialize an `Auth` object; this object is used for signing operations to prevent unauthorized access to API requests and ensure data security.

  1. Get upload credentials

Call the `uploadToken` method on the signing object to obtain the upload token `$token`; this token contains all the necessary parameters required for uploading a file, such as upload policy, upload storage space, upload expiration time, and more.

  1. Upload file

Use the UploadManager object to call the `putFile` method to upload a local file to the specified Bucket.The `token` parameter serves as the upload credential.`key` is the filename of the file to be uploaded and stored in the Bucket; `$filePath` is the local file path of the file to be uploaded. When using this method to upload a file, you can also specify optional upload parameters, such as the file type or whether to overwrite an existing file with the same name.

  1. Output upload result

Finally, use the `var_dump()` function to output the upload results ($ret and $err); if $err is `null`, it indicates a successful upload; otherwise, it indicates a failed upload – in this case, you can examine the error message contained in $err.

微信赞赏

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
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
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? Summary of pitfalls

This tool is really handy! After enabling the private space feature in Qiniu Cloud Object Storage, the timestamp-based防盗 linking feature was also activated. How can you obtain a download link? When the timestamp verification feature is enabled for your Qiniu Cloud Object Storage private space, you need to include a timestamp parameter when generating a temporary download link. Here are the specific steps: To obtain the download domain name for your private space: In the Qiniu Cloud backend, select the corresponding storage space, navigate to the "Domain Name Settings" page, and locate the section for the private space...
👁 212
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

Building Materials & Interior Decoration Website Templates – 0902

Building Materials & Interior Decoration Website Templates – 0902 Practical Collection Yiyou template

This EyouCMS template is ideal for the building materials, construction, and interior decoration industries. Its modern and professional design style effectively showcases building material products, construction projects, decoration solutions, and corporate strength. It helps building material and construction enterprises showcase their brands online and attract developers and property owners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 44
Law Firm Legal Advisory Website Template 0232

Law Firm Legal Advisory Website Template 0232 Practical Collection Yiyou template

An EyouCMS website template designed specifically for law firms and legal consulting businesses. Its professional and authoritative design effectively showcases legal services, attorney teams, successful case studies, and consultation booking features. This template helps law firms build a professional online presence and attract clients seeking legal services. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 63
(Adaptive mobile version) PBootCMS template for tea-related information websites; Download source code for tea product and tea knowledge information websites – 0758

(Adaptive mobile version) PBootCMS template for tea-related information websites; Download source code for tea product and tea knowledge information websites – 0758 Practical Collection pbootcms Template

A PbootCMS website template for tea-related information and knowledge sharing, compatible with both PC and WAP devices. Its classical and elegant design makes it ideal for showcasing tea products, tea culture knowledge, and industry news. This template helps tea brands or tea culture advocates attract tea enthusiasts online. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 35
(PC + WAP) Cake & Bread Food Website – pbootCMS Template; Download Source Code for Food & Snack Websites (Cake & Pastries Category) – 0117

(PC + WAP) Cake & Bread Food Website – pbootCMS Template; Download Source Code for Food & Snack Websites (Cake & Pastries Category) – 0117 Practical Collection pbootcms Template

This PbootCMS template is ideal for the cake, bread, and food and confectionery industry, supporting both PC and mobile devices. Its cozy and appealing design effectively showcases baked goods and culinary culture. This template helps cake chain brands or food companies showcase their product ranges online, attract franchisees, and enhance their brand image. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 61
Smart Air Conditioning and Home Appliances Company Website Template 0903

Smart Air Conditioning and Home Appliances Company Website Template 0903 Practical Collection Yiyou template

An eyouCMS website template designed for air conditioning and smart home appliance manufacturers. Its modern, tech-driven design is ideal for showcasing air conditioning products, smart home appliances, system solutions, and brand identity. This template helps home appliance brands showcase their products online and attract both residential and commercial customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eyouCMS | eyouCMS...
👁 32