cosserver.php 1.9 KB

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