ailiangkuai / aliyun-oss-php-sdk
PHP 阿里云 OSS SDK (Swoole 分支)
Requires
- php: >=5.3
Requires (Dev)
- phpunit/phpunit: ~4.0
- satooshi/php-coveralls: ~1.0
This package is auto-updated.
Last update: 2024-09-09 09:27:53 UTC
README
(阿里云 OSS SDK Swoole 协程兼容版)
需要打开 Swoole 的 CURL HOOK,并安装 4.4.15 版本以上的 Swoole
README 中文版
概述
阿里云对象存储服务(OSS)是阿里云提供的一种云存储服务,具有海量容量、安全、低成本和高可靠性等特点。您可以通过调用 API 在任何时间和任何地点上传和下载数据,并通过网页控制台对数据进行简单管理。OSS 可以存储任何类型的文件,因此适用于各种网站、开发企业和开发者。
运行环境
- PHP 5.3+。
- cURL 扩展。
提示
- 在 Ubuntu 上,您可以使用 apt-get 软件包管理器安装 PHP cURL 扩展:
sudo apt-get install php5-curl
。
安装 OSS PHP SDK
-
如果您使用 composer 来管理项目依赖项,请在项目的根目录中运行以下命令
composer require aliyuncs/oss-sdk-php
您也可以在
composer.json
文件中声明对阿里云 OSS SDK for PHP 的依赖。"require": { "aliyuncs/oss-sdk-php": "~2.0" }
然后运行
composer install
以安装依赖项。在 Composer 依赖管理器安装后,将依赖项导入到您的 PHP 代码中require_once __DIR__ . '/vendor/autoload.php';
-
您还可以直接下载打包的 PHAR 文件,并将文件引入到您的代码中
require_once '/path/to/oss-sdk-php.phar';
-
下载 SDK 源代码,并将 SDK 目录下的
autoload.php
文件引入到您的代码中require_once '/path/to/oss-sdk/autoload.php';
快速使用
常用类
初始化 OSSClient
SDK 对 OSS 的操作通过 OSSClient 类执行。以下代码创建了一个 OSSClient 对象
<?php $accessKeyId = "<AccessKeyID that you obtain from OSS>"; $accessKeySecret = "<AccessKeySecret that you obtain from OSS>"; $endpoint = "<Domain that you select to access an OSS data center, such as "oss-cn-hangzhou.aliyuncs.com>"; try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); } catch (OssException $e) { print $e->getMessage(); }
对象操作
对象是 OSS 上最基本的数据单元。您可以简单地将对象视为文件。以下代码上传了一个对象
<?php $bucket= "<Name of the bucket in use. Pay attention to naming conventions>"; $object = "<Name of the object in use. Pay attention to naming conventions>"; $content = "Hello, OSS!"; // Content of the uploaded file try { $ossClient->putObject($bucket, $object, $content); } catch (OssException $e) { print $e->getMessage(); }
存储桶操作
存储桶是您用于管理存储对象的空间。它是用户对象管理的单元。每个对象都必须属于一个存储桶。以下代码创建了一个存储桶
<?php $bucket= "<Name of the bucket in use. Pay attention to naming conventions>"; try { $ossClient->createBucket($bucket); } catch (OssException $e) { print $e->getMessage(); }
处理返回结果
OSSClient 提供以下两种类型的返回数据从接口
-
PUT 和 DELETE 接口:如果没有返回 OSSException,则认为 null 由接口返回时 PUT 和 DELETE 操作成功。
-
GET 和 LIST 接口:如果没有返回 OSSException,则认为 GET 和 LIST 操作成功返回所需数据。例如,
<?php $bucketListInfo = $ossClient->listBuckets(); $bucketList = $bucketListInfo->getBucketList(); foreach($bucketList as $bucket) { print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n"); }
在上面的代码中,$bucketListInfo 落入 'OSS\Model\BucketListInfo' 数据类型。
运行示例项目
- 修改
samples/Config.php
以完成配置信息。 - 运行
cd samples/ && php RunAll.php
。
运行单元测试
-
运行
composer install
下载依赖库。 -
设置环境变量。
export OSS_ACCESS_KEY_ID=access-key-id export OSS_ACCESS_KEY_SECRET=access-key-secret export OSS_ENDPOINT=endpoint export OSS_BUCKET=bucket-name
-
运行
php vendor/bin/phpunit
许可协议
- MIT
联系我们
- 阿里云 OSS 官方网站.
- 阿里云 OSS 官方论坛.
- 阿里云 OSS 官方文档中心.
- 阿里云官方技术支持: 提交工单。