Skip to content

Commit 119b2ae

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
add more samples
1 parent 901f578 commit 119b2ae

22 files changed

+1054
-14
lines changed

alibabacloud_oss_v2/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def uploader(self, **kwargs) -> Uploader:
697697
return Uploader(self, **kwargs)
698698

699699
# file like objects
700-
def appen_file(self, bucket: str, key: str,
700+
def append_file(self, bucket: str, key: str,
701701
request_payer: Optional[str] = None,
702702
create_parameter: Optional[models.AppendObjectRequest] = None,
703703
**kwargs) -> AppendOnlyFile:

sample/append_file.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="append file sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
10+
def main():
11+
12+
args = parser.parse_args()
13+
14+
# Loading credentials values from the environment variables
15+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
16+
17+
# Using the SDK's default configuration
18+
cfg = oss.config.load_default()
19+
cfg.credentials_provider = credentials_provider
20+
cfg.region = args.region
21+
if args.endpoint is not None:
22+
cfg.endpoint = args.endpoint
23+
24+
client = oss.Client(cfg)
25+
26+
data1 = b'hello'
27+
data2 = b' world. '
28+
29+
30+
with client.append_file(bucket=args.bucket, key=args.key) as f:
31+
append_f = f
32+
f.write(data1)
33+
print(f'closed: {append_f.closed},'
34+
f' name: {append_f.name},'
35+
)
36+
37+
with client.append_file(bucket=args.bucket, key=args.key) as f:
38+
append_f = f
39+
f.write(data2)
40+
print(f'closed: {append_f.closed},'
41+
f' name: {append_f.name},'
42+
)
43+
44+
result = client.get_object(oss.GetObjectRequest(
45+
bucket=args.bucket,
46+
key=args.key,
47+
))
48+
print(f'status code: {result.status_code},'
49+
f' request id: {result.request_id},'
50+
f' content: { result.body.content},'
51+
)
52+
53+
54+
if __name__ == "__main__":
55+
main()

sample/describe_regions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="describe regions sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--regions', help='Regional information.')
9+
10+
11+
def main():
12+
13+
args = parser.parse_args()
14+
15+
# Loading credentials values from the environment variables
16+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
17+
18+
# Using the SDK's default configuration
19+
cfg = oss.config.load_default()
20+
cfg.credentials_provider = credentials_provider
21+
cfg.region = args.region
22+
if args.endpoint is not None:
23+
cfg.endpoint = args.endpoint
24+
25+
client = oss.Client(cfg)
26+
27+
result = client.describe_regions(oss.DescribeRegionsRequest(
28+
regions=args.regions,
29+
))
30+
31+
print(f'status code: {result.status_code},'
32+
f' request id: {result.request_id},'
33+
)
34+
for rg in result.region_info:
35+
print(f'region: {rg.region},'
36+
f' internet endpoint: {rg.internet_endpoint},'
37+
f' internal endpoint: {rg.internal_endpoint},'
38+
f' accelerate endpoint: {rg.accelerate_endpoint},'
39+
)
40+
41+
if __name__ == "__main__":
42+
main()

sample/download_file.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="download file sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
parser.add_argument('--file_path', help='The path of Upload file.', required=True)
10+
11+
12+
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.Client(cfg)
27+
28+
down_loader = client.downloader()
29+
30+
# down_loader = client.downloader(part_size=100*1024,
31+
# parallel_num=5,
32+
# block_size=1024,
33+
# use_temp_file=True,
34+
# enable_checkpoint=True,
35+
# checkpoint_dir=args.file_path,
36+
# verify_data=True)
37+
38+
result = down_loader.download_file(oss.GetObjectRequest(
39+
bucket=args.bucket,
40+
key=args.key,
41+
), filepath=args.file_path)
42+
43+
print(f'written: {result.written}')
44+
45+
46+
47+
if __name__ == "__main__":
48+
main()
49+

sample/download_to.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="download to sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
parser.add_argument('--file_path', help='The path of Upload file.', required=True)
10+
11+
12+
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.Client(cfg)
27+
28+
down_loader = client.downloader()
29+
30+
# down_loader = client.downloader(part_size=100*1024,
31+
# parallel_num=5,
32+
# block_size=1024,
33+
# use_temp_file=True,
34+
# enable_checkpoint=True,
35+
# checkpoint_dir=args.file_path,
36+
# verify_data=True)
37+
38+
39+
with open(args.file_path, 'wb') as f:
40+
result = down_loader.download_to(oss.GetObjectRequest(
41+
bucket=args.bucket,
42+
key=args.key,
43+
), writer=f)
44+
print(f'written: {result.written}')
45+
46+
47+
48+
if __name__ == "__main__":
49+
main()
50+

sample/encryption_get_object.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="encryption get object sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
10+
11+
RSA_PUBLIC_KEY = """-----BEGIN PUBLIC KEY-----
12+
MIGfMA0G6mse2QsIgz3******GBcom6kEF6MmR1EKixaQIDAQAB
13+
-----END PUBLIC KEY-----"""
14+
15+
RSA_PRIVATE_KEY = """-----BEGIN PRIVATE KEY-----
16+
MIICdQIBADANBgk******ItewfwXIL1Mqz53lO/gK+q6TR92gGc+4ajL
17+
-----END PRIVATE KEY-----"""
18+
19+
def main():
20+
21+
args = parser.parse_args()
22+
23+
# Loading credentials values from the environment variables
24+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
25+
26+
# Using the SDK's default configuration
27+
cfg = oss.config.load_default()
28+
cfg.credentials_provider = credentials_provider
29+
cfg.region = args.region
30+
if args.endpoint is not None:
31+
cfg.endpoint = args.endpoint
32+
33+
client = oss.Client(cfg)
34+
35+
mc = oss.crypto.MasterRsaCipher(
36+
mat_desc={"tag": "value"},
37+
public_key=RSA_PUBLIC_KEY,
38+
private_key=RSA_PRIVATE_KEY
39+
)
40+
encryption_client = oss.EncryptionClient(client, mc)
41+
42+
result = encryption_client.get_object(oss.GetObjectRequest(
43+
bucket=args.bucket,
44+
key=args.key,
45+
))
46+
47+
print(f'status code: {result.status_code},'
48+
f' request id: {result.request_id},'
49+
f' content md5: {result.content_md5},'
50+
f' etag: {result.etag},'
51+
f' hash crc64: {result.hash_crc64},'
52+
f' version id: {result.version_id},'
53+
)
54+
55+
56+
if __name__ == "__main__":
57+
main()

sample/encryption_put_object.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="encryption put object sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
10+
11+
RSA_PUBLIC_KEY = """-----BEGIN PUBLIC KEY-----
12+
MIGfMA0G6mse2QsIgz3******GBcom6kEF6MmR1EKixaQIDAQAB
13+
-----END PUBLIC KEY-----"""
14+
15+
RSA_PRIVATE_KEY = """-----BEGIN PRIVATE KEY-----
16+
MIICdQIBADANBgk******ItewfwXIL1Mqz53lO/gK+q6TR92gGc+4ajL
17+
-----END PRIVATE KEY-----"""
18+
19+
def main():
20+
21+
args = parser.parse_args()
22+
23+
# Loading credentials values from the environment variables
24+
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
25+
26+
# Using the SDK's default configuration
27+
cfg = oss.config.load_default()
28+
cfg.credentials_provider = credentials_provider
29+
cfg.region = args.region
30+
if args.endpoint is not None:
31+
cfg.endpoint = args.endpoint
32+
33+
client = oss.Client(cfg)
34+
35+
mc = oss.crypto.MasterRsaCipher(
36+
mat_desc={"tag": "value"},
37+
public_key=RSA_PUBLIC_KEY,
38+
private_key=RSA_PRIVATE_KEY
39+
)
40+
encryption_client = oss.EncryptionClient(client, mc)
41+
42+
data = b'hello world'
43+
44+
result = encryption_client.put_object(oss.PutObjectRequest(
45+
bucket=args.bucket,
46+
key=args.key,
47+
body=data,
48+
))
49+
50+
print(f'status code: {result.status_code},'
51+
f' request id: {result.request_id},'
52+
f' content md5: {result.content_md5},'
53+
f' etag: {result.etag},'
54+
f' hash crc64: {result.hash_crc64},'
55+
f' version id: {result.version_id},'
56+
)
57+
58+
59+
if __name__ == "__main__":
60+
main()

sample/get_object_range.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import argparse
2+
import alibabacloud_oss_v2 as oss
3+
4+
parser = argparse.ArgumentParser(description="get object range sample")
5+
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
6+
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
7+
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
8+
parser.add_argument('--key', help='The name of the object.', required=True)
9+
parser.add_argument('--range', help='Specify the scope of file transfer. Example value: bytes=0-9', required=True)
10+
parser.add_argument('--range_behavior', help='Standard for downloading behavior within a specified range. Example value: standard.')
11+
12+
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.Client(cfg)
27+
28+
result = client.get_object(oss.GetObjectRequest(
29+
bucket=args.bucket,
30+
key=args.key,
31+
range_header=args.range,
32+
range_behavior=args.range_behavior,
33+
))
34+
print(f'status code: {result.status_code},'
35+
f' request id: {result.request_id},'
36+
f' content length: {result.content_length},'
37+
f' content range: {result.content_range},'
38+
f' content type: {result.content_type},'
39+
f' etag: {result.etag},'
40+
f' last modified: {result.last_modified},'
41+
f' content md5: {result.content_md5},'
42+
f' cache control: {result.cache_control},'
43+
f' content disposition: {result.content_disposition},'
44+
f' content encoding: {result.content_encoding},'
45+
f' expires: {result.expires},'
46+
f' hash crc64: {result.hash_crc64},'
47+
f' storage class: {result.storage_class},'
48+
f' object type: {result.object_type},'
49+
f' version id: {result.version_id},'
50+
f' tagging count: {result.tagging_count},'
51+
f' server side encryption: {result.server_side_encryption},'
52+
f' server side data encryption: {result.server_side_data_encryption},'
53+
f' sse kms key id: {result.sse_kms_key_id},'
54+
f' next append position: {result.next_append_position},'
55+
f' expiration: {result.expiration},'
56+
f' restore: {result.restore},'
57+
f' process status: {result.process_status},'
58+
f' delete marker: {result.delete_marker},'
59+
)
60+
61+
if __name__ == "__main__":
62+
main()
63+

0 commit comments

Comments
 (0)