Cloud Storage Access through API - ASP.NET MVC 5 - C# (Part-3)
--------------------------------------------------------------------------------------------------------
Source Code: https://goo.gl/iw94UW
Blog: https://everyday-be-coding.blogspot.c...
----------------------------------------------------------------------------------------------------------
Suggested Video link: 1) • Google Drive API v3 - Enable & Get Client ...
2) • Google Drive API: Uploading, Viewing, Down...
-----------------------------------------------------------------------
Twitter : / everydaycoding
-----------------------------------------------------------------------
Facebook: / everydaybecoding
-----------------------------------------------------------------------------------------------
Google Drive API v2 & v3 - Key Concepts are:
Upload a file or create a folder in google drive :
1) Google drive create a unique ID for each & every item in the drive.
2) We create & upload same name folder and files in multiple time in the same location in google drive. Google drive is identified this item by the unique IDs.
So, Next question in our mind how to identifies a file or folder in google drives.
A) How to Identifies a file or folder in Google Drive?
1) A File has size property but folder has no size property.
2) Difference between a File MIME Type and Folder MIME Type.
3) A folder contains - a) may be empty
b) one/more files or folders.
A File has raw information.
In this tutorials, I use file size property of Google drive API.
B) Create a folder in Google Drive using API?
When we creating a folder into the google drive, just like a file, google drive provide a specific unique folder id for the newly created folder. But In this case, we need to provide MIME type for folder "application/vnd.google-apps.folder".
Please see this code:
public static void CreateFolder(string FolderName)
{
Google.Apis.Drive.v3.DriveService service = GetService_v3();
Google.Apis.Drive.v3.Data.File FileMetaData = new Google.Apis.Drive.v3.Data.File();
FileMetaData.Name = FolderName;
FileMetaData.MimeType = "application/vnd.google-apps.folder";
Google.Apis.Drive.v3.FilesResource.CreateRequest request;
request = service.Files.Create(FileMetaData);
request.Fields = "id";
var file = request.Execute();
}
C) Inserting a file in a folder using Google Drive API?
Inserting a file in particular folder, specify the correct folder id in the parent property of the google drive file.
Please see this code:
public static void FileUploadInFolder(string folderId, HttpPostedFileBase file)
{
if (file != null)
{
Google.Apis.Drive.v3.DriveService service = GetService_v3();
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
var FileMetaData = new Google.Apis.Drive.v3.Data.File()
{
Name = Path.GetFileName(file.FileName),
MimeType = MimeMapping.GetMimeMapping(path),
Parents = new List[string]
{
folderId
}
};
Google.Apis.Drive.v3.FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}
var file1 = request.ResponseBody;
}
}
#EverydayBeCoding
On this page of the site you can watch the video online Google Drive API : Create folder, Upload file to folder, Show folder Content. with a duration of hours minute second in good quality, which was uploaded by the user Everyday Be Coding 16 October 2017, share the link with friends and acquaintances, this video has already been watched 22,176 times on youtube and it was liked by 114 viewers. Enjoy your viewing!