Hello All,

Today, I'll talk about how to upload the file to Google Drive and generate a downloadable file link through .Internet.

I am using VB.Net to do this. Create a console project or a windows form project in Visual Studio.

In that location is an important stride to undertake, and that is to acquire the access of the Google Drive API through your Google Business relationship. Below is the link that will guide y'all through the entire procedure of enabling the Drive UI API

How to Enable the Drive Platform

Make sure, that at the cease yous should have three details with you, which is required in the tutorial:

  1. Client ID
  2. Client Surreptitious
  3. Project Name

GDRIVE.png

Next, make sure, you have the Google Drive API v3 with you if not, you lot can use Nuget to install the API for yous.

Link: Nuget: Google Drive API V3

Through Package Manager Console: Install-Package Google.Apis.Drive.v3

Below is the listing of MIME types, that can be used with Google Drive API. Please keep this handy for the rest of the tutorial:

File Type MIME
xls application/vnd.ms-excel
xlsx awarding/vnd.openxmlformats-officedocument.spreadsheetml.canvas
xml text/xml
ods awarding/vnd.haven.opendocument.spreadsheet
csv text/plain
tmpl text/plain
pdf application/pdf
php application/x-httpd-php
jpg image/jpeg
png image/png
gif prototype/gif
bmp image/bmp
txt text/evidently
doc application/msword
js text/js
swf application/10-shockwave-flash
mp3 audio/mpeg
zip application/goose egg
rar application/rar
tar application/tar
arj awarding/arj
cab awarding/cab
html text/html
htm text/html
default application/octet-stream
folder application/vnd.google-apps.folder

After you ensure, that the API V3 package is deployed correctly, write the below import statements.

Imports Google.Apis.Auth
Imports Google.Apis.Download
Imports Google.Apis.Drive.v3
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports System.Threading
Imports Google.Apis.Drive.v3.Data
Imports Google.Apis.Upload

Declare a Bulldoze Service Variable

Private moService Every bit DriveService = New DriveService

TIP: Always re-initialize the Bulldoze Service variable, before yous upload. It took me a while to realise this 🙂

Next, create a method past the proper noun CreateService, which will hold your Google Bulldoze Client IDs and connect to your Project Name. You can copy the contents as information technology is, and reply the place holders, with your Client ID, Customer Hole-and-corner and Project Names respectively.

Private Sub CreateService()

Dim ClientId = "YOUR CLIENT ID HERE"
Dim ClientSecret = "YOUR Client SECRET HERE"

Dim MyUserCredential Equally UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "user", CancellationToken.None).Event
moService = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "YOUR Projection NAME Hither"})

End Sub

Next, nosotros will be using a PDF file to upload into the drive, you can copy the beneath snippet into a method or a function, every bit you feel appropriate.

' Checking if the Service is notwithstanding alive, if not create the service once again.
If moService.ApplicationName <> "YOUR Projection NAME" So CreateService()

Dim oGDriveFile Equally New File()

oGDriveFile.Proper noun =  "" ' Set your File Proper noun hither.
oGDriveFile.Description =  "" ' Set a meaningful description, I had fix the aforementioned proper name equally my project proper noun
oGDriveFile.MimeType = "application/pdf" ' You must gear up your MIME type carefully. Refer to the MIME table above

Dim bArrByteArray Equally Byte() = System.IO.File.ReadAllBytes("") ' Your File Path from where you would desire to upload from.
Dim oStream As New System.IO.MemoryStream(bArrByteArray)

Dim oUploadRequest Equally FilesResource.CreateMediaUpload

oUploadRequest = moService.Files.Create(oGDriveFile, oStream, oGDriveFile.MimeType)

oUploadRequest.Fields = "id"
oUploadRequest.Alt = FilesResource.CreateMediaUpload.AltEnum.Json

oUploadRequest.Upload() ' <- The file upload happens here.

Dim oFile As File = oUploadRequest.ResponseBody

' Setting this permission will let anyone having the link to directly download the file.
' Google Bulldoze, will non testify whatever login page to the user, while attempting to download the file.
' The below two blocks are imperative.
Dim permission As New Google.Apis.Drive.v3.Information.Permission()
permission.Type = "anyone"
permission.Role = "reader"
permission.AllowFileDiscovery = Truthful

Dim request As PermissionsResource.CreateRequest = moService.Permissions.Create(permission, oFile.Id)
request.Fields = "id"
request.Execute()

If Non IsNothing(oFile) Then

sViewLink = " https://drive.google.com/uc?id=&#8221; & oFile.Id  ' This is the link, that will be used to download the file.

Else
Cursor.Electric current = Cursors.Default
MessageBox.Show("Unable to contact Google Drive. Check your Connectedness.", "Championship", MessageBoxButtons.OK, MessageBoxIcon.Fault, MessageBoxDefaultButton.Button1)
Leave Sub
Stop If

And, that is it, your file is supposed to exist uploaded to Google Drive, and a link will be available for whatsoever ane to download. You can restrict the download of the file, past providing the emailaddress holding of the file in the permission object. It seems to work just for the Google Email IDs only.

And then, if y'all have any concerns or doubts on this tutorial, don't hesitate to contact me through eMail or by posting a comment on this blog.

Y'all tin can download the test file, using the beneath link.

DOWNLOAD THE Test FILE HERE