1
0

bulk_upload.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. set -eu
  3. # bulk_upload.sh <nb-of-files> <size-of-files>
  4. KB=${KB:-100}
  5. MB=${MB:-$((KB*1000))}
  6. NB=$1
  7. SIZE=$2
  8. CONCURRENCY=${CONCURRENCY:-1}
  9. BANDWIDTH=${BANDWIDTH:-$((100*MB/CONCURRENCY))}
  10. USER="admin"
  11. PASS="password"
  12. SERVER="nextcloud.test"
  13. UPLOAD_PATH="/tmp/bulk_upload_request_$(openssl rand --hex 8).txt"
  14. BOUNDARY="boundary_$(openssl rand --hex 8)"
  15. LOCAL_FOLDER="/tmp/bulk_upload/${BOUNDARY}_${NB}_${SIZE}"
  16. REMOTE_FOLDER="/bulk_upload/${BOUNDARY}_${NB}_${SIZE}"
  17. mkdir --parent "$LOCAL_FOLDER"
  18. for ((i=1; i<="$NB"; i++))
  19. do
  20. file_name=$(openssl rand --hex 8)
  21. file_local_path="$LOCAL_FOLDER/$file_name.txt"
  22. file_remote_path="$REMOTE_FOLDER/$file_name.txt"
  23. head -c "$SIZE" /dev/urandom > "$file_local_path"
  24. file_mtime=$(stat -c %Y "$file_local_path")
  25. file_hash=$(md5sum "$file_local_path" | awk '{ print $1 }')
  26. file_size=$(du -sb "$file_local_path" | awk '{ print $1 }')
  27. {
  28. echo -en "--$BOUNDARY\r\n"
  29. # echo -en "Content-ID: $file_name\r\n"
  30. echo -en "X-File-Path: $file_remote_path\r\n"
  31. echo -en "X-OC-Mtime: $file_mtime\r\n"
  32. # echo -en "X-File-Id: $file_id\r\n"
  33. echo -en "X-File-Md5: $file_hash\r\n"
  34. echo -en "Content-Length: $file_size\r\n"
  35. echo -en "\r\n" >> "$UPLOAD_PATH"
  36. cat "$file_local_path"
  37. echo -en "\r\n" >> "$UPLOAD_PATH"
  38. } >> "$UPLOAD_PATH"
  39. done
  40. echo -en "--$BOUNDARY--\r\n" >> "$UPLOAD_PATH"
  41. echo "Creating folder /bulk_upload"
  42. curl \
  43. -X MKCOL \
  44. -k \
  45. "https://$USER:$PASS@$SERVER/remote.php/dav/files/$USER/bulk_upload" > /dev/null
  46. echo "Creating folder $REMOTE_FOLDER"
  47. curl \
  48. -X MKCOL \
  49. -k \
  50. "https://$USER:$PASS@$SERVER/remote.php/dav/files/$USER/$REMOTE_FOLDER"
  51. echo "Uploading $NB files with total size: $(du -sh "$UPLOAD_PATH" | cut -d ' ' -f1)"
  52. echo "Local file is: $UPLOAD_PATH"
  53. curl \
  54. -X POST \
  55. -k \
  56. --progress-bar \
  57. --limit-rate "${BANDWIDTH}k" \
  58. --cookie "XDEBUG_PROFILE=true;path=/;" \
  59. -H "Content-Type: multipart/related; boundary=$BOUNDARY" \
  60. --data-binary "@$UPLOAD_PATH" \
  61. "https://$USER:$PASS@$SERVER/remote.php/dav/bulk"
  62. rm -rf "${LOCAL_FOLDER:?}"
  63. rm "$UPLOAD_PATH"