|
|
<?php
|
|
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\ClientInterface;
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
|
|
/** Signed, read-only S3 compatibility check. */
|
|
|
final class CloudObjectStorageS3Client
|
|
|
{
|
|
|
public function __construct(private readonly ?ClientInterface $http = null)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param array<string, mixed> $config
|
|
|
* @param array{access_key_id:string,secret_access_key:string} $credentials
|
|
|
* @return array{status:string,http_status:int,region:string}
|
|
|
*/
|
|
|
public function headBucket(array $config, array $credentials): array
|
|
|
{
|
|
|
$url = self::bucketUrl(
|
|
|
(string) $config['endpoint'],
|
|
|
(string) $config['bucket'],
|
|
|
(bool) $config['path_style']
|
|
|
);
|
|
|
$headers = CloudS3Signer::sign(
|
|
|
'HEAD',
|
|
|
$url,
|
|
|
(string) $config['region'],
|
|
|
$credentials['access_key_id'],
|
|
|
$credentials['secret_access_key'],
|
|
|
time()
|
|
|
);
|
|
|
|
|
|
try {
|
|
|
$response = ($this->http ?? new Client())->request('HEAD', $url, [
|
|
|
'headers' => $headers,
|
|
|
'http_errors' => false,
|
|
|
'allow_redirects' => false,
|
|
|
'connect_timeout' => 5.0,
|
|
|
'timeout' => 15.0,
|
|
|
'verify' => true,
|
|
|
'proxy' => '',
|
|
|
]);
|
|
|
} catch (GuzzleException $exception) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'无法连接对象存储,请检查地址、证书、DNS 和防火墙。',
|
|
|
'endpoint_unreachable'
|
|
|
);
|
|
|
}
|
|
|
|
|
|
$status = $response->getStatusCode();
|
|
|
$responseRegion = trim($response->getHeaderLine('x-amz-bucket-region'));
|
|
|
if ($status >= 200 && $status < 300) {
|
|
|
return [
|
|
|
'status' => 'healthy',
|
|
|
'http_status' => $status,
|
|
|
'region' => $responseRegion,
|
|
|
];
|
|
|
}
|
|
|
if (in_array($status, [301, 307], true)
|
|
|
|| ($status === 400 && $responseRegion !== '')) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'Bucket 区域不匹配,请按对象存储返回的区域修改配置。',
|
|
|
'region_mismatch'
|
|
|
);
|
|
|
}
|
|
|
if (in_array($status, [401, 403], true)) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'凭据或 s3:ListBucket 权限不正确,请同时检查面板服务器时间。',
|
|
|
'auth_or_permission_denied'
|
|
|
);
|
|
|
}
|
|
|
if ($status === 404) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'指定的 Bucket 不存在,或当前地址无法访问该 Bucket。',
|
|
|
'bucket_not_found'
|
|
|
);
|
|
|
}
|
|
|
if ($status === 429) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'对象存储暂时限制请求,请稍后重试。',
|
|
|
'rate_limited'
|
|
|
);
|
|
|
}
|
|
|
if ($status >= 500) {
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'对象存储服务暂时不可用,请稍后重试。',
|
|
|
'service_unavailable'
|
|
|
);
|
|
|
}
|
|
|
throw new CloudObjectStorageTestError(
|
|
|
'对象存储拒绝了兼容性检查,请核对 Endpoint、Region 和寻址方式。',
|
|
|
'request_rejected'
|
|
|
);
|
|
|
}
|
|
|
|
|
|
private static function bucketUrl(
|
|
|
string $endpoint,
|
|
|
string $bucket,
|
|
|
bool $pathStyle
|
|
|
): string {
|
|
|
if ($pathStyle) {
|
|
|
return rtrim($endpoint, '/') . '/' . rawurlencode($bucket) . '/';
|
|
|
}
|
|
|
$parts = parse_url($endpoint);
|
|
|
if (!is_array($parts) || !isset($parts['scheme'], $parts['host'])) {
|
|
|
throw new \InvalidArgumentException('Invalid S3 endpoint');
|
|
|
}
|
|
|
$authority = $bucket . '.' . strtolower((string) $parts['host']);
|
|
|
if (isset($parts['port'])) {
|
|
|
$authority .= ':' . (int) $parts['port'];
|
|
|
}
|
|
|
return strtolower((string) $parts['scheme']) . '://' . $authority . '/';
|
|
|
}
|
|
|
}
|