Skip to content

Commit b88feb9

Browse files
yangzong18huiguangjun
authored andcommitted
add sample for basic api
1 parent 48aec4d commit b88feb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3023
-8
lines changed

sample/AbortBucketWorm.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
$request = new Oss\Models\AbortBucketWormRequest($bucket);
41+
$result = $client->abortBucketWorm($request);
42+
printf(
43+
'status code:' . $result->statusCode . PHP_EOL .
44+
'request id:' . $result->requestId
45+
);

sample/AbortMultipartUpload.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
"key" => ['help' => 'The name of the object', 'required' => True],
13+
"upload-id" => ['help' => 'The upload id', 'required' => True],
14+
];
15+
$longopts = \array_map(function ($key) {
16+
return "$key:";
17+
}, array_keys($optsdesc));
18+
$options = getopt("", $longopts);
19+
foreach ($optsdesc as $key => $value) {
20+
if ($value['required'] === True && empty($options[$key])) {
21+
$help = $value['help'];
22+
echo "Error: the following arguments are required: --$key, $help";
23+
exit(1);
24+
}
25+
}
26+
27+
$region = $options["region"];
28+
$bucket = $options["bucket"];
29+
$key = $options["key"];
30+
$uploadId = $options["upload-id"];
31+
32+
// Loading credentials values from the environment variables
33+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
34+
35+
// Using the SDK's default configuration
36+
$cfg = Oss\Config::loadDefault();
37+
$cfg->setCredentialsProvider($credentialsProvider);
38+
$cfg->setRegion($region);
39+
if (isset($options["endpoint"])) {
40+
$cfg->setEndpoint($options["endpoint"]);
41+
}
42+
43+
$client = new Oss\Client($cfg);
44+
$request = new Oss\Models\AbortMultipartUploadRequest($bucket, $key);
45+
$request->uploadId = $uploadId;
46+
$result = $client->abortMultipartUpload($request);
47+
printf(
48+
'status code:' . $result->statusCode . PHP_EOL .
49+
'request id:' . $result->requestId
50+
);

sample/AppendObject.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
"key" => ['help' => 'The name of the object', 'required' => True],
13+
];
14+
$longopts = \array_map(function ($key) {
15+
return "$key:";
16+
}, array_keys($optsdesc));
17+
$options = getopt("", $longopts);
18+
foreach ($optsdesc as $key => $value) {
19+
if ($value['required'] === True && empty($options[$key])) {
20+
$help = $value['help'];
21+
echo "Error: the following arguments are required: --$key, $help";
22+
exit(1);
23+
}
24+
}
25+
26+
$region = $options["region"];
27+
$bucket = $options["bucket"];
28+
$key = $options["key"];
29+
30+
// Loading credentials values from the environment variables
31+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
32+
33+
// Using the SDK's default configuration
34+
$cfg = Oss\Config::loadDefault();
35+
$cfg->setCredentialsProvider($credentialsProvider);
36+
$cfg->setRegion($region);
37+
if (isset($options["endpoint"])) {
38+
$cfg->setEndpoint($options["endpoint"]);
39+
}
40+
41+
$client = new Oss\Client($cfg);
42+
43+
$data = 'Hello Append Object';
44+
45+
$request = new Oss\Models\AppendObjectRequest($bucket, $key);
46+
$request->body = Oss\Utils::streamFor($data);
47+
$request->position = 0;
48+
49+
$result = $client->appendObject($request);
50+
51+
printf(
52+
'status code:' . $result->statusCode . PHP_EOL .
53+
'request id:' . $result->requestId
54+
);

sample/AsyncProcessObject.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"key" => ['help' => 'The name of the object', 'required' => True],
13+
];
14+
$longopts = \array_map(function ($key) {
15+
return "$key:";
16+
}, array_keys($optsdesc));
17+
$options = getopt("", $longopts);
18+
foreach ($optsdesc as $key => $value) {
19+
if ($value['required'] === True && empty($options[$key])) {
20+
$help = $value['help'];
21+
echo "Error: the following arguments are required: --$key, $help";
22+
exit(1);
23+
}
24+
}
25+
26+
$region = $options["region"];
27+
$bucket = $options["bucket"];
28+
$key = $options["key"];
29+
30+
// Loading credentials values from the environment variables
31+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
32+
33+
// Using the SDK's default configuration
34+
$cfg = Oss\Config::loadDefault();
35+
$cfg->setCredentialsProvider($credentialsProvider);
36+
$cfg->setRegion($region);
37+
if (isset($options["endpoint"])) {
38+
$cfg->setEndpoint($options["endpoint"]);
39+
}
40+
41+
$client = new Oss\Client($cfg);
42+
43+
$style = "video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1";
44+
$process = sprintf("%s|sys/saveas,b_%s,o_%s",
45+
$style,
46+
rtrim(base64_encode($bucket), '='),
47+
rtrim(base64_encode($key), '=')
48+
);
49+
50+
$request = new Oss\Models\AsyncProcessObjectRequest($bucket, $key);
51+
$request->process = $process;
52+
53+
$result = $client->asyncProcessObject($request);
54+
55+
printf(
56+
'status code:' . $result->statusCode . PHP_EOL .
57+
'request id:' . $result->requestId . PHP_EOL .
58+
'async process result:' . var_export($result, true)
59+
);

sample/CleanRestoredObject.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"key" => ['help' => 'The name of the object', 'required' => True],
13+
];
14+
$longopts = \array_map(function ($key) {
15+
return "$key:";
16+
}, array_keys($optsdesc));
17+
$options = getopt("", $longopts);
18+
foreach ($optsdesc as $key => $value) {
19+
if ($value['required'] === True && empty($options[$key])) {
20+
$help = $value['help'];
21+
echo "Error: the following arguments are required: --$key, $help";
22+
exit(1);
23+
}
24+
}
25+
26+
$region = $options["region"];
27+
$bucket = $options["bucket"];
28+
$key = $options["key"];
29+
30+
// Loading credentials values from the environment variables
31+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
32+
33+
// Using the SDK's default configuration
34+
$cfg = Oss\Config::loadDefault();
35+
$cfg->setCredentialsProvider($credentialsProvider);
36+
$cfg->setRegion($region);
37+
if (isset($options["endpoint"])) {
38+
$cfg->setEndpoint($options["endpoint"]);
39+
}
40+
41+
$client = new Oss\Client($cfg);
42+
43+
$request = new Oss\Models\CleanRestoredObjectRequest($bucket, $key);
44+
$result = $client->cleanRestoredObject($request);
45+
46+
printf(
47+
'status code:' . $result->statusCode . PHP_EOL .
48+
'request id:' . $result->requestId
49+
);

sample/CompleteMultipartUpload.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
"key" => ['help' => 'The name of the object', 'required' => True],
13+
"upload-id" => ['help' => 'The upload id', 'required' => True],
14+
];
15+
$longopts = \array_map(function ($key) {
16+
return "$key:";
17+
}, array_keys($optsdesc));
18+
$options = getopt("", $longopts);
19+
foreach ($optsdesc as $key => $value) {
20+
if ($value['required'] === True && empty($options[$key])) {
21+
$help = $value['help'];
22+
echo "Error: the following arguments are required: --$key, $help";
23+
exit(1);
24+
}
25+
}
26+
27+
$region = $options["region"];
28+
$bucket = $options["bucket"];
29+
$key = $options["key"];
30+
$uploadId = $options["upload-id"];
31+
32+
// Loading credentials values from the environment variables
33+
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
34+
35+
// Using the SDK's default configuration
36+
$cfg = Oss\Config::loadDefault();
37+
$cfg->setCredentialsProvider($credentialsProvider);
38+
$cfg->setRegion($region);
39+
if (isset($options["endpoint"])) {
40+
$cfg->setEndpoint($options["endpoint"]);
41+
}
42+
43+
$client = new Oss\Client($cfg);
44+
45+
$request = new Oss\Models\UploadPartRequest($bucket, $key);
46+
$bigFileName = "upload.tmp";
47+
$partSize = 200 * 1024;
48+
generateFile($bigFileName, 500 * 1024);
49+
$file = fopen($bigFileName, 'r');
50+
$parts = array();
51+
if ($file) {
52+
$i = 1;
53+
while (!feof($file)) {
54+
$chunk = fread($file, $partSize);
55+
$partResult = $client->uploadPart(
56+
new Oss\Models\UploadPartRequest(
57+
$bucket,
58+
$key,
59+
$i,
60+
$uploadId,
61+
null,
62+
null,
63+
null,
64+
null,
65+
Oss\Utils::streamFor($chunk)
66+
)
67+
);
68+
$part = new Oss\Models\UploadPart(
69+
$i,
70+
$partResult->etag,
71+
);
72+
array_push($parts, $part);
73+
$i++;
74+
}
75+
fclose($file);
76+
}
77+
$comResult = $client->completeMultipartUpload(
78+
new Oss\Models\CompleteMultipartUploadRequest(
79+
$bucket,
80+
$key,
81+
$uploadId,
82+
null,
83+
new Oss\Models\CompleteMultipartUpload(
84+
$parts
85+
),
86+
)
87+
);
88+
unlink($bigFileName);
89+
printf(
90+
'status code:' . $comResult->statusCode . PHP_EOL .
91+
'request id:' . $comResult->requestId . PHP_EOL .
92+
'complete multipart upload result:' . var_export($comResult, true)
93+
);
94+
95+
function generateFile($filename, $size)
96+
{
97+
if (
98+
file_exists($filename) &&
99+
$size == sprintf('%u', filesize($filename))
100+
) {
101+
return;
102+
}
103+
$part_size = 32;
104+
$fp = fopen($filename, "w");
105+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
106+
107+
$charactersLength = strlen($characters);
108+
if ($fp) {
109+
while ($size > 0) {
110+
if ($size < $part_size) {
111+
$write_size = $size;
112+
} else {
113+
$write_size = $part_size;
114+
}
115+
$size -= $write_size;
116+
$a = $characters[rand(0, $charactersLength - 1)];
117+
$content = str_repeat($a, $write_size);
118+
$flag = fwrite($fp, $content);
119+
if (!$flag) {
120+
break;
121+
}
122+
}
123+
}
124+
fclose($fp);
125+
}

0 commit comments

Comments
 (0)