PHP上传文件至腾讯云对象存储

一、开通腾讯云对象存储服务

点击前往腾讯云官网开通,开通后前往控制台获取 secretIdsecretKey

二、安装 PHP SDK
  1. 在你项目的 composer.json 文件中加入:
{
	"require": {
	    "qcloud/cos-sdk-v5": ">=2.0"
	}
}
  1. 执行 composer install 进行安装。
三、代码实现
<?php
namespace app\api\common\controller;

use Qcloud\Cos\Client;

class Upload
{
    private $secretId = ""; // 云 API 密钥 SecretId;
    private $secretKey = ""; // 云 API 密钥 SecretKey;
    
    // 上传资源至腾讯云对象存储
    public function uploadToTencentCos()
    {
        $region = "ap-beijing";
        // 初始化
        $cosClient = new Client(array(
            'region' => $region,
            'schema' => 'http',
            'credentials'=> array(
                'secretId'  => $this->secretId,
                'secretKey' => $this->secretKey
            )
        ));
        // 接收文件
        $file = $_FILES['file'];
        // 重新命名文件
        $newName = uniqid().time().strrchr($file['name'], '.');
        // 上传资源
        $result = $cosClient->putObject(array(
            'Bucket' => '', // 你的 bucket 名称
            'Key' => $newName,
            'Body' => fopen($file['tmp_name'], 'rb'),
            'ContentType' => 'image/jpg,image/png,image/jpeg,image/tmp'
        ));
        // 查看结果
        print_r($result);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是PHP实现腾讯云对象存储API签名的代码: ```php <?php // 腾讯云对象存储API的endpoint和密钥 $endpoint = "https://your-bucket.cos.ap-guangzhou.myqcloud.com"; $secretId = "your-secret-id"; $secretKey = "your-secret-key"; function getAuthorization($method, $path, $headers, $params) { global $secretId, $secretKey; $qSignAlgorithm = "sha1"; $qAk = $secretId; $qSignTime = time() . ";" . (time() + 3600); // 签名过期时间为当前时间+1小时 $qKeyTime = $qSignTime; $qHeaderList = implode(";", array_keys($headers)); $qUrlParamList = implode(";", array_keys($params)); $url = parse_url($path); $qPath = isset($url['path']) ? $url['path'] : '/'; $qPath .= isset($url['query']) ? '?' . $url['query'] : ''; $qSignString = implode("\n", array( $method, $qPath, http_build_query($params), implode("\n", array_map(function($k, $v) { return strtolower($k) . ":" . $v; }, array_keys($headers), $headers)), '', $qHeaderList, '', $qUrlParamList, )); $qSignKey = hash_hmac("sha1", $qKeyTime, $secretKey, true); $qSignature = hash_hmac($qSignAlgorithm, $qSignString, $qSignKey); return "q-sign-algorithm=" . $qSignAlgorithm . "&q-ak=" . $qAk . "&q-sign-time=" . $qSignTime . "&q-key-time=" . $qKeyTime . "&q-header-list=" . $qHeaderList . "&q-url-param-list=" . $qUrlParamList . "&q-signature=" . $qSignature; } // 示例:上传文件腾讯云对象存储 $file = "/path/to/your/file.jpg"; $bucket = "your-bucket"; $key = "file.jpg"; $headers = array( "Host" => "your-bucket.cos.ap-guangzhou.myqcloud.com", "Content-Type" => "image/jpeg", "Content-Length" => filesize($file), ); $params = array( "op" => "upload", "insertOnly" => "0", "bizAttr" => "", "version" => "1", ); $authorization = getAuthorization("PUT", "/" . $key, $headers, $params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $endpoint . "/" . $key . "?" . http_build_query($params)); curl_setopt($ch, CURLOPT_PUT, 1); curl_setopt($ch, CURLOPT_INFILE, fopen($file, "r")); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: " . $authorization, "Content-Type: " . $headers["Content-Type"], "Content-Length: " . $headers["Content-Length"], )); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ?> ``` 以上代码中,`getAuthorization`函数用于生成腾讯云对象存储API的签名,`$headers`和`$params`参数分别为HTTP请求的头部和参数,`$method`和`$path`参数分别为HTTP请求的方法和路径。 示例中使用了`curl`库来上传文件腾讯云对象存储,需要先将文件内容读入到内存中,然后通过`CURLOPT_INFILE`和`CURLOPT_INFILESIZE`选项传递给`curl`库。`$bucket`和`$key`参数分别为腾讯云对象存储的存储桶和对象名称。上传文件时,需要指定`op=upload`参数,表示上传操作。其他参数可根据需要进行设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

相逢不晚何必匆匆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值