|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import glob |
| 5 | +import re |
| 6 | +import logging |
| 7 | +import os |
| 8 | + |
| 9 | +import qiniu.config |
| 10 | +from qiniu import Auth, etag, put_file |
| 11 | +from qiniu import Zone, set_default |
| 12 | +from retry import retry |
| 13 | + |
| 14 | +class Qiniu: |
| 15 | + def __init__(self, access_key, secret_key, bucket_name: str): |
| 16 | + self._access_key = access_key |
| 17 | + self._secret_key = secret_key |
| 18 | + self._auth = Auth(access_key, secret_key) |
| 19 | + self._bucket = bucket_name |
| 20 | + |
| 21 | + @retry(tries=5, delay=0.5, jitter=0.1, logger=logging) |
| 22 | + def upload_file(self, key, localfile): |
| 23 | + token = self._auth.upload_token(self._bucket, key) |
| 24 | + ret, info = put_file(token, key, localfile) |
| 25 | + if ret: # ret possibly is None |
| 26 | + assert ret['key'] == key |
| 27 | + assert ret['hash'] == etag(localfile) |
| 28 | + return info |
| 29 | + |
| 30 | + |
| 31 | +access_key = 'caBese-UQYXwQeigGqtdgwybP2Qh2AlDPdcEd42C' |
| 32 | +secret_key = 'SIG4DGyO45aseWSvOc9Be-ZkGrxoc-IpWlM5xts8' |
| 33 | + |
| 34 | +zone = Zone(up_host='https://up.qiniup.com', |
| 35 | + up_host_backup='https://upload.qiniup.com', |
| 36 | + io_host='http://iovip.qbox.me', |
| 37 | + scheme='https') |
| 38 | +set_default(default_zone=zone) |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + parser = argparse.ArgumentParser() |
| 43 | + parser.add_argument("-A", |
| 44 | + '--access-key', |
| 45 | + help='access key, env-var QINIU_ACCESS_KEY', |
| 46 | + default=os.environ.get("QINIU_ACCESS_KEY", access_key)) |
| 47 | + parser.add_argument("-S", |
| 48 | + "--secret-key", |
| 49 | + help='secret key, env-var QINIU_SECRET_KEY', |
| 50 | + default=os.environ.get("QINIU_SECRET_KEY", secret_key)) |
| 51 | + parser.add_argument("-B", |
| 52 | + "--bucket", |
| 53 | + help='bucket name', |
| 54 | + default=os.environ.get("QINIU_BUCKET", "atxupload")) |
| 55 | + args = parser.parse_args() |
| 56 | + |
| 57 | + assert args.access_key |
| 58 | + assert args.secret_key |
| 59 | + assert args.bucket |
| 60 | + |
| 61 | + qn = Qiniu(args.access_key, args.secret_key, args.bucket) |
| 62 | + checksum_path = glob.glob("*/atx-agent_*_checksums.txt")[0] |
| 63 | + distdir = os.path.dirname(checksum_path) |
| 64 | + |
| 65 | + version = re.search(r"([\d.]+)", checksum_path).group(0) |
| 66 | + print(checksum_path, version) |
| 67 | + key_prefix = "openatx/atx-agent/releases/download/" + version + "/" |
| 68 | + qn.upload_file(key_prefix + os.path.basename(checksum_path), checksum_path) |
| 69 | + with open(checksum_path, 'r', encoding='utf-8') as f: |
| 70 | + for line in f: |
| 71 | + _, filename = line.split() |
| 72 | + key = key_prefix + filename |
| 73 | + print("-", filename, "->", key) |
| 74 | + qn.upload_file(key, os.path.join(distdir, filename)) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments