Last active
February 17, 2017 21:28
-
-
Save nottrobin/bad4d1b8f880bbb23ed10f5457d976bc to your computer and use it in GitHub Desktop.
A script for uploading assets in bulk to the Ubuntu assets server (assets.ubuntu.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python3 | |
| """ | |
| A command-line tool for uploading assets to the assets server. | |
| The only dependency is `requests`: | |
| $ pip install requests | |
| $ wget https://gist.githubusercontent.com/nottrobin/bad4d1b8f880bbb23ed10f5457d976bc/raw/707305dbd616dc46f38d0524e2f6248415173f57/upload-assets.py | |
| Usage: | |
| $ ./upload-assets.py --api-token xxxxxxxx asset1.png asset2.png # Upload specific assets | |
| $ ./upload-assets.py --api-token xxxxxxxx a_dir > output.json # Upload all assets in a directory (recursively), store JSON output in a file | |
| $ API_SECRET_TOKEN=xxxx ./upload-assets.py . # Upload all assets in the current directory, getting the API secret from an environment variable | |
| """ | |
| # Core packages | |
| import argparse | |
| import base64 | |
| import json | |
| import os | |
| import glob | |
| import sys | |
| import urllib | |
| # Third party packages | |
| import requests | |
| api_base_url = os.environ.get('API_BASE_URL', 'https://assets.ubuntu.com/v1/') | |
| api_secret_token = os.environ.get('API_SECRET_TOKEN') | |
| parser = argparse.ArgumentParser( | |
| description='Upload assets from this directory' | |
| ) | |
| parser.add_argument( | |
| '-u', '--api-url', | |
| help='The API base URL', default=api_base_url | |
| ) | |
| parser.add_argument( | |
| '-t', '--tags', | |
| help='Tags for uploaded assets', default='auto-upload' | |
| ) | |
| parser.add_argument( | |
| '-s', '--api-token', | |
| help='The secret authentication token for the API', | |
| required=not bool(api_secret_token), | |
| default=api_secret_token | |
| ) | |
| parser.add_argument( | |
| 'upload_paths', | |
| help='A list of paths to files or directories to upload', | |
| nargs='+' | |
| ) | |
| args = vars(parser.parse_args()) | |
| uploaded = [] | |
| print("[") | |
| for upload_path in args['upload_paths']: | |
| if os.path.isdir(upload_path): | |
| upload_path = os.path.join(upload_path, '**') | |
| for filepath in glob.glob(upload_path, recursive=True): | |
| filename = os.path.basename(filepath) | |
| if not os.path.isfile(filepath): | |
| continue | |
| with open(filepath, 'rb') as asset_file: | |
| content = asset_file.read() | |
| response = requests.post( | |
| args['api_url'], | |
| data={ | |
| 'asset': base64.b64encode(content), | |
| 'friendly-name': filename, | |
| 'tags': args['tags'], | |
| 'type': 'base64', | |
| 'token': args['api_token'] | |
| } | |
| ) | |
| if response.status_code != 409: | |
| response.raise_for_status() | |
| response_info = response.json() | |
| uploaded_info = { | |
| 'filepath': filepath, | |
| 'url': urllib.parse.urljoin( | |
| args['api_url'], | |
| response_info['file_path'] | |
| ) | |
| } | |
| print(' ' + json.dumps(uploaded_info) + ',') | |
| sys.stdout.flush() | |
| print("]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment