TencentCosHelper.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\DataApiNew\Helper;
  3. use Qcloud\Cos\Client;
  4. use Qcloud\Cos\Exception\ServiceResponseException;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 腾讯云cos 存储桶
  8. */
  9. class TencentCosHelper
  10. {
  11. public $cosClient;
  12. public function __construct()
  13. {
  14. // SECRETID 和 SECRETKEY 请登录访问管理控制台进行查看和管理
  15. $secretId = "AKIDsZ2IMEu24wfCbpW2KNFxWwRwNj9VkvDM"; //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
  16. $secretKey = "yujR4a9vXxLhw584JOH75tUs34uX3MRJ"; //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
  17. $region = "ap-shanghai"; //用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
  18. $data = [
  19. 'region' => $region,
  20. 'schema' => 'https', //协议头部,默认为 http
  21. 'credentials' => [
  22. 'secretId' => $secretId,
  23. 'secretKey' => $secretKey
  24. ]
  25. ];
  26. $this->cosClient = new Client($data);
  27. }
  28. /**
  29. * 上传腾讯存储桶
  30. *
  31. * @param [string] $files 文件路径
  32. * @return array
  33. */
  34. public function uploadFiles($files,$key = null)
  35. {
  36. try {
  37. $bucket = 'file-1314054542';
  38. $key = $key??$files; //文件名
  39. $local_path = $files;
  40. $body = fopen($local_path, 'rb');
  41. $result = $this->cosClient->upload(
  42. $bucket = $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
  43. $key = $key, //此处的 key 为对象键
  44. $body = $body
  45. );
  46. return ['code' => 1, 'msg' => '上传成功', 'data' => ['path' => 'https://' . $result['Location']]];
  47. } catch (ServiceResponseException $e) {
  48. // 腾讯云COS SDK异常
  49. Log::error("腾讯云COS上传失败1:".$e);
  50. return ['code' => 0, 'msg' => '上传失败', 'data' => []];
  51. } catch (\Exception $e) {
  52. // 请求失败
  53. Log::error("腾讯云COS上传失败2:".$e);
  54. return ['code' => 0, 'msg' => '上传失败', 'data' => []];
  55. }
  56. }
  57. //查询对象是否存在
  58. public function checkFileExist($key)
  59. {
  60. try {
  61. $bucket = 'file-1314054542';
  62. $result = $this->cosClient->doesObjectExist($bucket, $key);
  63. if ($result) {
  64. return ['code' => 1, 'msg' => 'success'];
  65. } else {
  66. return ['code' => 0, 'msg' => 'no exist'];
  67. }
  68. } catch (\Exception $e) {
  69. return ['code' => 0, 'msg' => $e->getMessage()];
  70. }
  71. }
  72. //获取对象链接
  73. public function getFileUrl($key)
  74. {
  75. try {
  76. $bucket = 'file-1314054542';
  77. // 获取对象链接
  78. $objectUrl = $this->cosClient->getObjectUrl($bucket, $key);
  79. return ['code' => 1, 'msg' => 'success','data'=>$objectUrl];
  80. } catch (\Exception $e) {
  81. return ['code' => 0, 'msg' => $e->getMessage()];
  82. }
  83. }
  84. }