说明:存在部分步骤省略的情况,请根据具体文档进行操作
下载相关sdk
composer require qiniu/php-sdk
composer require aliyuncs/oss-sdk-php
composer require alibabacloud/sts-20150401
composer require qcloud/cos-sdk-v5
composer require qcloud_sts/qcloud-sts-sdk
# 如果不需要,请移除,示例:
# composer remove qcloud_sts/qcloud-sts-sdk
use Qiniu\Auth;
use AlibabaCloud\SDK\Sts\V20150401\Sts;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Sts\V20150401\Models\AssumeRoleRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
require_once __DIR__ . 'vendor/autoload.php';
class oss
{
public function qiniuPolicy()
{
$domain = ''; // 访问oss文件的域名
$bucket = ''; // 空间名称
$accessKey = '';
$secretKey = '';
$endpoint = ''; // 上传文件的地址,例如:https://up-z2.qiniup.com
$prefix = ''; // 指定bucket目录前缀
$dir = $prefix . '/' . date('Ymd') . '/'; // 按日期上传到指定目录
$expire = time() + 3600;
$policyArr = [
'scope' => $bucket,
'deadline' => $expire,
'fsizeMin' => 1,
'fsizeLimit' => 10 * 1024 * 1024,
];
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket, null, 3600, $policyArr);
if (empty($token)) {
return [];
}
return [
'endpoint' => $endpoint,
'host' => $domain,
'accessId' => '',
'policy' => '',
'signature' => '',
'token' => $token,
'expire' => $expire,
'keyTime' => '',
'algorithm' => '',
'dir' => $dir,
];
}
public function aliPolicy()
{
// https://help.aliyun.com/zh/oss/use-cases/obtain-signature-information-from-the-server-and-upload-data-to-oss
$domain = ''; // 访问oss文件的域名
$bucket = ''; // 空间名称
$accessKey = '';
$secretKey = '';
$endpoint = ''; // 上传文件的地址,例如:https://{bucket名称}.oss-cn-shenzhen.aliyuncs.com
$prefix = ''; // 指定bucket目录前缀
$dir = $prefix . '/' . date('Ymd') . '/'; // 按日期上传到指定目录
// https://help.aliyun.com/zh/oss/developer-reference/postobject#section-d5z-1ww-wdb
$expire = time() + 3600;
$policyArr = [
'expiration' => date('Y-m-d\TH:i:s.000\Z', $expire),
'conditions' => [
['bucket' => $bucket],
['content-length-range', 1, 10 * 1024 * 1024],
]
];
$policy = base64_encode(json_encode($policyArr));
// https://help.aliyun.com/zh/oss/developer-reference/postobject#section-wny-mww-wdb
$signature = base64_encode(hash_hmac('sha1', $policy, $secretKey, true));
return [
'endpoint' => $endpoint,
'host' => $domain,
'accessId' => $accessKey,
'policy' => $policy,
'signature' => $signature,
'token' => '',
'expire' => $expire,
'keyTime' => '',
'algorithm' => '',
'dir' => $dir,
];
}
public function aliSts()
{
// https://help.aliyun.com/zh/oss/developer-reference/authorize-access-2
try {
// 填写步骤1创建的RAM用户AccessKey。
$config = new Config([
"accessKeyId" => "【填写】",
"accessKeySecret" => "【填写】"
]);
//
$config->endpoint = "【填写】"; // sts.cn-hangzhou.aliyuncs.com
$client = new Sts($config);
$assumeRoleRequest = new AssumeRoleRequest([
// roleArn填写步骤2获取的角色ARN,例如acs:ram::175708322470****:role/ramtest。
"roleArn" => "【填写】",
// roleSessionName用于自定义角色会话名称,用来区分不同的令牌,例如填写为sessiontest。
"roleSessionName" => "【填写】",
// durationSeconds用于设置临时访问凭证有效时间单位为秒,最小值为900,最大值以当前角色设定的最大会话时间为准。本示例指定有效时间为3000秒。
"durationSeconds" => 3000,
// policy填写自定义权限策略,用于进一步限制STS临时访问凭证的权限。如果不指定Policy,则返回的STS临时访问凭证默认拥有指定角色的所有权限。
// 临时访问凭证最后获得的权限是步骤4设置的角色权限和该Policy设置权限的交集。
// "policy" => ""
]);
$runtime = new RuntimeOptions([]);
$result = $client->assumeRoleWithOptions($assumeRoleRequest, $runtime);
//printf("AccessKeyId:" . $result->body->credentials->accessKeyId. PHP_EOL);
//printf("AccessKeySecret:".$result->body->credentials->accessKeySecret.PHP_EOL);
//printf("Expiration:".$result->body->credentials->expiration.PHP_EOL);
//printf("SecurityToken:".$result->body->credentials->securityToken.PHP_EOL);
}catch (Exception $e){
// printf($e->getMessage() . PHP_EOL);
return [];
}
return $result;
}
public function qcloudPolicy()
{
// https://cloud.tencent.com/document/product/436/14690
$domain = ''; // 访问oss文件的域名
$bucket = ''; // 空间名称
$accessKey = '';
$secretKey = '';
$endpoint = ''; // 上传文件的地址,例如:https://{bucket名称}.cos.ap-guangzhou.myqcloud.com
$prefix = ''; // 指定bucket目录前缀
$dir = $prefix . '/' . date('Ymd') . '/'; // 按日期上传到指定目录
$algorithm = 'sha1';
$startTime = time();
$endTime = time() + 3600;
$expiration = date('Y-m-d\TH:i:s.000\Z', $endTime);
$keyTime = implode(';', [$startTime, $endTime]);
$policyArr = [
'expiration' => $expiration,
'conditions' => [
['acl' => 'default'],
['bucket' => $bucket],
['q-sign-algorithm' => $algorithm],
['q-ak' => $secretId],
['q-sign-time' => $keyTime]
]
];
$policy = base64_encode(json_encode($policyArr));
$signKey = hash_hmac($algorithm, $keyTime, $secretKey);
$stringToSign = sha1(json_encode($policyArr));
$signature = hash_hmac($algorithm, $stringToSign, $signKey);
return [
'endpoint' => $endpoint,
'host' => $domain,
'accessId' => $secretId,
'policy' => $policy,
'signature' => $signature,
'token' => '',
'expire' => $endTime,
'keyTime' => $keyTime,
'algorithm' => $algorithm,
'dir' => $dir,
];
}
public function qcloudSts()
{
// https://cloud.tencent.com/document/product/436/14048
// https://github.com/tencentyun/qcloud-cos-sts-sdk/blob/master/php/demo/sts_test.php
$domain = config('oss.qcloud_domain');
$bucket = config('oss.qcloud_bucket');
$secretId = config('oss.qcloud_access_key');
$secretKey = config('oss.qcloud_secret_key');
$endpoint = config('oss.qcloud_endpoint');
$prefix = config('oss.qcloud_bucket_key_prefix');
$dir = $prefix . '/' . date('Ymd') . '/';
$region = 'ap-guangzhou';
$sts = new \QCloud\COSSTS\Sts();
$config = array(
'url' => 'https://sts.tencentcloudapi.com/', // url和domain保持一致
'domain' => 'sts.tencentcloudapi.com', // 域名,非必须,默认为 sts.tencentcloudapi.com
'proxy' => '',
'secretId' => $secretId, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
'secretKey' => $secretKey, // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
'bucket' => $bucket, // 换成你的 bucket
'region' => $region, // 换成 bucket 所在园区
'durationSeconds' => 3600, // 密钥有效期
'allowPrefix' => ['*'], // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
// 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
'allowActions' => array(
// 简单上传
'name/cos:PutObject',
'name/cos:PostObject',
// 分片上传
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload'
),
// 临时密钥生效条件,关于condition的详细设置规则和COS支持的condition类型可以参考 https://cloud.tencent.com/document/product/436/71306
'condition' => []
);
// 获取临时密钥,计算签名
$tempKeys = $sts->getTempKeys($config);
return $tempKeys ?: [];
/**
数据如下:
{
"expiredTime": 1691169303,
"expiration": "2023-08-04T17:15:03Z",
"credentials": {
"sessionToken": "",
"tmpSecretId": "",
"tmpSecretKey": ""
},
"requestId": "6b274db5-a86b-4e27-a0e9-50f8ae1832f4",
"startTime": 1691165703
}
*/
}
}
表单提交到七牛云
七牛云oss upload
表单提交到阿里云
阿里云oss upload
表单提交到阿里云(sts)
阿里云oss upload
表单提交到腾讯云
腾讯云oss upload
表单提交到腾讯云(sts)
腾讯云oss upload