Download the official SDK: https://github.com/tencentyun/cos-php-sdk-v5/releases

Upload procedure:

<?php
ini_set('max_execution_time',300);
require 'vendor/autoload.php';
$secretId = "";
$secretKey = "";
$region = "";
$bucket = "";
            
if(empty($argv[1])){
    $dt=date("Ymd",time()-86400);
}else{
    $dt=intval($argv[1]);
}
$local_path = "/www/wwwroot/$dt.tar.gz";
$key = "$dt.tar.gz";
if(!file_exists($local_path)) exit("file not exist");
$cosClient = new QcloudCosClient(
    array(
        'region' => $region,
        'schema' => 'https',
        'credentials'=> array(
            'secretId'  => $secretId ,
            'secretKey' => $secretKey
        )
    )
);
try {
    $result = $cosClient->upload($bucket,$key,$body=fopen($local_path,'rb'));
    print_r($result);
} catch (Exception $e) {
    echo($e);
}

Modify the path of the require file according to the SDK folder as appropriate.

You need to fill in the parameters for configuring 4 COS and obtain them from the COS console.

The $dt related code is automatically set to yesterday. You can also manually fill in the parameters during execution to achieve different dates. The default is yesterday.

$local_path is the local file path and file name, and $key is the path and file name stored in COS. If the name is repeated, the original file will be overwritten.

Finally, configure crontab for scheduled execution.

Leave a Reply