|
| 1 | +import argparse |
| 2 | +import alibabacloud_oss_v2 as oss |
| 3 | + |
| 4 | +parser = argparse.ArgumentParser(description="upload from 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('--src_key', help='The name of the object.', required=True) |
| 9 | +parser.add_argument('--dst_key', help='The name of the object.', required=True) |
| 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 | + up_loader = client.uploader() |
| 28 | + |
| 29 | + # up_loader = client.uploader(part_size=100*1024, |
| 30 | + # parallel_num=5, |
| 31 | + # leave_parts_on_error=True, |
| 32 | + # enable_checkpoint=True, |
| 33 | + # checkpoint_dir=args.file_path) |
| 34 | + # |
| 35 | + with client.open_file( |
| 36 | + bucket=args.bucket, |
| 37 | + key=args.src_key, |
| 38 | + enable_prefetch=True, |
| 39 | + #prefetch_num=3, |
| 40 | + #chunk_size=6*1024*1024, |
| 41 | + prefetch_threshold = 0) as f: |
| 42 | + |
| 43 | + # set seekable to False to read source sequentially |
| 44 | + f._seekable = False |
| 45 | + |
| 46 | + result = up_loader.upload_from(oss.PutObjectRequest( |
| 47 | + bucket=args.bucket, |
| 48 | + key=args.dst_key, |
| 49 | + ), reader=f) |
| 50 | + |
| 51 | + print(f'status code: {result.status_code},' |
| 52 | + f' request id: {result.request_id},' |
| 53 | + f' content md5: {result.headers.get("Content-MD5")},' |
| 54 | + f' etag: {result.etag},' |
| 55 | + f' hash crc64: {result.hash_crc64},' |
| 56 | + f' version id: {result.version_id},' |
| 57 | + f' server time: {result.headers.get("x-oss-server-time")},' |
| 58 | + ) |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
| 62 | + |
0 commit comments