cosserver.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /**
  15. * SecretId: AKIDra9VetxqUwBCyRjqlU1LQIzVxGRQDmkX
  16. * SecretKey: gDWjjXAj7IPL8nlcHl4C8qOjsoZCsNdh
  17. */
  18. 'secretId' => 'AKID2uVrwjGXWEfpjMjqhJejY6ML2TdV6SXL',
  19. 'secretKey' => 'Yqrb3EH4ZnlSs2xaw1qOffNoyrSnrfCU',
  20. //use ap-beijing
  21. 'region' => "ap-nanjing",
  22. ];
  23. return $data;
  24. }
  25. public function uploadfiles($path, $dir, $filename, $bucket)
  26. {
  27. $config = $this->getconfig();
  28. //$dirname = "workcard/" . $dir;
  29. $key = $dir . '/' . $filename;
  30. $cosClient = new Client(
  31. array(
  32. 'region' => $config['region'],
  33. 'schema' => 'https', //协议头部,默认为http
  34. 'credentials' => array(
  35. 'secretId' => $config['secretId'],
  36. 'secretKey' => $config['secretKey'],
  37. ),
  38. )
  39. );
  40. //key可以设置路径
  41. try {
  42. $result = $cosClient->putObject(array(
  43. //'Bucket' => 'backup-1306866048', //格式:BucketName-APPID
  44. 'Bucket' => $bucket,
  45. 'Key' => $key,
  46. 'Body' => fopen($path, 'rb'),
  47. //'Body' => $base64,
  48. ));
  49. // 请求成功
  50. $path = $result['Location'];
  51. return backarr(1, 'success', ['path' => 'http://' . $path]);
  52. } catch (\Exception $e) {
  53. // 请求失败
  54. return backarr(0, $e->getMessage());
  55. }
  56. }
  57. }