Home >Backend Development >PHP Tutorial >How PHP connects to Tencent Cloud COS storage service to implement file upload and download functions

How PHP connects to Tencent Cloud COS storage service to implement file upload and download functions

PHPz
PHPzOriginal
2023-07-05 17:05:252196browse

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

  1. Apply for a Tencent Cloud account and create a COS bucket.
  2. Install PHP SDK.

2. Implementation of the file upload function
Using the PHP SDK, we can easily implement the file upload function.

  1. Import SDK library

    require_once 'vendor/autoload.php';
    use QcloudCosClient;
    use QcloudCosExceptionServiceResponseException;
  2. 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.

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

  1. Import SDK library

    require_once 'vendor/autoload.php';
    use QcloudCosClient;
    use QcloudCosExceptionServiceResponseException;
  2. 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);
  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn