Skip to content

Instantly share code, notes, and snippets.

@k0z4c
Forked from ConnorGriffin/GDrive.Upload.ps1
Created February 9, 2024 22:10
Show Gist options
  • Select an option

  • Save k0z4c/2f26ee34e7719011afd9e8269cb70fbb to your computer and use it in GitHub Desktop.

Select an option

Save k0z4c/2f26ee34e7719011afd9e8269cb70fbb to your computer and use it in GitHub Desktop.

Revisions

  1. @ConnorGriffin ConnorGriffin revised this gist Mar 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GDrive.Upload.ps1
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ $uploadMetadata = @{
    name = $sourceItem.Name
    description = $sourceItem.VersionInfo.FileDescription
    #parents = @('teamDriveid or folderId') # Include to upload to a specific folder
    #teamDriveId = ‘teamDriveId’ # Include to upload to a specific teamdrive
    #teamDriveId = ‘teamDriveId’ # Include to upload to a specific teamdrive
    }

    # Set the upload body
  2. @ConnorGriffin ConnorGriffin revised this gist Mar 21, 2018. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions GDrive.Upload.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    # Set the Google Auth parameters. Fill in your RefreshToken, ClientID, and ClientSecret
    $params = @{
    Uri = 'https://accounts.google.com/o/oauth2/token'
    Body = @(
    "refresh_token=$RefreshToken", # Replace $RefreshToken with your refresh token
    "client_id=$ClientID", # Replace $ClientID with your client ID
    "client_secret=$ClientSecret", # Replace $ClientSecret with your client secret
    "grant_type=refresh_token"
    ) -join '&'
    Method = 'Post'
    ContentType = 'application/x-www-form-urlencoded'
    }
    $accessToken = (Invoke-RestMethod @params).access_token

    # Change this to the file you want to upload
    $SourceFile = 'C:\Path\To\File'

  3. @ConnorGriffin ConnorGriffin revised this gist Mar 21, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion GDrive.Download.ps1
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    #
  4. @ConnorGriffin ConnorGriffin revised this gist Mar 21, 2018. No changes.
  5. @ConnorGriffin ConnorGriffin revised this gist Mar 21, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions GDrive.Download.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #
  6. @ConnorGriffin ConnorGriffin created this gist Mar 21, 2018.
    44 changes: 44 additions & 0 deletions GDrive.Upload.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # Change this to the file you want to upload
    $SourceFile = 'C:\Path\To\File'

    # Get the source file contents and details, encode in base64
    $sourceItem = Get-Item $sourceFile
    $sourceBase64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($sourceItem.FullName))
    $sourceMime = [System.Web.MimeMapping]::GetMimeMapping($sourceItem.FullName)

    # If uploading to a Team Drive, set this to 'true'
    $supportsTeamDrives = 'false'

    # Set the file metadata
    $uploadMetadata = @{
    originalFilename = $sourceItem.Name
    name = $sourceItem.Name
    description = $sourceItem.VersionInfo.FileDescription
    #parents = @('teamDriveid or folderId') # Include to upload to a specific folder
    #teamDriveId = ‘teamDriveId’ # Include to upload to a specific teamdrive
    }

    # Set the upload body
    $uploadBody = @"
    --boundary
    Content-Type: application/json; charset=UTF-8
    $($uploadMetadata | ConvertTo-Json)
    --boundary
    Content-Transfer-Encoding: base64
    Content-Type: $sourceMime
    $sourceBase64
    --boundary--
    "@

    # Set the upload headers
    $uploadHeaders = @{
    "Authorization" = "Bearer $accessToken"
    "Content-Type" = 'multipart/related; boundary=boundary'
    "Content-Length" = $uploadBody.Length
    }

    # Perform the upload
    $response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsTeamDrives=$supportsTeamDrives" -Method Post -Headers $uploadHeaders -Body $uploadBody