1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- //腾讯云链接用
- namespace app\common\server;
- use Qcloud\Cos\Client;
- class cosserver
- {
- public function __construct()
- {
- require EXTEND_PATH . '/tencent-php/vendor/autoload.php';
- }
- public function getconfig()
- {
- $data = [
- /**
- * SecretId: AKIDra9VetxqUwBCyRjqlU1LQIzVxGRQDmkX
- * SecretKey: gDWjjXAj7IPL8nlcHl4C8qOjsoZCsNdh
- */
- 'secretId' => 'AKID2uVrwjGXWEfpjMjqhJejY6ML2TdV6SXL',
- 'secretKey' => 'Yqrb3EH4ZnlSs2xaw1qOffNoyrSnrfCU',
- //use ap-beijing
- 'region' => "ap-nanjing",
- ];
- return $data;
- }
- public function uploadfiles($path, $dir, $filename, $bucket)
- {
- $config = $this->getconfig();
- //$dirname = "workcard/" . $dir;
- $key = $dir . '/' . $filename;
- $cosClient = new Client(
- array(
- 'region' => $config['region'],
- 'schema' => 'https', //协议头部,默认为http
- 'credentials' => array(
- 'secretId' => $config['secretId'],
- 'secretKey' => $config['secretKey'],
- ),
- )
- );
- //key可以设置路径
- try {
- $result = $cosClient->putObject(array(
- //'Bucket' => 'backup-1306866048', //格式:BucketName-APPID
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => fopen($path, 'rb'),
- //'Body' => $base64,
- ));
- // 请求成功
- $path = $result['Location'];
- return backarr(1, 'success', ['path' => 'http://' . $path]);
- } catch (\Exception $e) {
- // 请求失败
- return backarr(0, $e->getMessage());
- }
- }
- }
|