Skip to content

Instantly share code, notes, and snippets.

@rob-p
Created September 24, 2017 23:06
Show Gist options
  • Save rob-p/bf6c9ddec36a6472bb3df4a8c1ac68ce to your computer and use it in GitHub Desktop.
Save rob-p/bf6c9ddec36a6472bb3df4a8c1ac68ce to your computer and use it in GitHub Desktop.
Upload a local file to a zenodo deposition
import requests
import argparse
import pprint
import os
def do_upload(args):
ACCESS_TOKEN = args.token
depid = args.deposition_id
url = 'https://zenodo.org/api/deposit/depositions/{}'.format(depid)
r = requests.get(url, params={'access_token': ACCESS_TOKEN})
bucket_url = r.json()['links']['bucket'] + '/'
fname = args.file
target = args.target
if target is None:
target = os.path.basename(fname)
print("writing {} to {}".format(fname, "{}{}".format(bucket_url, target)))
r = requests.put('{}{}'.format(bucket_url, target),
data=open(fname, 'rb'),
headers={"Accept":"application/json",
"Authorization":"Bearer {}".format(ACCESS_TOKEN),
"Content-Type":"application/octet-stream"})
print("Request completed: status code = {}".format(r.status_code))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="upload files to zenodo bucket")
parser.add_argument("--file", required=True, help="path to the local file to be uploaded")
parser.add_argument("--token", required=True, help="zenodo token")
parser.add_argument("--deposition_id", required=True, help="zenodo deposition id")
parser.add_argument("--target", help="target filename (default, same as local file)")
args = parser.parse_args()
do_upload(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment