You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace tencent\cos\object;
use think\facade\Request;
use tencent\cos\Base;
class File extends Base
{
/**
* 文件上传
* @param string $local_file_url 本地存储路径,填写的时候以'./'开头
* @param string $cos_file_dirname 存放文件的文件夹层级例如uid2
* @param string $cos_file_name 文件名例如2.txt
* @param boolean $is_delete_file 上传成功之后是否删除源文件(false--不删除 true--删除),本地调试时无法删除
* @date 2022-11-28
*/
public function uploadFile($local_file_url, $cos_file_dirname, $cos_file_name = '', $is_delete_file = true)
{
// 上传到COS的文件没有指定文件名自动生成
if (empty($cos_file_name)) {
// 文件名为当前微秒加密
$cos_file_name = md5(microtime(true));
// 获取后缀名
$suffix = get_suffix($local_file_url);
// 文件名
$cos_file_name .= '.' . $suffix;
}
// 上传到cos的文件指定文件名只需拼接项目文件夹
$object = $this->config['first_directory'] . '/' . $cos_file_dirname . '/' . $cos_file_name;
// 存储桶名称由BucketName-Appid 组成
$bucket = $this->config['bucket_name'];
try {
$cosClient = new \Qcloud\Cos\Client([
'region' => $this->config['region'],
// 协议头部默认为http
'schema' => Request::scheme(),
'credentials' => [
'secretId' => $this->config['secret_id'],
'secretKey' => $this->config['secret_key']
]
]);
$body = fopen($local_file_url, 'rb');
// 上传对象
$result = $cosClient->upload($bucket, $object, $body);
$result = $this->resultToArray($result);
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object,
'local_file_url' => $local_file_url
], $result, 'tencent_cos_upload_file_uid_' . $uid);
if ($is_delete_file) {
// 删除原文件
unlink($local_file_url);
}
// 返回成功数据
return 'https://' . $result['Location'];
} catch (\Exception $e) {
$result['Code'] = 5000;
$result['Message'] = $e->getMessage();
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object,
'local_file_url' => $local_file_url
], $result, 'tencent_cos_upload_file_uid_' . $uid);
return $result;
}
}
/**
* 将object下载到服务器指定的文件
* @param string $object object开头没有'/'
* @param string $local_file_url 本地存储路径,填写的时候以'./'开头
* @date 2022-11-29
*/
public function download($object, $local_file_url = '')
{
// 判断$object是否为带http连接的全路径
if (strpos($object, 'https://') !== false || strpos($object, 'http://') !== false) {
// 分割字符串
$array = explode('://', $object);
// 获取第1个/之后的字符串即为$object
$index = strpos($array[1], '/');
$object = substr($array[1], $index + 1);
}
// 获取文件后缀名
$suffix = get_suffix($object);
// 没有定义本地存储路径的时候,自动生成文件保存的本地路径
if (!$local_file_url) {
$local_file_url = './temp/' . md5(microtime(true)) . '.' . $suffix;
}
// 没有文件夹则先生成
create_directory($local_file_url);
// 存储桶名称由BucketName-Appid 组成
$bucket = $this->config['bucket_name'];
try {
// 实例化
$cosClient = new \Qcloud\Cos\Client([
'region' => $this->config['region'],
// 协议头部默认为http
'schema' => Request::scheme(),
'credentials' => [
'secretId' => $this->config['secret_id'],
'secretKey' => $this->config['secret_key']
]
]);
// 下载
$result = $cosClient->download($bucket, $object, $local_file_url);
// 结果转为数组
$result = $this->resultToArray($result);
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object,
'local_file_url' => $local_file_url
], $result, 'tencent_cos_get_object_uid_' . $uid);
// 返回成功数据
return [
// 本地路径
'local_file_url' => $local_file_url,
// 带域名的本地路径
'domain_local_file_url' => Request::domain(true) . substr($local_file_url, 1)
];
} catch (\Exception $e) {
// 返回报错
$result['Code'] = 5000;
$result['Message'] = $e->getMessage();
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object,
'local_file_url' => $local_file_url
], $result, 'tencent_cos_get_object_uid_' . $uid);
// 返回错误数据
return $result;
}
}
/**
* 删除cos文件
* @param string $object object路径已包含项目文件夹路径
* @date 2022-11-29
*/
function deleteObject($object)
{
// 判断$object是否为带http连接的全路径
if (strpos($object, 'https://') !== false || strpos($object, 'http://') !== false) {
// 分割字符串
$array = explode('://', $object);
// 获取第1个/之后的字符串即为$object
$index = strpos($array[1], '/');
$object = substr($array[1], $index + 1);
}
// 存储桶名称由BucketName-Appid 组成
$bucket = $this->config['bucket_name'];
try {
// 实例化
$cosClient = new \Qcloud\Cos\Client([
'region' => $this->config['region'],
// 协议头部默认为http
'schema' => Request::scheme(),
'credentials' => [
'secretId' => $this->config['secret_id'],
'secretKey' => $this->config['secret_key']
]
]);
// 下载
$result = $cosClient->deleteObject([
'Bucket' => $bucket,
'Key' => $object
]);
// 结果转为数组
$result = $this->resultToArray($result);
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object
], $result, 'tencent_cos_delete_object_uid_' . $uid);
// 返回成功数据
return $result;
} catch (\Exception $e) {
// 返回报错
$result['Code'] = 5000;
$result['Message'] = $e->getMessage();
// 记录日志
$uid = defined('UID') ? UID : '';
platformLog([
'bucket' => $bucket,
'key' => $object
], $result, 'tencent_cos_delete_object_uid_' . $uid);
// 返回错误数据
return $result;
}
}
}