Skip to content

Commit fab3d55

Browse files
committed
add async sample code
1 parent 1633bb4 commit fab3d55

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

sample/aio/get_bucket_info.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import asyncio
2+
import argparse
3+
import alibabacloud_oss_v2 as oss
4+
import alibabacloud_oss_v2.aio as oss_aio
5+
6+
7+
parser = argparse.ArgumentParser(description="get bucket info sample")
8+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
9+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
10+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
11+
12+
13+
async def main():
14+
15+
args = parser.parse_args()
16+
17+
# Loading credentials values from the environment variables
18+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
19+
20+
# Using the SDK's default configuration
21+
cfg = oss.config.load_default()
22+
cfg.credentials_provider = credentials_provider
23+
cfg.region = args.region
24+
if args.endpoint is not None:
25+
cfg.endpoint = args.endpoint
26+
27+
client = oss_aio.AsyncClient(cfg)
28+
29+
result = await client.get_bucket_info(oss.GetBucketInfoRequest(
30+
bucket=args.bucket,
31+
))
32+
33+
print(f'status code: {result.status_code},'
34+
f' request id: {result.request_id},'
35+
f' name: {result.bucket_info.name},'
36+
f' access monitor: {result.bucket_info.access_monitor},'
37+
f' location: {result.bucket_info.location},'
38+
f' creation date: {result.bucket_info.creation_date},'
39+
f' extranet endpoint: {result.bucket_info.extranet_endpoint},'
40+
f' intranet endpoint: {result.bucket_info.intranet_endpoint},'
41+
f' acl: {result.bucket_info.acl},'
42+
f' data redundancy type: {result.bucket_info.data_redundancy_type},'
43+
f' id: {result.bucket_info.owner.id},'
44+
f' display name: {result.bucket_info.owner.display_name},'
45+
f' storage class: {result.bucket_info.storage_class},'
46+
f' resource group id: {result.bucket_info.resource_group_id},'
47+
f' kms master key id: {result.bucket_info.sse_rule.kms_master_key_id},'
48+
f' sse algorithm: {result.bucket_info.sse_rule.sse_algorithm},'
49+
f' kms data encryption: {result.bucket_info.sse_rule.kms_data_encryption},'
50+
f' versioning: {result.bucket_info.versioning},'
51+
f' transfer acceleration: {result.bucket_info.transfer_acceleration},'
52+
f' cross region replication: {result.bucket_info.cross_region_replication},'
53+
f' log bucket: {result.bucket_info.bucket_policy.log_bucket},'
54+
f' log prefix: {result.bucket_info.bucket_policy.log_prefix},'
55+
f' comment: {result.bucket_info.comment},'
56+
f' block public access: {result.bucket_info.block_public_access},'
57+
)
58+
59+
await client.close()
60+
61+
62+
if __name__ == "__main__":
63+
asyncio.run(main())

sample/aio/get_object.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
import argparse
3+
import alibabacloud_oss_v2 as oss
4+
import alibabacloud_oss_v2.aio as oss_aio
5+
6+
parser = argparse.ArgumentParser(description="get object sample")
7+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
8+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
9+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
10+
parser.add_argument('--key', help='The name of the object.', required=True)
11+
12+
13+
async def main():
14+
15+
args = parser.parse_args()
16+
17+
# Loading credentials values from the environment variables
18+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
19+
20+
# Using the SDK's default configuration
21+
cfg = oss.config.load_default()
22+
cfg.credentials_provider = credentials_provider
23+
cfg.region = args.region
24+
if args.endpoint is not None:
25+
cfg.endpoint = args.endpoint
26+
27+
client = oss_aio.AsyncClient(cfg)
28+
29+
result = await client.get_object(oss.GetObjectRequest(
30+
bucket=args.bucket,
31+
key=args.key,
32+
))
33+
34+
# Under the async mode, the data has been loaded into memory
35+
#print(f'content:{result.body.content}')
36+
37+
print(f'status code: {result.status_code},'
38+
f' request id: {result.request_id},'
39+
f' content length: {result.content_length},'
40+
f' content range: {result.content_range},'
41+
f' content type: {result.content_type},'
42+
f' etag: {result.etag},'
43+
f' last modified: {result.last_modified},'
44+
f' content md5: {result.content_md5},'
45+
f' cache control: {result.cache_control},'
46+
f' content disposition: {result.content_disposition},'
47+
f' content encoding: {result.content_encoding},'
48+
f' expires: {result.expires},'
49+
f' hash crc64: {result.hash_crc64},'
50+
f' storage class: {result.storage_class},'
51+
f' object type: {result.object_type},'
52+
f' version id: {result.version_id},'
53+
f' tagging count: {result.tagging_count},'
54+
f' server side encryption: {result.server_side_encryption},'
55+
f' server side data encryption: {result.server_side_data_encryption},'
56+
f' server side encryption key id: {result.server_side_encryption_key_id},'
57+
f' next append position: {result.next_append_position},'
58+
f' expiration: {result.expiration},'
59+
f' restore: {result.restore},'
60+
f' process status: {result.process_status},'
61+
f' delete marker: {result.delete_marker},'
62+
)
63+
64+
await client.close()
65+
66+
67+
if __name__ == "__main__":
68+
asyncio.run(main())

sample/aio/put_object.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import argparse
3+
import alibabacloud_oss_v2 as oss
4+
import alibabacloud_oss_v2.aio as oss_aio
5+
6+
parser = argparse.ArgumentParser(description="put object sample")
7+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
8+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
9+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
10+
parser.add_argument('--key', help='The name of the object.', required=True)
11+
12+
async def main():
13+
14+
args = parser.parse_args()
15+
16+
# Loading credentials values from the environment variables
17+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
18+
19+
# Using the SDK's default configuration
20+
cfg = oss.config.load_default()
21+
cfg.credentials_provider = credentials_provider
22+
cfg.region = args.region
23+
if args.endpoint is not None:
24+
cfg.endpoint = args.endpoint
25+
26+
client = oss_aio.AsyncClient(cfg)
27+
28+
data = b'hello world'
29+
30+
result = await client.put_object(oss.PutObjectRequest(
31+
bucket=args.bucket,
32+
key=args.key,
33+
body=data,
34+
))
35+
36+
print(f'status code: {result.status_code},'
37+
f' request id: {result.request_id},'
38+
f' content md5: {result.content_md5},'
39+
f' etag: {result.etag},'
40+
f' hash crc64: {result.hash_crc64},'
41+
f' version id: {result.version_id},'
42+
)
43+
44+
await client.close()
45+
46+
47+
if __name__ == "__main__":
48+
asyncio.run(main())

sample/vector/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)