1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import os import datetime import zipfile from qiniu import Auth, put_file, etag, urlsafe_base64_encode
def create_zip_archive(source_dir, output_filename): with zipfile.ZipFile(output_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, dirs, files in os.walk(source_dir): for file in files: zipf.write(os.path.join(root, file))
def upload_to_qiniu(localfile, bucket_name, key): access_key = 'your_access_key' secret_key = 'your_secret_key'
q = Auth(access_key, secret_key) token = q.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, localfile) print(info)
yesterday = datetime.datetime.now() - datetime.timedelta(days=1) date_str = yesterday.strftime('%Y/%m/%d')
source_dir = '/path/to/your/directory'
output_filename = f'data_{yesterday.strftime("%Y%m%d")}.zip'
create_zip_archive(source_dir, output_filename)
remote_path = f'yunzai/data/backup/{date_str}/data.zip'
upload_to_qiniu(output_filename, 'your_bucket_name', remote_path)
os.remove(output_filename)
|