Browse Source

Add backup to tencent cos

Signed-off-by: Ein Verne <[email protected]>
pull/4/head
Ein Verne 7 years ago
parent
commit
f4896a7b83
  1. 42
      backup_tencent_cos.py

42
backup_tencent_cos.py

@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pip install -U cos-python-sdk-v5
import os
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
secret_id = ''
secret_key = ''
region = ''
bucket = ''
token = ''
def put(client, file_path):
if not os.path.exists(file_path):
raise Exception("file not exist")
with open(file_path, 'rb') as f:
r = client.put_object(
Bucket=bucket,
Body=f,
Key=os.path.basename(file_path),
)
print r
def get(client, file_name):
r = client.get_object(
Bucket=bucket,
Key=file_name,
)
r['Body'].get_stream_to_file(file_name)
# r['Body'].get_raw_stream() 流
if __name__ == '__main__':
config = CosConfig(Secret_id=secret_id, Secret_key=secret_key, Region=region)
client = CosS3Client(conf=config)
put(client, '')
get(client, '')
Loading…
Cancel
Save