|
1
|
|
|
<?php |
|
2
|
|
|
define("ENCRYPTPTION_STORAGE_MODE","InstructionFile"); |
|
3
|
|
|
require_once "../Ks3EncryptionClient.class.php"; |
|
4
|
|
|
require_once "../Ks3Client.class.php"; |
|
5
|
|
|
$bucket = "test-encryption"; |
|
6
|
|
|
$keyprefix = "php/"; |
|
7
|
|
|
|
|
8
|
|
|
$filename = "secret.key"; |
|
9
|
|
|
$handle = fopen($filename, "r"); |
|
10
|
|
|
$contents = fread($handle, filesize ($filename)); |
|
11
|
|
|
fclose($handle); |
|
12
|
|
|
|
|
13
|
|
|
$client = new Ks3EncryptionClient("lMQTr0hNlMpB0iOk/i+x","D4CsYLs75JcWEjbiI22zR3P7kJ/+5B1qdEje7A7I",$contents); |
|
14
|
|
|
putObjectByContentAndGetObjectUsingInstruction($client,$bucket,$keyprefix); |
|
15
|
|
|
putObjectByFileAndGetObjectUsingFile($client,$bucket,$keyprefix); |
|
16
|
|
|
multipartUpload($client,$bucket,$keyprefix); |
|
|
|
|
|
|
17
|
|
|
copyAnddeleteObject($client,$bucket,$keyprefix); |
|
18
|
|
|
|
|
19
|
|
|
function putObjectByContentAndGetObjectUsingInstruction($client,$bucket,$keyprefix){ |
|
20
|
|
|
$content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
21
|
|
|
$args = array( |
|
22
|
|
|
"Bucket"=>$bucket, |
|
23
|
|
|
"Key"=>$keyprefix."EOFile", |
|
24
|
|
|
"ACL"=>"public-read", |
|
25
|
|
|
"Content"=>$content |
|
26
|
|
|
); |
|
27
|
|
|
$client->putObjectByContent($args); |
|
28
|
|
|
rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
29
|
|
|
"D://testdown/down",base64_encode(md5($args["Content"]))); |
|
30
|
|
|
} |
|
31
|
|
|
function putObjectByFileAndGetObjectUsingFile($client,$bucket,$keyprefix){ |
|
32
|
|
|
|
|
33
|
|
|
$args = array( |
|
34
|
|
|
"Bucket"=>$bucket, |
|
35
|
|
|
"Key"=>$keyprefix."EOFile", |
|
36
|
|
|
"ACL"=>"public-read", |
|
37
|
|
|
"Content"=>array( |
|
38
|
|
|
"content"=>"D://IMG.jpg" |
|
39
|
|
|
) |
|
40
|
|
|
); |
|
41
|
|
|
$client->putObjectByFile($args); |
|
42
|
|
|
rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
43
|
|
|
"D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
44
|
|
|
} |
|
45
|
|
|
function multipartUpload($client,$bucket,$keyprefix){ |
|
46
|
|
|
//初始化分开上传,获取uploadid |
|
47
|
|
|
$args = array( |
|
48
|
|
|
"Bucket"=>$bucket, |
|
49
|
|
|
"Key"=>$keyprefix."EOFile", |
|
50
|
|
|
); |
|
51
|
|
|
$uploadid = $client->initMultipartUpload($args); |
|
52
|
|
|
$uploadid = $uploadid["UploadId"];//获取到uploadid |
|
53
|
|
|
//开始上传 |
|
54
|
|
|
|
|
55
|
|
|
$file = "D://IMG.jpg";//要上传的文件 |
|
56
|
|
|
$partsize = 1024*100; |
|
57
|
|
|
$resource = fopen($file,"r"); |
|
58
|
|
|
$stat = fstat($resource); |
|
59
|
|
|
$total = $stat["size"];//获取文件的总大小 |
|
60
|
|
|
fclose($resource); |
|
61
|
|
|
$count = (int)(($total-1)/$partsize)+1;//计算文件需要分几块上传 |
|
62
|
|
|
for($i = 0;$i < $count;$i++){ |
|
63
|
|
|
//依次上传每一块 |
|
64
|
|
|
echo "upload".$i."\r\n"; |
|
65
|
|
|
$args=array( |
|
66
|
|
|
"Bucket"=>$bucket, |
|
67
|
|
|
"Key"=>$keyprefix."EOFile", |
|
68
|
|
|
"LastPart"=>($i===$count-1), |
|
69
|
|
|
"Options"=>array( |
|
70
|
|
|
"partNumber"=>$i+1, |
|
71
|
|
|
"uploadId"=>$uploadid |
|
72
|
|
|
), |
|
73
|
|
|
"ObjectMeta"=>array( |
|
74
|
|
|
"Content-Length"=>min($partsize,$total-$partsize*$i)//每次上传$partsize大小 |
|
75
|
|
|
), |
|
76
|
|
|
"Content"=>array( |
|
77
|
|
|
"content"=>$file, |
|
78
|
|
|
"seek_position"=>$partsize*$i//跳过之前已经上传的 |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
$etag = $client->uploadPart($args); |
|
82
|
|
|
$etag = $etag["ETag"]; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
$parts = $client->listParts(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFile","Options"=>array("uploadId"=>$uploadid))); |
|
85
|
|
|
//结束上传 |
|
86
|
|
|
$args=array( |
|
87
|
|
|
"Bucket"=>$bucket, |
|
88
|
|
|
"Key"=>$keyprefix."EOFile", |
|
89
|
|
|
"Options"=>array("uploadId"=>$uploadid), |
|
90
|
|
|
"Parts"=>$parts["Parts"]//使用之前列出的块完成分开上传 |
|
91
|
|
|
); |
|
92
|
|
|
$result = $client->completeMultipartUpload($args); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
95
|
|
|
"D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
96
|
|
|
} |
|
97
|
|
|
function copyAnddeleteObject($client,$bucket,$keyprefix){ |
|
98
|
|
|
$content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
99
|
|
|
$args = array( |
|
100
|
|
|
"Bucket"=>$bucket, |
|
101
|
|
|
"Key"=>$keyprefix."EOFile", |
|
102
|
|
|
"ACL"=>"public-read", |
|
103
|
|
|
"Content"=>$content |
|
104
|
|
|
); |
|
105
|
|
|
$client->putObjectByContent($args); |
|
106
|
|
|
|
|
107
|
|
|
if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
108
|
|
|
$client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
109
|
|
|
if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
110
|
|
|
$client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction")); |
|
111
|
|
|
|
|
112
|
|
|
$copyReq = array( |
|
113
|
|
|
"Bucket"=>$bucket, |
|
114
|
|
|
"Key"=>$keyprefix."EOFileCopy", |
|
115
|
|
|
"CopySource"=>array( |
|
116
|
|
|
"Bucket"=>$bucket, |
|
117
|
|
|
"Key"=>$keyprefix."EOFile" |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
$client->copyObject($copyReq); |
|
121
|
|
|
|
|
122
|
|
|
if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
123
|
|
|
throw new Exception("not found ".$keyprefix."EOFileCopy"); |
|
124
|
|
|
if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
125
|
|
|
throw new Exception("not found ".$keyprefix."EOFileCopy.instruction"); |
|
126
|
|
|
|
|
127
|
|
|
$client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
128
|
|
|
|
|
129
|
|
|
if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
130
|
|
|
throw new Exception("found ".$keyprefix."EOFileCopy"); |
|
131
|
|
|
if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
132
|
|
|
throw new Exception("found ".$keyprefix."EOFileCopy.instruction"); |
|
133
|
|
|
} |
|
134
|
|
|
function rangeGetAndCheckMd5($client,$bucket,$key,$file,$expectedMd5){ |
|
135
|
|
|
$args = array("Bucket"=>$bucket,"Key"=>$key); |
|
136
|
|
|
$meta = $client->getObjectMeta($args); |
|
137
|
|
|
$contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
138
|
|
|
|
|
139
|
|
|
$filelist = array(); |
|
140
|
|
|
|
|
141
|
|
|
for($begin = 0;$begin <$contentlength;){ |
|
142
|
|
|
$index = rand((int)($contentlength/20),(int)($contentlength/10)); |
|
143
|
|
|
$range = array("start"=>$begin,"end"=>$begin+$index); |
|
144
|
|
|
$destFile = $file.$begin."-".($begin+$index); |
|
145
|
|
|
array_push($filelist,$destFile); |
|
146
|
|
|
$begin += ($index+1); |
|
147
|
|
|
$args = array( |
|
148
|
|
|
"Bucket"=>$bucket, |
|
149
|
|
|
"Key"=>$key, |
|
150
|
|
|
"Range"=>$range, |
|
151
|
|
|
"WriteTo"=>$destFile |
|
152
|
|
|
); |
|
153
|
|
|
$client->getObject($args); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
foreach ($filelist as $key => $value) { |
|
|
|
|
|
|
157
|
|
|
$handle = fopen($value,"r"); |
|
158
|
|
|
$size = filesize($value); |
|
159
|
|
|
if($size > 0){ |
|
160
|
|
|
$content = fread($handle,$size); |
|
161
|
|
|
file_put_contents($file,$content,FILE_APPEND); |
|
162
|
|
|
} |
|
163
|
|
|
fclose($handle); |
|
164
|
|
|
@unlink($value); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
$md5 = base64_encode(md5_file($file)); |
|
167
|
|
|
if($md5 != $expectedMd5) |
|
168
|
|
|
throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
169
|
|
|
@unlink($file); |
|
170
|
|
|
} |
|
171
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.