*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 15f0ae11 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Add simple file upload script

parents
No related branches found
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# Idea software family
.idea/
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
# Django stuff:
*.log
# PyBuilder
target/
# Vim swapfiles
.*.sw?
Invenio Utilities
=================
Utility scripts and tools for handling Invenio.
upload
------
Record creation & file upload utility for the REST API introduced in the InvenioRDM November 2020 release.
{
"metadata": {
"resource_type": { "type": "other", "subtype": "" },
"title": "Test Record without Files",
"description": "A test record following the metadata schema for InvenioRDM November 2020 release (https://inveniordm.docs.cern.ch/reference/metadata/)",
"creators": [
{
"given_name": "Max",
"family_name": "Moser",
"type": "personal",
"affiliations": [
{ "name": "TU Wien", "identifiers": { "ror": "04d836q62" } }
]
},
{
"given_name": "Tomasz",
"family_name": "Miksa",
"type": "personal",
"identifiers": { "orcid": "0000-0002-4929-7875" },
"affiliations": [
{ "name": "TU Wien", "identifiers": { "ror": "04d836q62" } }
]
}
],
"publisher": "TU Data",
"publication_date": "2020-12-01"
},
"access": {
"metadata": false,
"files": false,
"owned_by": [1],
"access_right": "open"
}
}
upload/test-record/files/GrillingFloor.png

50.1 KiB

upload/test-record/files/alps.jpg

969 KiB

upload/test-record/files/max.png

297 KiB

{
"metadata": {
"resource_type": { "type": "other", "subtype": "" },
"title": "Test Record with Files",
"description": "A test record following the metadata schema for InvenioRDM November 2020 release (https://inveniordm.docs.cern.ch/reference/metadata/)",
"creators": [
{
"given_name": "Max",
"family_name": "Moser",
"type": "personal",
"affiliations": [
{ "name": "TU Wien", "identifiers": { "ror": "04d836q62" } }
]
},
{
"given_name": "Tomasz",
"family_name": "Miksa",
"type": "personal",
"identifiers": { "orcid": "0000-0002-4929-7875" },
"affiliations": [
{ "name": "TU Wien", "identifiers": { "ror": "04d836q62" } }
]
}
],
"publisher": "TU Data",
"publication_date": "2020-12-01"
},
"access": {
"metadata": false,
"files": false,
"owned_by": [1],
"access_right": "open"
}
}
#!/bin/bash
# script for creating records in invenio (november 2020 release)
#
# a record should either consist of a single json file containing the metadata
# in the expected format (record.json), or it should be a directory containing
# the file 'metadata.json' and a directory 'files/'.
# in the latter case, all files in this subdirectory will be uploaded as part
# of the deposit.
#
# record/
# metadata.json
# files/
# file1.ext
# file2.ext
# ...
function usage () {
echo "usage: $0 [ RECORD_DIR | RECORD_JSON ] ..." 1>&2
}
base_url="https://localhost:5000"
while getopts "u:h" opt; do
case "${opt}" in
u)
base_url=${OPTARG}
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $# -lt 1 ]]; then
usage
exit 1
fi
record_dirs=()
record_files=()
while [[ $# -gt 0 ]]; do
record=$1
if [[ -f $record ]]; then
jq . $record > /dev/null
if [[ $? -eq 0 ]]; then
record_files+=( $record )
else
echo "not a valid json file: $record/metadata.json" 1>&2
fi
elif [[ -d $record && -f $record/metadata.json ]]; then
jq . $record/metadata.json > /dev/null
if [[ $? -eq 0 ]]; then
record_dirs+=( $record )
else
echo "not a valid json file: $record/metadata.json" 1>&2
fi
else
echo "unknown entity: $record" 1>&2
fi
shift
done
if [[ $(( ${#record_dirs[@]} + ${#record_files[@]} )) -le 0 ]]; then
echo "no valid record files/directories specified"
exit 1
fi
recids=()
for record in ${record_dirs[@]}; do
recid=$(curl -sk -X POST -H 'Content-Type: application/json' -d @${record}/metadata.json ${base_url}/api/records | jq ".id")
recid=${recid%\"}
recid=${recid#\"}
recids+=( $recid )
files=()
file_objects=""
if [[ -d $record/files ]]; then
for file in $record/files/*; do
if [[ ! -f $file ]]; then
echo "not a file: $file" 1>&2
continue
fi
file_name=$(basename $file)
files+=( $file_name )
file_object="{\"key\":\"${file_name}\"}"
file_objects="${file_objects},${file_object}"
done
fi
curl -sk -X POST -H 'Content-Type: application/json' -d "[${file_objects:1}]" ${base_url}/api/records/${recid}/draft/files > /dev/null
for file in ${files[@]}; do
curl -sk -X PUT -H 'Content-Type: application/octet-stream' --upload-file ${record}/files/${file} ${base_url}/api/records/${recid}/draft/files/${file}/content > /dev/null
curl -sk -X POST ${base_url}/api/records/${recid}/draft/files/${file}/commit > /dev/null
done
curl -sk -X POST ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
done
for record in ${record_files[@]}; do
recid=$(curl -sk -X POST -H 'Content-Type: application/json' -d @${record} ${base_url}/api/records | jq ".id")
recid=${recid%\"}
recid=${recid#\"}
recids+=( $recid )
curl -sk -X POST ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
done
for recid in ${recids[@]}; do
echo ${recid}
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment