{"id":20724068,"url":"https://github.com/jerray/qcloud-cos-php-sdk","last_synced_at":"2025-04-23T17:44:56.085Z","repository":{"id":56998189,"uuid":"55672715","full_name":"jerray/qcloud-cos-php-sdk","owner":"jerray","description":"腾讯云COS PHP SDK","archived":false,"fork":false,"pushed_at":"2016-04-20T04:02:29.000Z","size":24,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T02:04:13.098Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jerray.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-07T07:19:18.000Z","updated_at":"2021-08-17T03:02:51.000Z","dependencies_parsed_at":"2022-08-21T11:10:20.511Z","dependency_job_id":null,"html_url":"https://github.com/jerray/qcloud-cos-php-sdk","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerray%2Fqcloud-cos-php-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerray%2Fqcloud-cos-php-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerray%2Fqcloud-cos-php-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jerray%2Fqcloud-cos-php-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jerray","download_url":"https://codeload.github.com/jerray/qcloud-cos-php-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250484034,"owners_count":21438173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T04:12:06.508Z","updated_at":"2025-04-23T17:44:56.066Z","avatar_url":"https://github.com/jerray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"qcloud-cos-php-sdk\n===========\n\n腾讯云[对象存储服务 COS](http://www.qcloud.com/wiki/COS%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D) PHP SDK\n\n[![Latest Stable Version](https://poser.pugx.org/jerray/qcloud-cos-php-sdk/v/stable)](https://packagist.org/packages/jerray/qcloud-cos-php-sdk) [![Total Downloads](https://poser.pugx.org/jerray/qcloud-cos-php-sdk/downloads)](https://packagist.org/packages/jerray/qcloud-cos-php-sdk) [![Latest Unstable Version](https://poser.pugx.org/jerray/qcloud-cos-php-sdk/v/unstable)](https://packagist.org/packages/jerray/qcloud-cos-php-sdk) [![License](https://poser.pugx.org/jerray/qcloud-cos-php-sdk/license)](https://packagist.org/packages/jerray/qcloud-cos-php-sdk) [![Build Status](https://travis-ci.org/jerray/qcloud-cos-php-sdk.svg?branch=master)](https://travis-ci.org/jerray/qcloud-cos-php-sdk) [![Coverage Status](https://coveralls.io/repos/github/jerray/qcloud-cos-php-sdk/badge.svg?branch=master)](https://coveralls.io/github/jerray/qcloud-cos-php-sdk?branch=master)\n\n安装\n----\n\n```\ncomposer require jerray/qcloud-cos-php-sdk\n```\n\n使用方法\n--------\n\n### 创建客户端实例\n\n创建SDK客户端实例。`$options`中的参数请到腾讯云对象存储的[密钥管理](https://console.qcloud.com/cos/project)页面获取。\n\n```php\n$options = [\n    'appId' =\u003e 'Your app id',\n    'secretId' =\u003e 'Your secret id',\n    'secretKey' =\u003e 'Your secret key',\n];\n$cos = new jerray\\QCloudCos\\QCloudCos($options);\n```\n\n各接口返回结果详情参见腾讯云对象存储服务 [RESTful API文档](http://www.qcloud.com/wiki/RESTful_API%E6%96%87%E6%A1%A3)\n\n### 文件操作\n\n完整上传\n\n```php\n$localFilePath = '/path/to/a/local/file';\n$bucketName = 'bucket';\n$cosFilePath = '/remote/file/path';\n$bizAttr = 'File attributes';\n\ntry {\n    $response = $cos-\u003eupload($localFilePath, $bucketName, $cosFilePath, $bizAttr);\n    $code = $response-\u003ecode;\n    $fileUrl = $response-\u003edata-\u003eaccess_url;\n} catch (jerray\\QCloudCos\\Exceptions\\RequestException $e) {\n    $response = $e-\u003egetBody();\n    $httpMessage = $e-\u003egetMessage();\n    $httpCode = $e-\u003egetCode();\n} catch (Exception $e) {\n    // ...\n}\n```\n\n分片上传（超过一定大小的文件需要使用分片上传）\n\n```php\n// response为最后一片的响应，与完整上传结构相同\n$response = $cos-\u003euploadSlice($localFilePath, $bucketName, $cosFilePath, $bizAttr);\n```\n\n查询文件\n\n```php\n$response = $cos-\u003equeryFile($bucketName, $cosFilePath);\n```\n\n更新文件bizAttr\n\n```php\n$response = $cos-\u003eupdateFile($bucketName, $cosFilePath, $bizAttr);\n```\n\n删除文件\n\n```php\n$response = $cos-\u003edeleteFile($bucketName, $cosFilePath);\n```\n\n下载文件\n\n```\n$result = $cos-\u003edownloadFile($bucketName, $cosFilePath, $localFilePath);\n```\n\n### 目录操作\n\n创建目录\n\n```php\n$response = $cos-\u003ecreateFolder($bucketName, 'test/');\n```\n\n目录列表\n\n```php\n$limit = 20; // 每页列表数量\n$pattern = 'both'; // 显示所有文件和目录 file - 只显示文件；foler - 只显示目录\n\n// direction参数需要配合context参数使用\n// context为空时始终取第一页，第一页返回的数据中会含有context参数\n// 将此context值传入再次调用，即取到第二页\n// direction用来控制翻页方向，next下一页，prev前一页\n$context = '';\n$direction = 'next';\n\n// 取到第一页 返回当前context为第一页\n$response = $cos-\u003elistFolder($bucketName, 'test/', $limit, $pattern, $context, $direction);\n\n// next 向后翻页，取到第二页，返回当前context为第二页\n$response = $cos-\u003elistFolder($bucketName, 'test/', $limit, $pattern, $response-\u003edata-\u003econtext, 'next');\n\n// prev 向前翻页，取到第一页，返回当前context为第一页\n$response = $cos-\u003elistFolder($bucketName, 'test/', $limit, $pattern, $response-\u003edata-\u003econtext, 'prev');\n```\n\n更新目录bizAttr\n\n```php\n$response = $cos-\u003eupdateFolder($bucketName, 'test/');\n```\n\n目录查询\n\n```php\n$response = $cos-\u003equeryFolder($bucketName, 'test/');\n```\n\n删除目录\n\n**注意**：目录不为空时会删除失败并抛出 `jerray\\QCloudCos\\Exceptions\\ClientException` 异常\n\n```php\n$response = $cos-\u003edeleteFolder($bucketName, 'test/');\n```\n\n### Exceptions\n\n接口请求异常\n\n接口返回4xx错误时，抛出 `jerray\\QCloudCos\\Exceptions\\ClientException`。\n返回5xx错误时， 抛出 `jerray\\QCloudCos\\Exceptions\\ServerException`。\n这两个异常均继承自 `jerray\\QCloudCos\\Exceptions\\RequestException`。\n\n运行时异常\n\n涉及到本地文件读取的接口，如果本地文件不存在或读取失败，会抛出 `jerray\\QCloudCos\\Exceptions\\FileNotFoundException`。\n创建实例时，如果传入的自定义存储（参数`store`，可选参数）没有实现 `jerray\\QCloudCos\\Contracts\\Store` 接口，\n抛出 `jerray\\QCloudCos\\Exceptions\\InvalidStoreInstance` 。\n这两个异常均继承自 `jerray\\QCloudCos\\Exceptions\\RuntimeException`。\n\nLicense\n--------------\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjerray%2Fqcloud-cos-php-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjerray%2Fqcloud-cos-php-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjerray%2Fqcloud-cos-php-sdk/lists"}