Skip to content

Commit 3cc0a5c

Browse files
yangzong18huiguangjun
authored andcommitted
add invokeOperation sample
1 parent 04e9c52 commit 3cc0a5c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

sample/InvokeOperation.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
);
51+
$input->setParameter("cname", "");
52+
$input->setParameter("comp", "add");
53+
$input->setBody($body);
54+
$input->setBucket($bucket);
55+
56+
$result = $client->invokeOperation($input);
57+
printf(
58+
'status code:' . $result->GetStatusCode() . PHP_EOL .
59+
'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL
60+
);
61+
62+
// PutCname sample case 2:
63+
//$body = Oss\Utils::streamFor("<BucketCnameConfiguration>
64+
// <Cname>
65+
// <Domain>example.com</Domain>
66+
// </Cname>
67+
//</BucketCnameConfiguration>");
68+
//
69+
//
70+
//$input = new Oss\OperationInput(
71+
// opName: "PutCname",
72+
// method: "POST",
73+
// headers: null,
74+
// parameters: ["cname" => "", "comp" => "add"],
75+
// body: $body,
76+
// bucket: $bucket
77+
//);
78+
//$result = $client->invokeOperation($input);
79+
//printf(
80+
// 'status code:' . $result->GetStatusCode() . PHP_EOL .
81+
// 'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL
82+
//);
83+
84+
// GetCnameToken sample
85+
$input = new Oss\OperationInput(
86+
"GetCnameToken",
87+
"GET",
88+
null,
89+
["cname" => "example.com", "comp" => "token"],
90+
NULL,
91+
$bucket
92+
);
93+
$result = $client->invokeOperation($input);
94+
printf(
95+
'status code:' . $result->GetStatusCode() . PHP_EOL .
96+
'request id:' . $result->getHeaders()["x-oss-request-id"] . PHP_EOL .
97+
'body:' . $result->getBody()->getContents()
98+
);

0 commit comments

Comments
 (0)