hy-xusong/aliyun-oss-php-sdk-swoole

适用于 PHP 的阿里云 OSS SDK(swoole 版本)

v1.0.1 2021-10-09 01:40 UTC

This package is auto-updated.

Last update: 2024-09-09 08:11:11 UTC


README

(阿里云 OSS SDK Swoole 协程兼容版)

需要开启 Swoole 的 CURL HOOK,并安装 4.4.15 版本以上的 Swoole

README 中文版

概述

阿里云对象存储服务(OSS)是阿里云提供的一种云存储服务,具有大容量、安全、低成本和高可靠性。您可以通过调用 API 在任何时间、任何地点上传和下载数据,并通过 Web 控制台对数据进行简单的管理。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

联系我们