From 73c67585ecc87928b219d8b7000181f3b8804472 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 25 Jul 2023 10:00:05 +0800 Subject: [PATCH] Support RAM --- aliyun-oss-wordpress.php | 57 ++++++++++++++++++++++++++++++++++++++-- sdk/composer.json | 3 ++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/aliyun-oss-wordpress.php b/aliyun-oss-wordpress.php index 7bef04a..cb4debc 100644 --- a/aliyun-oss-wordpress.php +++ b/aliyun-oss-wordpress.php @@ -16,6 +16,9 @@ require_once 'sdk/vendor/autoload.php'; use OSS\OssClient; use OSS\Core\OssException; +use OSS\Credentials\CredentialsProvider; +use AlibabaCloud\Credentials\Credential; +use OSS\Credentials\StaticCredentialsProvider; define('OSS_VERSION', '1.4.0'); define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__))); @@ -24,6 +27,27 @@ if (!function_exists('get_home_path')) { require_once(ABSPATH . 'wp-admin/includes/file.php'); } +class AlibabaCloudCredentialsWrapper implements CredentialsProvider +{ + /** + * @var \OSS\Credentials\Credentials + */ + private $wrapper; + + public function __construct($wrapper) + { + $this->wrapper = $wrapper; + } + + public function getCredentials() + { + $ak = $this->wrapper->getAccessKeyId(); + $sk = $this->wrapper->getAccessKeySecret(); + $token = $this->wrapper->getSecurityToken(); + return new StaticCredentialsProvider($ak, $sk, $token); + } +} + // 初始化选项 register_activation_hook(__FILE__, 'oss_set_options'); // 初始化选项 @@ -40,6 +64,7 @@ function oss_set_options() 'upload_url_path' => '', // URL前缀 'style' => '', // 图片处理 'update_file_name' => 'false', // 是否重命名文件名 + 'role_name' => '' // 角色名称 ); add_option('oss_options', $options, '', 'yes'); } @@ -47,9 +72,25 @@ function oss_set_options() function oss_get_client() { $oss_opt = get_option('oss_options', true); + $roleName = esc_attr($oss_opt['role_name'] ?? ''); + $endpoint = oss_get_bucket_endpoint($oss_opt); + + if (!empty($roleName)) { + $ecsRamRole = new Credential([ + // 填写Credential类型,固定值为ecs_ram_role。 + 'type' => 'ecs_ram_role', + // 填写角色名称。 + 'role_name' => $roleName, + ]); + $providerWarpper = new AlibabaCloudCredentialsWrapper($ecsRamRole); + $provider = $providerWarpper->getCredentials(); + $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; + $config = ['provider' => $provider, 'endpoint'=> $protocol . $endpoint]; + return new OssClient($config); + } + $accessKeyId = esc_attr($oss_opt['accessKeyId']); $accessKeySecret = esc_attr($oss_opt['accessKeySecret']); - $endpoint = oss_get_bucket_endpoint($oss_opt); return new OssClient($accessKeyId, $accessKeySecret, $endpoint); } @@ -475,6 +516,7 @@ function oss_setting_page() if (!empty($_POST) and $_POST['type'] == 'oss_set') { $options['bucket'] = isset($_POST['bucket']) ? sanitize_text_field($_POST['bucket']) : ''; $options['regional'] = isset($_POST['regional']) ? sanitize_text_field($_POST['regional']) : ''; + $options['role_name'] = isset($_POST['role_name']) ? sanitize_text_field($_POST['role_name']) : ''; $options['accessKeyId'] = isset($_POST['accessKeyId']) ? sanitize_text_field($_POST['accessKeyId']) : ''; $options['accessKeySecret'] = isset($_POST['accessKeySecret']) ? sanitize_text_field($_POST['accessKeySecret']) : ''; $options['is_internal'] = isset($_POST['is_internal']) ? 'true' : 'false'; @@ -560,9 +602,11 @@ function oss_setting_page()