How PHP connects to Tencent Cloud COS storage service to implement file upload and download functions
Tencent Cloud provides a series of rich cloud services, including Cloud Object Storage (COS), which is a A highly reliable and scalable cloud storage service. This article will introduce how to use PHP to connect to Tencent Cloud COS storage service to implement file upload and download functions.
1. Preparation
2. Implementation of the file upload function
Using the PHP SDK, we can easily implement the file upload function.
Import SDK library
require_once 'vendor/autoload.php'; use QcloudCosClient; use QcloudCosExceptionServiceResponseException;
Initialize API interface
$bucket = 'your-bucket-name'; $region = 'your-bucket-region'; $credentials = new Credential( 'your-secret-id', 'your-secret-key' ); $client = new Client($credentials, $region);
Here you need to change your- in the above code Replace bucket-name
and your-bucket-region
with your COS bucket name and region information. In addition, your-secret-id
and your-secret-key
are replaced with the SecretId and SecretKey of your Tencent Cloud account respectively.
Upload files
$file = '/path/to/local/file.ext'; $key = 'remote/file.ext'; $options = [ 'Bucket' => $bucket, 'Key' => $key, ]; try { $result = $client->putObject([ 'Bucket' => $bucket, 'Key' => $key, 'Body' => fopen($file, 'rb') ]); echo '文件上传成功'; } catch (ServiceResponseException $e) { echo '文件上传失败:' . $e->getMessage(); }
In the above code, you need to replace /path/to/local/file.ext
with the path of the local file , remote/file.ext
is replaced with the path of the remote file in the COS bucket. The putObject
method is used to upload an object to the specified bucket.
3. Implementation of the file download function
Using the PHP SDK, we can easily implement the file download function.
Import SDK library
require_once 'vendor/autoload.php'; use QcloudCosClient; use QcloudCosExceptionServiceResponseException;
Initialize API interface
$bucket = 'your-bucket-name'; $region = 'your-bucket-region'; $credentials = new Credential( 'your-secret-id', 'your-secret-key' ); $client = new Client($credentials, $region);
Download file
$key = 'remote/file.ext'; $saveAs = '/path/to/local/file.ext'; $options = [ 'Bucket' => $bucket, 'Key' => $key, 'SaveAs' => $saveAs, ]; try { $result = $client->getObject($options); echo '文件下载成功'; } catch (ServiceResponseException $e) { echo '文件下载失败:' . $e->getMessage(); }
In the above code, remote/file.ext
needs to be replaced with the path of the remote file in the COS bucket, /path/to/local/file.ext
It is the local path saved after downloading.
4. Summary
This article uses the PHP SDK and the API interface provided by Tencent Cloud COS storage service to briefly introduce how to implement the file upload and download functions. By connecting to Tencent Cloud COS storage service, we can achieve highly reliable and scalable file storage and access functions.
The above is a brief introduction to using PHP to connect to Tencent Cloud COS storage service to implement file upload and download functions. Hope this article can be helpful to you.
The above is the detailed content of How PHP connects to Tencent Cloud COS storage service to implement file upload and download functions. For more information, please follow other related articles on the PHP Chinese website!