|
| 1 | +# Check that we're in a bash shell |
| 2 | +if [[ $SHELL != *"bash"* ]]; then |
| 3 | + echo "PROBLEM: Run these scripts from within the bash shell." |
| 4 | +fi |
| 5 | + |
| 6 | +# Obtain your OAuth token |
| 7 | +ACCESS_TOKEN=$(cat config/ds_access_token.txt) |
| 8 | + |
| 9 | +# Set up variables for full code example |
| 10 | +account_id=$(cat config/API_ACCOUNT_ID) |
| 11 | +base_path="https://api-d.docusign.com/v1" |
| 12 | + |
| 13 | +request_data=$(mktemp /tmp/request-nav-003.XXXXXX) |
| 14 | +response=$(mktemp /tmp/response-nav-003.XXXXXX) |
| 15 | + |
| 16 | +#ds-snippet-start:Navigator3Step2 |
| 17 | +printf \ |
| 18 | +'{ |
| 19 | + "job_name": "Example bulk upload job", |
| 20 | + "expected_number_of_docs": 5, |
| 21 | + "language": "en-US" |
| 22 | +}' >> $request_data |
| 23 | + |
| 24 | +curl --request POST ${base_path}/accounts/${account_id}/upload/jobs \ |
| 25 | + --header "Authorization: Bearer ${ACCESS_TOKEN}" \ |
| 26 | + --header "Accept: application/json" \ |
| 27 | + --header "Content-Type: application/json" \ |
| 28 | + --data-binary @${request_data} \ |
| 29 | + --output $response |
| 30 | + |
| 31 | +# Extract job ID and upload URLs for each document |
| 32 | +job_id=$(cat "$response" | grep -o '"id":"[^"]*' | head -1 | cut -d'"' -f4) |
| 33 | +upload_urls=$(cat $response | grep -o '"upload_document":"[^"]*' | cut -d'"' -f4) |
| 34 | +#ds-snippet-end:Navigator3Step2 |
| 35 | + |
| 36 | +echo "Created upload job with ID: $job_id" |
| 37 | + |
| 38 | +read -p "Press Enter to upload documents for this job" |
| 39 | + |
| 40 | +echo "Uploading documents..." |
| 41 | +echo "" |
| 42 | + |
| 43 | +#ds-snippet-start:Navigator3Step3 |
| 44 | +# Array of demo documents to upload |
| 45 | +declare -a demo_files=( |
| 46 | + "demo_documents/World_Wide_Corp_Battle_Plan_Trafalgar.docx" |
| 47 | + "demo_documents/World_Wide_Corp_lorem.pdf" |
| 48 | + "demo_documents/doc_1.html" |
| 49 | + "demo_documents/Welcome.txt" |
| 50 | + "demo_documents/Id.jpg" |
| 51 | +) |
| 52 | + |
| 53 | +# Convert to array (cross-platform: works on macOS and Linux) |
| 54 | +upload_urls_array=() |
| 55 | +while IFS= read -r url; do |
| 56 | + upload_urls_array+=("$url") |
| 57 | +done <<<"$upload_urls" |
| 58 | + |
| 59 | +# Upload each file |
| 60 | +for i in "${!demo_files[@]}"; do |
| 61 | + file_path="${demo_files[$i]}" |
| 62 | + upload_url="${upload_urls_array[$i]}" |
| 63 | + |
| 64 | + # Extract filename from path |
| 65 | + filename=$(basename "$file_path") |
| 66 | + |
| 67 | + if [[ ! -f "$file_path" ]]; then |
| 68 | + echo "Skipping $filename - file not found: $file_path" |
| 69 | + continue |
| 70 | + fi |
| 71 | + |
| 72 | + if [[ -z "$upload_url" ]]; then |
| 73 | + echo "Skipping $filename - no upload URL found" |
| 74 | + continue |
| 75 | + fi |
| 76 | + |
| 77 | + case "$filename" in |
| 78 | + *.docx) |
| 79 | + content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document" |
| 80 | + ;; |
| 81 | + *.pdf) |
| 82 | + content_type="application/pdf" |
| 83 | + ;; |
| 84 | + *.html) |
| 85 | + content_type="text/html" |
| 86 | + ;; |
| 87 | + *.txt) |
| 88 | + content_type="text/plain" |
| 89 | + ;; |
| 90 | + *.jpg|*.jpeg) |
| 91 | + content_type="image/jpeg" |
| 92 | + ;; |
| 93 | + *) |
| 94 | + content_type="application/octet-stream" |
| 95 | + ;; |
| 96 | + esac |
| 97 | + |
| 98 | + echo "Uploading $filename..." |
| 99 | + |
| 100 | + |
| 101 | + # Make PUT request with binary file content and required headers. |
| 102 | + curl --request PUT "$upload_url" \ |
| 103 | + --header "x-ms-blob-type: BlockBlob" \ |
| 104 | + --header "x-ms-meta-filename: ${filename}" \ |
| 105 | + --header "Content-Type: ${content_type}" \ |
| 106 | + --data-binary @"$file_path" |
| 107 | +done |
| 108 | +#ds-snippet-end:Navigator3Step3 |
| 109 | + |
| 110 | +echo "" |
| 111 | +read -p "The documents have been uploaded. Press Enter to update the job status." |
| 112 | + |
| 113 | +#ds-snippet-start:Navigator3Step4 |
| 114 | +curl --request POST "${base_path}/accounts/${account_id}/upload/jobs/${job_id}/actions/complete" \ |
| 115 | + --header "Authorization: Bearer ${ACCESS_TOKEN}" \ |
| 116 | + --header "Accept: application/json" \ |
| 117 | + --header "Content-Type: application/json" \ |
| 118 | + --output "$response" |
| 119 | +#ds-snippet-end:Navigator3Step4 |
| 120 | + |
| 121 | +echo "" |
| 122 | +echo "Bulk upload job has been completed. Response:" |
| 123 | +echo "" |
| 124 | +cat "$response" |
| 125 | +echo "" |
| 126 | + |
| 127 | +rm -f "$response" |
| 128 | +rm -f "$request_data" |
| 129 | + |
| 130 | +echo "" |
| 131 | +echo "Done." |
| 132 | +echo "" |
0 commit comments