Skip to content

Commit 0f2d8e3

Browse files
committed
add invokeOperation sample
1 parent 04e9c52 commit 0f2d8e3

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

sample/InvokeOperation.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use AlibabaCloud\Oss\V2 as Oss;
6+
7+
// parse args
8+
$optsdesc = [
9+
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True],
10+
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
11+
"bucket" => ['help' => 'The name of the bucket', 'required' => True],
12+
];
13+
$longopts = \array_map(function ($key) {
14+
return "$key:";
15+
}, array_keys($optsdesc));
16+
$options = getopt("", $longopts);
17+
foreach ($optsdesc as $key => $value) {
18+
if ($value['required'] === True && empty($options[$key])) {
19+
$help = $value['help'];
20+
echo "Error: the following arguments are required: --$key, $help";
21+
exit(1);
22+
}
23+
}
24+
25+
$region = $options["region"];
26+
$bucket = $options["bucket"];
27+
28+
// Loading credentials values from the environment variables
29+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
30+
31+
// Using the SDK's default configuration
32+
$cfg = Oss\Config::loadDefault();
33+
$cfg->setCredentialsProvider($credentialsProvider);
34+
$cfg->setRegion($region);
35+
if (isset($options["endpoint"])) {
36+
$cfg->setEndpoint($options["endpoint"]);
37+
}
38+
39+
$client = new Oss\Client($cfg);
40+
// PutCname sample case 1:
41+
$body = Oss\Utils::streamFor("<BucketCnameConfiguration>
42+
<Cname>
43+
<Domain>example.com</Domain>
44+
</Cname>
45+
</BucketCnameConfiguration>");
46+
47+
$input = new Oss\OperationInput(
48+
"PutCname",
49+
"POST",
50+
null,
51+
["cname" => "", "comp" => "add"],
52+
$body,
53+
$bucket
54+
);
55+
$result = $client->invokeOperation($input);
56+
printf(
57+
'status code:' . $result->GetStatusCode() . PHP_EOL .
58+
'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL
59+
);
60+
61+
// PutCname sample case 2:
62+
//$body = Oss\Utils::streamFor("<BucketCnameConfiguration>
63+
// <Cname>
64+
// <Domain>example.com</Domain>
65+
// </Cname>
66+
//</BucketCnameConfiguration>");
67+
//
68+
//
69+
//$input = new Oss\OperationInput(
70+
// opName: "PutCname",
71+
// method: "POST",
72+
// headers: null,
73+
// parameters: ["cname" => "", "comp" => "add"],
74+
// body: $body,
75+
// bucket: $bucket
76+
//);
77+
//$result = $client->invokeOperation($input);
78+
//printf(
79+
// 'status code:' . $result->GetStatusCode() . PHP_EOL .
80+
// 'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL
81+
//);
82+
83+
// GetCnameToken sample
84+
$input = new Oss\OperationInput(
85+
"GetCnameToken",
86+
"GET",
87+
null,
88+
["cname" => "example.com", "comp" => "token"],
89+
NULL,
90+
$bucket
91+
);
92+
$result = $client->invokeOperation($input);
93+
printf(
94+
'status code:' . $result->GetStatusCode() . PHP_EOL .
95+
'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL .
96+
'body:' . $result->getBody()->getContents()
97+
);

0 commit comments

Comments
 (0)