新增自定义域名功能 (#8)

Co-authored-by: liucheng.yao <liucheng.yao@ucloud.cn>
This commit is contained in:
babybaby 2025-11-10 16:01:18 +08:00 committed by GitHub
parent ab79c6b1ae
commit 347a81c90a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 32 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
.idea/

View File

@ -14,6 +14,10 @@ ucloud/conf.php 为配置文件,按需填写:
- $UCLOUD_PRIVATE_KEY = 'paste your private key here';
若需使用自定义域名,可额外设置:
- $UCLOUD_CUSTOM_DOMAIN = 'your-custom-domain.com'; // 可选,置空则继续使用 bucket + suffix
Demo 目录中,包含各个接口的使用例子:
- append.php
- delete.php

View File

@ -2,14 +2,18 @@
global $SDK_VER;
global $UCLOUD_PROXY_SUFFIX;
global $UCLOUD_CUSTOM_DOMAIN;
global $UCLOUD_PUBLIC_KEY;
global $UCLOUD_PRIVATE_KEY;
$SDK_VER = "1.0.9";
$SDK_VER = "1.1.0";
//空间域名后缀,请查看控制台上空间域名再配置此处
//https://docs.ucloud.cn/storage_cdn/ufile/tools/introduction
$UCLOUD_PROXY_SUFFIX = '.cn-bj.ufileos.com'; //如果是其他地域的,请参考上面的说明
//可选使用自定义域名your-custom-domain.com置空则继续使用 bucket + suffix
$UCLOUD_CUSTOM_DOMAIN = '';
$UCLOUD_PUBLIC_KEY = 'paste your public key here';
$UCLOUD_PRIVATE_KEY = 'paste your private key here';

View File

@ -18,8 +18,7 @@ function UCloud_PutFile($bucket, $key, $file)
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = $key;
$content = @fread($f, filesize($file));
list($mimetype, $err) = GetFileMimeType($file);
@ -49,8 +48,7 @@ function UCloud_MultipartForm($bucket, $key, $file)
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = "";
$fsize = filesize($file);
$content = "";
@ -89,8 +87,7 @@ function UCloud_MInit($bucket, $key)
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = $key;
$querys = array(
"uploads" => ""
@ -114,8 +111,6 @@ function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$etagList = array();
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
@ -124,7 +119,7 @@ function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0
}
$client = new UCloud_AuthHttpClient(null);
for(;;) {
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = $key;
if (@fseek($f, $blkSize*$partNumber, SEEK_SET) < 0) {
fclose($f);
@ -170,8 +165,7 @@ function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = $key;
$querys = array(
'uploadId' => $uploadId,
@ -194,8 +188,7 @@ function UCloud_MCancel($bucket, $key, $uploadId)
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = $key;
$querys = array(
'uploadId' => $uploadId
@ -236,8 +229,7 @@ function UCloud_UploadHit($bucket, $key, $file)
}
fclose($f);
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = "uploadhit";
$querys = array(
'Hash' => $fileHash,
@ -261,8 +253,7 @@ function UCloud_Delete($bucket, $key)
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = "$key";
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
@ -281,8 +272,7 @@ function UCloud_Head($bucket, $key)
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$path = "$key";
$req = new HTTP_Request('HEAD', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
@ -305,8 +295,7 @@ function UCloud_AppendFile($bucket, $key, $file, $position)
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$key = $key . "?append&position=" . $position;
$path = $key;
$content = @fread($f, filesize($file));
@ -334,8 +323,7 @@ function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$host = UCloud_GetHost($bucket);
$query = "listobjects&prefix=" . $path_prefix ."&marker=". $marker . "&max-keys=" . $count ."&delimiter=" .$delimiter;
parse_str($query,$arr);
$path = "?" . http_build_query($arr);
@ -352,8 +340,7 @@ function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
// @results: $url
function UCloud_MakePublicUrl($bucket, $key)
{
global $UCLOUD_PROXY_SUFFIX;
return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
return UCloud_GetHost($bucket) . "/" . rawurlencode($key);
}
//------------------------------生成私有文件Url------------------------------
// @results: $url

View File

@ -112,11 +112,26 @@ function GetFileMimeType($filename)
return array($mimetype, null);
}
function UCloud_GetHost($bucket) {
global $UCLOUD_CUSTOM_DOMAIN;
global $UCLOUD_PROXY_SUFFIX;
$customDomain = trim($UCLOUD_CUSTOM_DOMAIN);
if ($customDomain !== '') {
return $customDomain;
}
return $bucket . $UCLOUD_PROXY_SUFFIX;
}
function CheckConfig($action) {
global $UCLOUD_PUBLIC_KEY;
global $UCLOUD_PRIVATE_KEY;
global $UCLOUD_PROXY_SUFFIX;
global $UCLOUD_CUSTOM_DOMAIN;
$customDomain = trim($UCLOUD_CUSTOM_DOMAIN);
switch ($action) {
case ActionType::PUTFILE:
@ -129,8 +144,8 @@ function CheckConfig($action) {
case ActionType::UPLOADHIT:
case ActionType::LISTOBJECTS:
case ActionType::HEAD:
if ($UCLOUD_PROXY_SUFFIX == "") {
return new UCloud_Error(400, -1, "no proxy suffix found in config");
if ($customDomain === '' && $UCLOUD_PROXY_SUFFIX == "") {
return new UCloud_Error(400, -1, "no proxy suffix or custom domain found in config");
} else if ($UCLOUD_PUBLIC_KEY == "" || strstr($UCLOUD_PUBLIC_KEY, " ") != FALSE) {
return new UCloud_Error(400, -1, "invalid public key found in config");
} else if ($UCLOUD_PRIVATE_KEY == "" || strstr($UCLOUD_PRIVATE_KEY, " ") != FALSE) {
@ -138,8 +153,8 @@ function CheckConfig($action) {
}
break;
case ActionType::GETFILE:
if ($UCLOUD_PROXY_SUFFIX == "") {
return new UCloud_Error(400, -1, "no proxy suffix found in config");
if ($customDomain === '' && $UCLOUD_PROXY_SUFFIX == "") {
return new UCloud_Error(400, -1, "no proxy suffix or custom domain found in config");
}
break;
default: