mirror of
https://github.com/ufilesdk-dev/ufile-phpsdk.git
synced 2025-04-06 04:00:05 +08:00
Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ab79c6b1ae | ||
|
793a4fef43 | ||
|
332770e79f | ||
|
8c125d0213 | ||
|
4779049fae | ||
|
8498d2f80b | ||
|
fb8cda063c | ||
|
418725fb99 | ||
|
9af6f6af5d | ||
|
a5ef7e0ccc | ||
|
c8d6006822 | ||
|
1daed4cf76 | ||
|
0e4135d715 | ||
|
0c2a88d040 | ||
|
12af6b3e3b | ||
|
2af55e185a | ||
|
14093f24bf | ||
|
d6ffe9118e | ||
|
88e302dd72 | ||
|
18317d89cd | ||
|
4f0837d83e | ||
|
7d21ebb8ec | ||
|
738a9ac389 |
201
LICENSE
Normal file
201
LICENSE
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright UCloud
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
29
README.md
Normal file
29
README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# UCloud 对象存储官方 Php 语言 SDK
|
||||
|
||||
[](LICENSE)
|
||||
|
||||
## US3 (原名UFile) 对象存储基本概念
|
||||
在对象存储系统中,存储空间(Bucket)是文件(File)的组织管理单位,文件(File)是存储空间的逻辑存储单元。对于每个账号,该账号里存放的每个文件都有唯一的一对存储空间(Bucket)与键(Key)作为标识。我们可以把 Bucket 理解成一类文件的集合,Key 理解成文件名。由于每个 Bucket 需要配置和权限不同,每个账户里面会有多个 Bucket。在 US3 里面,Bucket 主要分为公有和私有两种,公有 Bucket 里面的文件可以对任何人开放,私有 Bucket 需要配置对应访问签名才能访问。
|
||||
使用本 SDK 你不需要考虑签名,包装 URL,处理 HTTP response code 等一系列非常繁琐的事情。
|
||||
|
||||
## 功能说明
|
||||
ucloud/conf.php 为配置文件,按需填写:
|
||||
|
||||
- $UCLOUD_PROXY_SUFFIX = '.cn-bj.ufileos.com';
|
||||
- $UCLOUD_PUBLIC_KEY = 'paste your public key here';
|
||||
- $UCLOUD_PRIVATE_KEY = 'paste your private key here';
|
||||
|
||||
|
||||
Demo 目录中,包含各个接口的使用例子:
|
||||
- append.php
|
||||
- delete.php
|
||||
- get.php
|
||||
- multipart.php
|
||||
- mupload.php
|
||||
- put.php
|
||||
- listobjects.php
|
||||
- uploadhit.php
|
||||
|
||||
|
||||
## 许可证
|
||||
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html)
|
53
v1/del_bom_header.php
Normal file
53
v1/del_bom_header.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
if (isset($_GET['dir'])) { //设置文件目录
|
||||
$basedir = $_GET['dir'];
|
||||
}else{
|
||||
$basedir = '.';
|
||||
}
|
||||
$auto = true; //定义是否去掉文件中的BOM头,如果为 false 则只检测是否含有 BOM 头
|
||||
checkdir($basedir);//检测目录
|
||||
function checkdir($basedir){
|
||||
if ($dh = opendir($basedir)) {
|
||||
while (($file = readdir($dh)) !== false) {
|
||||
if($file{0} == '.'){
|
||||
continue;
|
||||
}
|
||||
if($file != '.' && $file != '..'){
|
||||
if (!is_dir($basedir."/".$file)) {
|
||||
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
|
||||
}else{
|
||||
$dirname = $basedir."/".$file;
|
||||
checkdir($dirname);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dh);
|
||||
}
|
||||
}
|
||||
//检查文件是否有BOM头,通过 全局变量 $auto 来控制是否删除文件中的BOM头
|
||||
function checkBOM ($filename) {
|
||||
global $auto;
|
||||
$contents = file_get_contents($filename);
|
||||
$charset[1] = substr($contents, 0, 1);
|
||||
$charset[2] = substr($contents, 1, 1);
|
||||
$charset[3] = substr($contents, 2, 1);
|
||||
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
|
||||
if ($auto) {
|
||||
$rest = substr($contents, 3);
|
||||
rewrite ($filename, $rest);
|
||||
return ("<font color=red>BOM found, automatically removed.</font>");
|
||||
} else {
|
||||
return ("<font color=red>BOM found.</font>");
|
||||
}
|
||||
}else{
|
||||
return ("BOM Not Found.");
|
||||
}
|
||||
}
|
||||
//重写文件,以达到删除BOM头的目的
|
||||
function rewrite ($filename, $data) {
|
||||
$filenum = fopen($filename, "w");
|
||||
flock($filenum, LOCK_EX);
|
||||
fwrite($filenum, $data);
|
||||
fclose($filenum);
|
||||
}
|
||||
?>
|
21
v1/demo/head.php
Normal file
21
v1/demo/head.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
require_once("../ucloud/proxy.php");
|
||||
|
||||
//存储空间名
|
||||
$bucket = "your bucket";
|
||||
//上传至存储空间后的文件名称(请不要和API公私钥混淆)
|
||||
$key = "your key";
|
||||
|
||||
|
||||
list($header, $data, $err) = UCloud_Head($bucket, $key);
|
||||
if ($err) {
|
||||
echo "error: " . $err->ErrMsg . "\n";
|
||||
echo "code: " . $err->Code . "\n";
|
||||
exit;
|
||||
}else{
|
||||
echo "code: " . 200 ."\n";
|
||||
}
|
||||
|
||||
print_r($header);
|
||||
echo "head $bucket/$key success\n";
|
26
v1/demo/listobjects.php
Normal file
26
v1/demo/listobjects.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
require_once("../ucloud/proxy.php");
|
||||
|
||||
//存储空间名
|
||||
$bucket = "your bucket";
|
||||
|
||||
//需要拉列表的目录前缀
|
||||
$prefix = "path prefix";
|
||||
|
||||
//拉列表一般分页拉取,每次拉取会返回一个marker,用来作为下一次拉取的marker,实现翻页拉取,初始为""
|
||||
//如果拉取到结尾:"IsTruncated":false,"NextMarker":""
|
||||
$marker = "";
|
||||
|
||||
#默认分隔符,目前支持/ 和空字符;如果是空表示不区分目录递归返回列表;
|
||||
$delimiter ="/";
|
||||
|
||||
//该接口拉取一个目录前缀下的目录、文件列表
|
||||
list($data, $err) = UCloud_ListObjects($bucket, $prefix, $marker, 100, $delimiter);
|
||||
if ($err) {
|
||||
echo "error: " . $err->ErrMsg . "\n";
|
||||
echo "code: " . $err->Code . "\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode($data, 128);
|
@ -5,7 +5,7 @@ global $UCLOUD_PROXY_SUFFIX;
|
||||
global $UCLOUD_PUBLIC_KEY;
|
||||
global $UCLOUD_PRIVATE_KEY;
|
||||
|
||||
$SDK_VER = "1.0.8";
|
||||
$SDK_VER = "1.0.9";
|
||||
|
||||
//空间域名后缀,请查看控制台上空间域名再配置此处
|
||||
//https://docs.ucloud.cn/storage_cdn/ufile/tools/introduction
|
||||
|
@ -4,8 +4,8 @@ require_once("conf.php");
|
||||
require_once("utils.php");
|
||||
require_once("digest.php");
|
||||
|
||||
$CURL_TIMEOUT=30;
|
||||
// --------------------------------------------------------------------------------
|
||||
|
||||
class HTTP_Request
|
||||
{
|
||||
public $URL;
|
||||
@ -36,7 +36,7 @@ class HTTP_Request
|
||||
global $UFILE_ACTION_TYPE;
|
||||
if ($CURL_TIMEOUT == null && $action_type !== ActionType::PUTFILE
|
||||
&& $action_type !== ActionType::POSTFILE) {
|
||||
$CURL_TIMEOUT = 10;
|
||||
$CURL_TIMEOUT = 20;
|
||||
}
|
||||
$this->Timeout = $CURL_TIMEOUT;
|
||||
}
|
||||
@ -70,7 +70,7 @@ class HTTP_Response
|
||||
|
||||
global $CURL_TIMEOUT;
|
||||
if ($CURL_TIMEOUT == null) {
|
||||
$CURL_TIMEOUT = 10;
|
||||
$CURL_TIMEOUT = 20;
|
||||
}
|
||||
$this->Timeout = $CURL_TIMEOUT;
|
||||
}
|
||||
@ -129,13 +129,16 @@ function UCloud_Client_Do($req)
|
||||
CURLOPT_TIMEOUT => $req->Timeout,
|
||||
CURLOPT_CONNECTTIMEOUT => $req->Timeout
|
||||
);
|
||||
|
||||
if($req->EncodedQuery() !== ""){
|
||||
if($req->METHOD =="HEAD"){
|
||||
$options[CURLOPT_NOBODY]=true;
|
||||
}
|
||||
if($req->EncodedQuery() !== ""){
|
||||
$options[CURLOPT_URL] = $url['host'] . "/" . $url['path'] . "?" . $req->EncodedQuery();
|
||||
}else{
|
||||
$options[CURLOPT_URL] = $url['host'] . "/" . $url['path'];
|
||||
}
|
||||
|
||||
|
||||
$httpHeader = $req->Header;
|
||||
if (!empty($httpHeader))
|
||||
{
|
||||
@ -151,7 +154,9 @@ function UCloud_Client_Do($req)
|
||||
} else {
|
||||
$options[CURLOPT_POSTFIELDS] = "";
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $options);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
$ret = curl_errno($ch);
|
||||
if ($ret !== 0) {
|
||||
@ -159,6 +164,7 @@ function UCloud_Client_Do($req)
|
||||
curl_close($ch);
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||
curl_close($ch);
|
||||
@ -253,7 +259,12 @@ function UCloud_Client_Ret($resp)
|
||||
}
|
||||
|
||||
$etag = UCloud_Header_Get($resp->Header, 'ETag');
|
||||
if ($etag != '') $data['ETag'] = $etag;
|
||||
if ($etag != ''){
|
||||
$data['ETag'] = $etag;
|
||||
}else{
|
||||
$data['ETag'] = UCloud_Header_Get($resp->Header, 'Etag');
|
||||
}
|
||||
|
||||
if (floor($code/100) == 2) {
|
||||
return array($data, null);
|
||||
}
|
||||
@ -270,6 +281,33 @@ function UCloud_Client_Call($self, $req, $type = HEAD_FIELD_CHECK)
|
||||
return UCloud_Client_Ret($resp);
|
||||
}
|
||||
|
||||
//@results: ($header,$data, $error)
|
||||
function UCloud_Client_Call_ReHeader($self, $req, $type = HEAD_FIELD_CHECK)
|
||||
{
|
||||
list($resp, $err) = $self->RoundTrip($req, $type);
|
||||
if ($err !== null) {
|
||||
return array(null, null, $err);
|
||||
}
|
||||
|
||||
$code = $resp->StatusCode;
|
||||
$data = null;
|
||||
if ($code >= 200 && $code <= 299) {
|
||||
if ($resp->ContentLength !== 0 && UCloud_Header_Get($resp->Header, 'Content-Type') == 'application/json') {
|
||||
$data = json_decode($resp->Body, true);
|
||||
if ($data === null) {
|
||||
$err = new UCloud_Error($code, 0, "");
|
||||
return array($resp->Header, null, $err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (floor($code/100) == 2) {
|
||||
return array($resp->Header, $data, null);
|
||||
}
|
||||
|
||||
return array($resp->Header, $data, UCloud_ResponseError($resp));
|
||||
}
|
||||
|
||||
//@results: $error
|
||||
function UCloud_Client_CallNoRet($self, $req, $type = HEAD_FIELD_CHECK)
|
||||
{
|
||||
|
@ -260,6 +260,7 @@ $mimetype_complete_map = array(
|
||||
".ppm" => "image/x-portable-pixmap",
|
||||
".pps" => "application/mspowerpoint",
|
||||
".ppt" => "application/mspowerpoint",
|
||||
".pptx" => "application/mspowerpoint",
|
||||
".ppz" => "application/mspowerpoint",
|
||||
".pre" => "application/x-freelance",
|
||||
".prt" => "application/pro_eng",
|
||||
@ -337,6 +338,7 @@ $mimetype_complete_map = array(
|
||||
".svf" => "image/vnd.dwg",
|
||||
".svr" => "application/x-world",
|
||||
".swf" => "application/x-shockwave-flash",
|
||||
".svg" => "image/svg+xml",
|
||||
".t" => "application/x-troff",
|
||||
".talk" => "text/x-speech",
|
||||
".tar" => "application/x-tar",
|
||||
@ -438,7 +440,7 @@ $mimetype_complete_map = array(
|
||||
".zoo" => "application/octet-stream",
|
||||
".zsh" => "text/x-script.zsh",
|
||||
".apk" => "application/vnd.android.package-archive",
|
||||
".ipa" => "application/vnd.android.package-archive",
|
||||
".ipa" => "application/octet-stream.ipa",
|
||||
".flv" => "video/x-flv",
|
||||
".mp4" => "video/mp4",
|
||||
".m3u8" => "application/x-mpegURL",
|
||||
|
@ -1,341 +1,384 @@
|
||||
<?php
|
||||
|
||||
require_once("conf.php");
|
||||
require_once("http.php");
|
||||
require_once("utils.php");
|
||||
require_once("digest.php");
|
||||
|
||||
|
||||
//------------------------------普通上传------------------------------
|
||||
function UCloud_PutFile($bucket, $key, $file)
|
||||
{
|
||||
$action_type = ActionType::PUTFILE;
|
||||
$err = CheckConfig(ActionType::PUTFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$path = $key;
|
||||
$content = @fread($f, filesize($file));
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, $mimetype);
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------表单上传------------------------------
|
||||
function UCloud_MultipartForm($bucket, $key, $file)
|
||||
{
|
||||
$action_type = ActionType::POSTFILE;
|
||||
$err = CheckConfig(ActionType::POSTFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$path = "";
|
||||
$fsize = filesize($file);
|
||||
$content = "";
|
||||
if ($fsize != 0) {
|
||||
$content = @fread($f, filesize($file));
|
||||
if ($content == FALSE) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
}
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$token = UCloud_SignRequest(null, $req, $mimetype);
|
||||
|
||||
$fields = array('Authorization'=>$token, 'FileName' => $key);
|
||||
$files = array('files'=>array('file', $file, $content, $mimetype));
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
|
||||
list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------分片上传------------------------------
|
||||
function UCloud_MInit($bucket, $key)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MINIT);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
"uploads" => ""
|
||||
);
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//@results: (tagList, err)
|
||||
function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MUPLOAD);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
for(;;) {
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
if (@fseek($f, $blkSize*$partNumber, SEEK_SET) < 0) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "fseek error"));
|
||||
}
|
||||
$content = @fread($f, $blkSize);
|
||||
if ($content == FALSE) {
|
||||
if (feof($f)) break;
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
|
||||
$querys = array(
|
||||
"uploadId" => $uploadId,
|
||||
"partNumber" => $partNumber
|
||||
);
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $content, $bucket, $key);
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
$req->Header['Expect'] = '';
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array(null, $err);
|
||||
}
|
||||
$etag = @$data['ETag'];
|
||||
$part = @$data['PartNumber'];
|
||||
if ($part != $partNumber) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
|
||||
}
|
||||
$etagList[] = $etag;
|
||||
$partNumber += 1;
|
||||
}
|
||||
fclose($f);
|
||||
return array($etagList, null);
|
||||
}
|
||||
|
||||
function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MFINISH);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
'uploadId' => $uploadId,
|
||||
'newKey' => $newKey,
|
||||
);
|
||||
|
||||
$body = @implode(',', $etagList);
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $body, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'text/plain';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
function UCloud_MCancel($bucket, $key, $uploadId)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MCANCEL);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
'uploadId' => $uploadId
|
||||
);
|
||||
|
||||
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------秒传------------------------------
|
||||
function UCloud_UploadHit($bucket, $key, $file)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::UPLOADHIT);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$f = @fopen($file, "r");
|
||||
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
|
||||
|
||||
$content = "";
|
||||
$fileSize = filesize($file);
|
||||
if ($fileSize != 0) {
|
||||
$content = @fread($f, $fileSize);
|
||||
if ($content == FALSE) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
}
|
||||
list($fileHash, $err) = UCloud_FileHash($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array(null, $err);
|
||||
}
|
||||
fclose($f);
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = "uploadhit";
|
||||
$querys = array(
|
||||
'Hash' => $fileHash,
|
||||
'FileName' => $key,
|
||||
'FileSize' => $fileSize
|
||||
);
|
||||
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------删除文件------------------------------
|
||||
function UCloud_Delete($bucket, $key)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::DELETE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = "$key";
|
||||
|
||||
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------追加上传------------------------------
|
||||
function UCloud_AppendFile($bucket, $key, $file, $position)
|
||||
{
|
||||
$action_type = ActionType::APPENDFILE;
|
||||
$err = CheckConfig(ActionType::APPENDFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$key = $key . "?append&position=" . $position;
|
||||
$path = $key;
|
||||
$content = @fread($f, filesize($file));
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, $mimetype);
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------生成公有文件Url------------------------------
|
||||
// @results: $url
|
||||
function UCloud_MakePublicUrl($bucket, $key)
|
||||
{
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
|
||||
}
|
||||
//------------------------------生成私有文件Url------------------------------
|
||||
// @results: $url
|
||||
function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::GETFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PUBLIC_KEY;
|
||||
|
||||
$public_url = UCloud_MakePublicUrl($bucket, $key);
|
||||
$req = new HTTP_Request('GET', array('path'=>$public_url), null, $bucket, $key);
|
||||
if ($expires > 0) {
|
||||
$req->Header['Expires'] = $expires;
|
||||
}
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
$temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
|
||||
$signature = substr($temp, -28, 28);
|
||||
$url = $public_url . "?UCloudPublicKey=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
|
||||
if ('' != $expires) {
|
||||
$url .= "&Expires=" . rawurlencode($expires);
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
<?php
|
||||
|
||||
require_once("conf.php");
|
||||
require_once("http.php");
|
||||
require_once("utils.php");
|
||||
require_once("digest.php");
|
||||
|
||||
|
||||
//------------------------------普通上传------------------------------
|
||||
function UCloud_PutFile($bucket, $key, $file)
|
||||
{
|
||||
$action_type = ActionType::PUTFILE;
|
||||
$err = CheckConfig(ActionType::PUTFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$path = $key;
|
||||
$content = @fread($f, filesize($file));
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, $mimetype);
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------表单上传------------------------------
|
||||
function UCloud_MultipartForm($bucket, $key, $file)
|
||||
{
|
||||
$action_type = ActionType::POSTFILE;
|
||||
$err = CheckConfig(ActionType::POSTFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$path = "";
|
||||
$fsize = filesize($file);
|
||||
$content = "";
|
||||
if ($fsize != 0) {
|
||||
$content = @fread($f, filesize($file));
|
||||
if ($content == FALSE) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
}
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$token = UCloud_SignRequest(null, $req, $mimetype);
|
||||
|
||||
$fields = array('Authorization'=>$token, 'FileName' => $key);
|
||||
$files = array('files'=>array('file', $file, $content, $mimetype));
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
|
||||
list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------分片上传------------------------------
|
||||
function UCloud_MInit($bucket, $key)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MINIT);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
"uploads" => ""
|
||||
);
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//@results: (tagList, err)
|
||||
function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MUPLOAD);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
for(;;) {
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
if (@fseek($f, $blkSize*$partNumber, SEEK_SET) < 0) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "fseek error"));
|
||||
}
|
||||
$content = @fread($f, $blkSize);
|
||||
if ($content == FALSE) {
|
||||
if (feof($f)) break;
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
|
||||
$querys = array(
|
||||
"uploadId" => $uploadId,
|
||||
"partNumber" => $partNumber
|
||||
);
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $content, $bucket, $key);
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
$req->Header['Expect'] = '';
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array(null, $err);
|
||||
}
|
||||
$etag = @$data['ETag'];
|
||||
$part = @$data['PartNumber'];
|
||||
if ($part != $partNumber) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
|
||||
}
|
||||
$etagList[] = $etag;
|
||||
$partNumber += 1;
|
||||
}
|
||||
fclose($f);
|
||||
return array($etagList, null);
|
||||
}
|
||||
|
||||
function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MFINISH);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
'uploadId' => $uploadId,
|
||||
'newKey' => $newKey,
|
||||
);
|
||||
|
||||
$body = @implode(',', $etagList);
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $body, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'text/plain';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
function UCloud_MCancel($bucket, $key, $uploadId)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::MCANCEL);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = $key;
|
||||
$querys = array(
|
||||
'uploadId' => $uploadId
|
||||
);
|
||||
|
||||
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------秒传------------------------------
|
||||
function UCloud_UploadHit($bucket, $key, $file)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::UPLOADHIT);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$f = @fopen($file, "r");
|
||||
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
|
||||
|
||||
$content = "";
|
||||
$fileSize = filesize($file);
|
||||
if ($fileSize != 0) {
|
||||
$content = @fread($f, $fileSize);
|
||||
if ($content == FALSE) {
|
||||
fclose($f);
|
||||
return array(null, new UCloud_Error(0, -1, "read file error"));
|
||||
}
|
||||
}
|
||||
list($fileHash, $err) = UCloud_FileHash($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array(null, $err);
|
||||
}
|
||||
fclose($f);
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = "uploadhit";
|
||||
$querys = array(
|
||||
'Hash' => $fileHash,
|
||||
'FileName' => $key,
|
||||
'FileSize' => $fileSize
|
||||
);
|
||||
|
||||
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------删除文件------------------------------
|
||||
function UCloud_Delete($bucket, $key)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::DELETE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = "$key";
|
||||
|
||||
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call($client, $req);
|
||||
}
|
||||
|
||||
//------------------------------Head文件------------------------------
|
||||
function UCloud_Head($bucket, $key)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::HEAD);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$path = "$key";
|
||||
|
||||
$req = new HTTP_Request('HEAD', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
return UCloud_Client_Call_ReHeader($client, $req);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------追加上传------------------------------
|
||||
function UCloud_AppendFile($bucket, $key, $file, $position)
|
||||
{
|
||||
$action_type = ActionType::APPENDFILE;
|
||||
$err = CheckConfig(ActionType::APPENDFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
$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;
|
||||
$key = $key . "?append&position=" . $position;
|
||||
$path = $key;
|
||||
$content = @fread($f, filesize($file));
|
||||
list($mimetype, $err) = GetFileMimeType($file);
|
||||
if ($err) {
|
||||
fclose($f);
|
||||
return array("", $err);
|
||||
}
|
||||
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
|
||||
$req->Header['Expect'] = '';
|
||||
$req->Header['Content-Type'] = $mimetype;
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null, $mimetype);
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
fclose($f);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------列表目录------------------------------
|
||||
function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
|
||||
{
|
||||
$action_type = ActionType::LISTOBJECTS;
|
||||
$err = CheckConfig(ActionType::LISTOBJECTS);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
|
||||
$query = "listobjects&prefix=" . $path_prefix ."&marker=". $marker . "&max-keys=" . $count ."&delimiter=" .$delimiter;
|
||||
parse_str($query,$arr);
|
||||
$path = "?" . http_build_query($arr);
|
||||
|
||||
$req = new HTTP_Request('GET', array('host'=>$host, 'path'=>$path), null, $bucket, null, $action_type);
|
||||
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
list($data, $err) = UCloud_Client_Call($client, $req);
|
||||
return array($data, $err);
|
||||
}
|
||||
|
||||
//------------------------------生成公有文件Url------------------------------
|
||||
// @results: $url
|
||||
function UCloud_MakePublicUrl($bucket, $key)
|
||||
{
|
||||
global $UCLOUD_PROXY_SUFFIX;
|
||||
return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
|
||||
}
|
||||
//------------------------------生成私有文件Url------------------------------
|
||||
// @results: $url
|
||||
function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
|
||||
{
|
||||
|
||||
$err = CheckConfig(ActionType::GETFILE);
|
||||
if ($err != null) {
|
||||
return array(null, $err);
|
||||
}
|
||||
|
||||
global $UCLOUD_PUBLIC_KEY;
|
||||
|
||||
$public_url = UCloud_MakePublicUrl($bucket, $key);
|
||||
$req = new HTTP_Request('GET', array('path'=>$public_url), null, $bucket, $key);
|
||||
if ($expires > 0) {
|
||||
$req->Header['Expires'] = $expires;
|
||||
}
|
||||
|
||||
$client = new UCloud_AuthHttpClient(null);
|
||||
$temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
|
||||
$signature = substr($temp, -28, 28);
|
||||
$url = $public_url . "?UCloudPublicKey=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
|
||||
if ('' != $expires) {
|
||||
$url .= "&Expires=" . rawurlencode($expires);
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ abstract class ActionType
|
||||
const UPLOADHIT = 7;
|
||||
const GETFILE = 8;
|
||||
const APPENDFILE = 9;
|
||||
const LISTOBJECTS = 10;
|
||||
const HEAD = 11;
|
||||
}
|
||||
|
||||
class UCloud_Error
|
||||
@ -125,6 +127,8 @@ function CheckConfig($action) {
|
||||
case ActionType::MFINISH:
|
||||
case ActionType::DELETE:
|
||||
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");
|
||||
} else if ($UCLOUD_PUBLIC_KEY == "" || strstr($UCLOUD_PUBLIC_KEY, " ") != FALSE) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user