forked from NetEase-Object-Storage/nos-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignature.php
49 lines (42 loc) · 1.75 KB
/
Signature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
require_once __DIR__ . '/Common.php';
use NOS\Http\RequestCore;
use NOS\Http\ResponseCore;
use NOS\NosClient;
use NOS\Core\NosException;
$bucket = Common::getTestBucketName();
$nosClient = Common::getNosClient();
if (is_null($nosClient)) exit(1);
//------------------------------------------------------------基本使用----------------------------------------
$nosClient->uploadFile($bucket, "a.file", __FILE__);
$signedUrl = $nosClient->signUrl($bucket, "a.file", 3600);
Common::println($signedUrl);
//-----------------------------------------------------------完整用法-------------------------------------------
$nosClient->putObject($bucket, "test/test-signature-test-upload-and-download.txt", "nos php sample");
getSignedUrlForGettingObject($nosClient,$bucket);
function getSignedUrlForGettingObject($nosClient, $bucket)
{
$object = "test/test-signature-test-upload-and-download.txt";
$timeout = 3600;
try {
$signedUrl = $nosClient->signUrl($bucket, $object, $timeout);
} catch (NosException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
/**
* 可以类似的代码来访问签名的URL,也可以输入到浏览器中去访问
*/
$request = new RequestCore($signedUrl);
$request->set_method('GET');
$request->add_header('Content-Type', '');
$request->send_request();
$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
if ($res->isOK()) {
print(__FUNCTION__ . ": OK" . "\n");
} else {
print(__FUNCTION__ . ": FAILED" . "\n");
};
}