Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Last active December 25, 2023 15:17
Show Gist options
  • Save jrichardsz/466964e3d90b2df60f870798db66f774 to your computer and use it in GitHub Desktop.
Save jrichardsz/466964e3d90b2df60f870798db66f774 to your computer and use it in GitHub Desktop.
file services
#!/bin/bash
# Define your variables
CLIENT_ID="<>"
CLIENT_SECRET="<>"
TENANT_ID="<>"
# Define the folder where your Jenkins instance stores the files
SOURCE_FOLDER="/Users/<>/Desktop/TEST"
# Define the folder path in OneDrive where you want to upload the files
ONEDRIVE_FOLDER_PATH=""
# Authenticate and obtain an access token
TOKEN_RESPONSE=$(curl -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/token" \
-d "client_id=$CLIENT_ID" \
-d "client_secret=$CLIENT_SECRET" \
-d "grant_type=client_credentials" \
-d "scope=https://graph.microsoft.com/.default")
# Extract the access token from the JSON response
ACCESS_TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.access_token')
if [ -z "$ACCESS_TOKEN" ]; then
echo "Failed to obtain an access token."
exit 1
fi
# Loop through files in the source folder and upload them to OneDrive
for FILE in "$SOURCE_FOLDER"; do
FILE_NAME=$(basename "$FILE")
ONEDRIVE_API_URL="https://graph.microsoft.com/v1.0/me/drive/root:$ONEDRIVE_FOLDER_PATH/$FILE_NAME:/content"
ONEDRIVE_API_URL="https://$TENANT_ID.sharepoint.com/CRM/_api/v2.0/drive/root:$ONEDRIVE_FOLDER_PATH/$FILE_NAME:/content"
# Calculate the content length of the file
CONTENT_LENGTH=$(wc -c < "$FILE")
# Use curl to upload the file to OneDrive with the "Content-Length" header
UPLOAD_RESPONSE=$(curl -X PUT -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Length: $CONTENT_LENGTH" --upload-file "$FILE" "$ONEDRIVE_API_URL")
if [ $? -eq 0 ]; then
echo "File '$FILE_NAME' uploaded to OneDrive successfully."
else
echo "Failed to upload file '$FILE_NAME' to OneDrive."
fi
done
# Exit with success
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment