Last active
May 21, 2024 10:58
-
-
Save slint/92e4d38eb49dd177f46b02e1fe9761e1 to your computer and use it in GitHub Desktop.
This file contains 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 | |
params = {'access_token': '<your-access-token>'} | |
# Create the deposit resource | |
url = "https://sandbox.zenodo.org/api/deposit/depositions" | |
headers = {"Content-Type": "application/json"} | |
res = requests.post( | |
url, | |
json={}, | |
# Headers are not necessary here since "requests" automatically | |
# adds "Content-Type: application/json", because we're using | |
# the "json=" keyword argument... | |
# headers=headers, | |
params=params, | |
) | |
print(res.json()) | |
# In the new files API we use a PUT request to a 'bucket' link, which is the container for files | |
# bucket url looks like this: 'https://sandbox.zenodo.org/api/files/12341234-abcd-1234-abcd-0e62efee00c0' | |
bucket_url = res.json()['links']['bucket'] | |
# We pass the file object (fp) directly to request as 'data' for stream upload | |
# the target URL is the URL of the bucket and the desired filename on Zenodo seprated by slash | |
with open('/path/to/my-file.zip', 'rb') as fp: | |
res = requests.put( | |
bucket_url + '/my-file.zip', | |
data=fp, | |
# No headers included in the request, since it's a raw byte request | |
params=params, | |
) | |
print(res.json()) |
I think this is all it takes to add a progress bar:
import os
import requests
from tqdm import tqdm # pip install tqdm
from tqdm.utils import CallbackIOWrapper
params = {"access_token": "<your-access-token>"}
# Get bucket URL
res = requests.get(
"https://zenodo.org/api/deposit/depositions/<your-deposit-id>",
json={},
params=params,
)
# print(res.json())
bucket_url = res.json()["links"]["bucket"]
# Upload file
file_path = "<path-to-file-to-upload>"
file_size = os.stat(file_path).st_size
with open(file_path, "rb") as f:
with tqdm(total=file_size, unit="B", unit_scale=True, unit_divisor=1024) as t:
wrapped_file = CallbackIOWrapper(t.update, f, "read")
requests.put(
bucket_url + "/" + os.path.basename(file_path),
data=wrapped_file,
params=params,
)
Based on this gist.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you add a percentage indicator of the upload process?
it would be great in large file sizes.