-
-
Save k3yavi/3ba060f229455e61d877f98163aeb592 to your computer and use it in GitHub Desktop.
Upload a local file to a zenodo deposition
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
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