cosserver.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. //腾讯云链接用
  3. namespace app\common\server;
  4. use Qcloud\Cos\Client;
  5. class cosserver
  6. {
  7. public function __construct()
  8. {
  9. require EXTEND_PATH . '/tencent-php/vendor/autoload.php';
  10. }
  11. public function getconfig()
  12. {
  13. $data = [
  14. 'secretId' => 'AKID2uVrwjGXWEfpjMjqhJejY6ML2TdV6SXL',
  15. 'secretKey' => 'Yqrb3EH4ZnlSs2xaw1qOffNoyrSnrfCU',
  16. 'region' => "ap-nanjing",
  17. ];
  18. return $data;
  19. }
  20. public function uploadfiles($path, $dir, $filename, $bucket)
  21. {
  22. $config = $this->getconfig();
  23. //$dirname = "workcard/" . $dir;
  24. $key = $dir . '/' . $filename;
  25. $cosClient = new Client(
  26. array(
  27. 'region' => $config['region'],
  28. 'schema' => 'https', //协议头部,默认为http
  29. 'credentials' => array(
  30. 'secretId' => $config['secretId'],
  31. 'secretKey' => $config['secretKey'],
  32. ),
  33. )
  34. );
  35. //key可以设置路径
  36. try {
  37. $result = $cosClient->putObject(array(
  38. //'Bucket' => 'backup-1306866048', //格式:BucketName-APPID
  39. 'Bucket' => $bucket,
  40. 'Key' => $key,
  41. 'Body' => fopen($path, 'rb'),
  42. //'Body' => $base64,
  43. ));
  44. // 请求成功
  45. $path = $result['Location'];
  46. return backarr(1, 'success', ['path' => 'http://' . $path]);
  47. } catch (\Exception $e) {
  48. // 请求失败
  49. return backarr(0, $e->getMessage());
  50. }
  51. }
  52. }