Last active
April 15, 2019 18:56
-
-
Save csjx/1f09dff328977f44d7e83c1ece8f29ef to your computer and use it in GitHub Desktop.
A quick example of a DataONE MNStorage.create() API call in python
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
#!/bin/env python3 | |
from d1_client.mnclient_2_0 import MemberNodeClient_2_0 | |
from d1_common.types import dataoneTypes | |
import uuid | |
import hashlib | |
import io | |
# Log in and get a token from https://knb.ecoinformatics.org | |
# or https://dev.nceas.ucsb.edu if using a a test server | |
token = "<your-jwt-token-goes-here>" | |
# base_url = "https://knb.ecoinformatics.org/knb/d1/mn" | |
base_url = "https://dev.nceas.ucsb.edu/knb/d1/mn" | |
# Append the JWT token as an HTTP header | |
headers = { | |
"Authorization": "Bearer " + token | |
} | |
# Create a Member Node Client to interact with the KNB | |
mn = MemberNodeClient_2_0(base_url, headers=headers) | |
# Pre-assign an object identifier (or generate one on the fly) | |
pid = "urn:uuid:043b67fd-825a-4dca-9149-7ab5b4c590ec" | |
#pid = "urn:uuid:" + str(uuid.uuid4()) | |
# Open the pre-created sysmeta document (or create one on the fly) | |
# along with the data file it describes | |
with open("urn-uuid-043b67fd-825a-4dca-9149-7ab5b4c590ec.sysmeta.xml", "r") as sysmeta_file, open("urn-uuid-043b67fd-825a-4dca-9149-7ab5b4c590ec.txt", "r") as object_file: | |
sysmeta_obj = dataoneTypes.CreateFromDocument(sysmeta_file.read()) | |
# An example of updating a system metadata field (the checksum) | |
# checksum = hashlib.sha1() | |
# checksum.update(object_file.read()) | |
# sysmeta_obj(setChecksum(checksum.digest())) | |
# Update the identifier if generated | |
# sysmeta_obj.identifier.value(pid) | |
print("Sending create() request for " + pid) | |
response = mn.create(pid, object_file.read(), sysmeta_obj) | |
print(response.value()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment