wlfpanda1012/aliyun-sts

使用最新版本的SDK封装了阿里云openapi系列中STS接口的Hyperf框架,并通过composer包进行封装。

v1.0.4 2024-08-27 06:12 UTC

This package is auto-updated.

Last update: 2024-09-27 06:35:13 UTC


README

Php Version Swoole Version

介绍

Hyperf 框架编写的基于阿里云openapi系列 sts 接口的最新版本SDK的封装包,并对 OSS 服务进行了优化封装。

安装

composer require wlfpanda1012/aliyun-sts

发布配置

 php bin/hyperf.php vendor:publish wlfpanda1012/aliyun-sts

如何使用

  • STS_ACCESS_KEY_IDSTS_ACCESS_KEY_SECRETSTS_ROLE_ARN 填写到 .env 文件中

如果你想使用原生的 STS 功能

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
use Wlfpanda1012\AliyunSts\StsService;

$effect = 'Allow';
$action = ['*'];
$resource = ['*'];
$condition = null;
$roleSessionName = 'session_name';
$service = \Hyperf\Support\make(StsService::class, ['option' => \Hyperf\Config\config('sts')]);
// 具体Statement参数请查询阿里云官方文档
$assumeRoleRequest = $service->generateAssumeRoleRequest($service->generatePolicy($service->generateStatement($effect, $action, $resource, $condition)), $roleSessionName);
$credentials = $service->getCredentials($service->assumeRole($assumeRoleRequest));

如果想使用封装好的 OSS-STS 功能,你只需要

  • 为了简化操作和降低用户心智负担,建议只传入 path
  • 如果你对权限时间有要求,可以传入时间,默认 time = 3600
  • 下载和上传默认使用了通配符形式。
  • 如果你需要更多个性化的操作,提供了 OSSAction 供你直观选择。
  • (如果枚举类中没有你想要的类型欢迎提 PR,当然也可以直接使用通配符降低负担。)
<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
use Wlfpanda1012\AliyunSts\Constants\OSSAction;
use Wlfpanda1012\AliyunSts\Oss\OssRamService;

$service = \Hyperf\Support\make(OssRamService::class, ['option' => \Hyperf\Config\config('sts')]);

/**
 * 如果你想获得下载文件的token,可以使用以下代码
 * 支持数组和字符串.
 */
$token = $service->allowGetObject('path/to/file');
$token = $service->allowGetObject('path/to/file', 3600);
$token = $service->allowGetObject(['path/to/file1', 'path/to/file2'], 3600);
/**
 * 如果你不想使用通配行为.
 */
$token = $service->allowGetObject(['path/to/file1', 'path/to/file2'], 3600, ['actions' => OSSAction::GET_OBJECT]);
$token = $service->allowGetObject(['path/to/file1', 'path/to/file2'], 3600, ['actions' => [OSSAction::GET_OBJECT, OSSAction::GET_OBJECT_ACL]]);

/**
 * allowPutObject
 * denyPutObject
 * denyGetObject
 * 功能同上.
 */