bulk_upload.sh 2.1 KB

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