文档地址
https://help.aliyun.com/document_detail/32099.html?spm=a2c4g.11186623.6.766.74cdc839H0RSId
安装(此处为composer安装)
1、PHP 5.3+
2、扩展依赖:cURL
3、在Ubuntu系统中,请使用apt-get包管理器安装PHP的cURL扩展 sudo apt-get install php-curl
4、在CentOS系统中,请使用yum包管理器安装PHP的cURL扩展 sudo yum install php-curl
composer安装
1、根目录执行:composer require aliyuncs/oss-sdk-php 或者 在composer.json文件中添加依赖关系.
2、如果使用composer出现网络错误,可以使用composer中国区的镜像源。方法是在命令行执行composer config -g repositories.packagist composer http://packagist.phpcomposer.com
"require": {
"aliyuncs/oss-sdk-php": "~2.x.x"
}
# composer install
// vendor/目录下包含了所依赖的库。您需要在app.php中添加依赖关系
# require_once __DIR__ . '/vendor/autoload.php';
源码安装方式
1、Github地址:https://github.com/aliyun/aliyun-oss-php-sdk/releases?spm=a2c4g.11186623.2.11.35dac83942Vl1K
2、 在GitHub中选择相应版本并下载打包好的zip文件
3、解压后的根目录中包含一个autoload.php文件,在代码中引入此文件:require_once ‘/path/to/oss-sdk/autoload.php’;
常见操作
- 创建存储空间
- 上传文件
- 下载文件
- 列举文件
- 删除文件
① 创建存储空间:

<?php
/*
* 创建存储空间
*/
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
//阿里云主账号AccessKey拥有所有API的访问权限
//创建RAM账号:https://ram.console.aliyun.com
$accessKeyId = "YOUR ACCESSID";//需修改
$accessKeySecret = "YOUR ACCESSSECRET";//需修改
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";//OSS数据中心访问域名,此处为杭州,以实际情况为准
$bucket = "jason-test0727";// 存储空间名称(数字、小写字母、-),唯一性
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->createBucket($bucket);
} catch (OssException $e) {
print $e->getMessage();
}
② 上传文件:
<?php
/**
* 上传文件
* PS:如果服务器上有同名的文件,则文件内容会被覆盖
*/
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
//accessId 和 accessSecret
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
//数据中心,此处为杭州
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//选择存储空间
$bucket= "jason-test0727";
//自定义文件名
$object = "Hello.md";
//自定义文件内容
$content = "Hello World!";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
③ 下载文件:
<?php
/**
* 下载文件
* PS:只是获取到文件中的内容,未将整个文件下载到本地
*/
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
//accessId 和 accessSecret
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "TNua9ItLYfhyt0OFX6RR1oTPMKQ2gX";
//数据中心,此处为杭州
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//选择存储空间
$bucket= "jason-test0727";
//选择文件名
$object = "Hello.md";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$content = $ossClient->getObject($bucket, $object);
print("object content: " . $content);
} catch (OssException $e) {
print $e->getMessage();
}
④ 列举文件:
<?php
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 存储空间名称
$bucket= "jason-test0727";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$listObjectInfo = $ossClient->listObjects($bucket);
$objectList = $listObjectInfo->getObjectList();
if (!empty($objectList)) {
foreach ($objectList as $objectInfo) {
print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "<br>");
}
}
} catch (OssException $e) {
print $e->getMessage();
}
⑤ 删除文件
<?php
if (is_file(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 存储空间名称
$bucket= "jason-test0727";
// 文件名称
$object = "hello.txt";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->deleteObject($bucket, $object);
} catch (OssException $e) {
print $e->getMessage();
}