Skip to content

Commit c94119e

Browse files
committed
add rs_utils.php
1 parent bf2f457 commit c94119e

File tree

6 files changed

+87
-10
lines changed

6 files changed

+87
-10
lines changed

qiniu/http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ public function RoundTrip($req) // => ($resp, $error)
156156

157157
class Qiniu_MacHttpClient
158158
{
159-
private $mac;
159+
public $Mac;
160160

161161
public function __construct($mac)
162162
{
163-
$this->mac = Qiniu_RequireMac($mac);
163+
$this->Mac = Qiniu_RequireMac($mac);
164164
}
165165

166166
public function RoundTrip($req) // => ($resp, $error)
167167
{
168168
$incbody = Qiniu_Client_incBody($req);
169-
$token = $this->mac->SignRequest($req, $incbody);
169+
$token = $this->Mac->SignRequest($req, $incbody);
170170
$req->Header['Authorization'] = "QBox $token";
171171
return Qiniu_Client_do($req);
172172
}

qiniu/rs_utils.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once("rs.php");
4+
require_once("io.php");
5+
6+
function Qiniu_RS_Put($self, $bucket, $key, $body, $putExtra) // => ($data, $err)
7+
{
8+
$putPolicy = new Qiniu_RS_PutPolicy("$bucket:$key");
9+
$upToken = $putPolicy->Token($self->Mac);
10+
return Qiniu_Put($upToken, $key, $body, $putExtra);
11+
}
12+
13+
function Qiniu_RS_PutFile($self, $bucket, $key, $localFile, $putExtra) // => ($data, $err)
14+
{
15+
$putPolicy = new Qiniu_RS_PutPolicy("$bucket:$key");
16+
$upToken = $putPolicy->Token($self->Mac);
17+
return Qiniu_PutFile($upToken, $key, $localFile, $putExtra);
18+
}
19+

tests/IoTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function setUp()
1515

1616
public function testPutFile()
1717
{
18-
$key = 'testPutFile';
18+
$key = 'testPutFile' . $tid;
1919
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
2020

2121
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
@@ -37,7 +37,7 @@ public function testPutFile()
3737

3838
public function testPut()
3939
{
40-
$key = 'testPut';
40+
$key = 'testPut' . $tid;
4141
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
4242

4343
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);

tests/RsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class RsTest extends PHPUnit_Framework_TestCase
77
public $client;
88
public $bucket;
99
public $key;
10-
public $notExistKey = "not_exist";
10+
public $notExistKey = 'not_exist';
1111

1212
public function setUp()
1313
{
@@ -28,8 +28,8 @@ public function testStat()
2828

2929
public function testDeleteMoveCopy()
3030
{
31-
$key2 = rand();
32-
$key3 = rand();
31+
$key2 = 'testOp2' . $tid;
32+
$key3 = 'testOp3' . $tid;
3333
Qiniu_RS_Delete($this->client, $this->bucket, $key2);
3434
Qiniu_RS_Delete($this->client, $this->bucket, $key3);
3535

tests/RsUtilsTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
require("bootstrap.php");
4+
5+
class RsUtilsTest extends PHPUnit_Framework_TestCase
6+
{
7+
public $bucket;
8+
public $client;
9+
10+
public function setUp()
11+
{
12+
$this->client = new Qiniu_MacHttpClient(null);
13+
$this->bucket = getenv("QINIU_BUCKET_NAME");
14+
}
15+
16+
public function testPutFile()
17+
{
18+
$key = 'testPutFile' . $tid;
19+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
20+
21+
$putExtra = new Qiniu_PutExtra();
22+
$putExtra->CheckCrc = 1;
23+
list($ret, $err) = Qiniu_RS_PutFile($this->client, $this->bucket, $key, __file__, $putExtra);
24+
$this->assertNull($err);
25+
$this->assertArrayHasKey('hash', $ret);
26+
var_dump($ret);
27+
28+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
29+
$this->assertNull($err);
30+
var_dump($ret);
31+
32+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
33+
$this->assertNull($err);
34+
}
35+
36+
public function testPut()
37+
{
38+
$key = 'testPut' . $tid;
39+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
40+
41+
list($ret, $err) = Qiniu_RS_Put($this->client, $this->bucket, $key, "hello world!", null);
42+
$this->assertNull($err);
43+
$this->assertArrayHasKey('hash', $ret);
44+
var_dump($ret);
45+
46+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
47+
$this->assertNull($err);
48+
var_dump($ret);
49+
50+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
51+
$this->assertNull($err);
52+
}
53+
}
54+

tests/bootstrap.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
require_once("../qiniu/rs.php");
4-
require_once("../qiniu/io.php");
53
require_once("../qiniu/fop.php");
4+
require_once("../qiniu/rs_utils.php");
65

76
$accessKey = getenv("QINIU_ACCESS_KEY");
87
$secretKey = getenv("QINIU_SECRET_KEY");
@@ -11,3 +10,8 @@
1110
Qiniu_SetKeys($accessKey, $secretKey);
1211
}
1312

13+
$tid = getenv("TRAVIS_JOB_NUMBER");
14+
if !empty($tid) {
15+
$tid = strstr($tid, ".");
16+
}
17+

0 commit comments

Comments
 (0)