Skip to content

Instantly share code, notes, and snippets.

@arbakker
Created August 14, 2019 14:19
Show Gist options
  • Save arbakker/4d7b0870d5f62f87ff13c40589788023 to your computer and use it in GitHub Desktop.
Save arbakker/4d7b0870d5f62f87ff13c40589788023 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z "$1" ]
then
echo "Usage: `basename $0` [endpoint] [file_path]"
exit 0
fi
if [ -z "$2" ]
then
echo "Usage: `basename $0` [endpoint] [file_path]"
exit 0
fi
endpoint="$1"
file_to_upload="$2"
filename=$(basename $file_to_upload)
filename_base64=$(echo -n "$filename" | base64)
file_size=$(du -b $file_to_upload | cut -f1)
creation_headers=$(curl -X POST -s -D - \
--header "Upload-Length:$file_size" \
--header "Content-Length:0" \
--header "Tus-Resumable:1.0.0" \
--header "Upload-Metadata:filename $filename_base64" \
"$endpoint" -o /dev/null)
echo "Response header: $creation_headers"
if [ -z "$(echo "$creation_headers" | grep "HTTP/1.1 201 Created")" ]
then
echo "Failed to create upload"
echo "Response header: $creation_headers"
exit 1
fi
upload_url="$(echo "$creation_headers" | grep "Location:" | cut -d " " -f2)"
upload_url=${upload_url%$'\r'}
echo "upload url: $upload_url"
upload_headers=$(curl -X PATCH -s -D - \
--header "Upload-Offset:0" \
--header "Content-Length:$file_size" \
--header "Tus-Resumable:1.0.0" \
--header "Content-Type:application/offset+octet-stream" \
"$upload_url" --data-binary "@$file_to_upload" -o /dev/null)
if [ -z "$(echo "$upload_headers" | grep "HTTP/1.1 204 No Content")" ]
then
echo "Failed to upload file"
echo "Response header: $upload_headers"
exit 1
fi
echo "Succesfully uploaded file to $upload_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment