From 347a81c90a47005e974e52d4b2174c1597fdea38 Mon Sep 17 00:00:00 2001 From: babybaby <90250014+summerboy2134@users.noreply.github.com> Date: Mon, 10 Nov 2025 16:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=9F=9F=E5=90=8D=E5=8A=9F=E8=83=BD=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: liucheng.yao --- .gitignore | 2 ++ README.md | 4 ++++ v1/ucloud/conf.php | 6 +++++- v1/ucloud/proxy.php | 37 ++++++++++++------------------------- v1/ucloud/utils.php | 27 +++++++++++++++++++++------ 5 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af56f61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.idea/ diff --git a/README.md b/README.md index dae5663..0d159a8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/v1/ucloud/conf.php b/v1/ucloud/conf.php index d59349c..e065b3f 100644 --- a/v1/ucloud/conf.php +++ b/v1/ucloud/conf.php @@ -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'; diff --git a/v1/ucloud/proxy.php b/v1/ucloud/proxy.php index 8d90917..35f3966 100644 --- a/v1/ucloud/proxy.php +++ b/v1/ucloud/proxy.php @@ -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 diff --git a/v1/ucloud/utils.php b/v1/ucloud/utils.php index 0b7f050..2881943 100644 --- a/v1/ucloud/utils.php +++ b/v1/ucloud/utils.php @@ -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,17 +144,17 @@ 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"); + return new UCloud_Error(400, -1, "invalid public key found in config"); } else if ($UCLOUD_PRIVATE_KEY == "" || strstr($UCLOUD_PRIVATE_KEY, " ") != FALSE) { - return new UCloud_Error(400, -1, "invalid private key found in config"); + return new UCloud_Error(400, -1, "invalid private key found in config"); } 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: