Cloud Storage Access through API - ASP.NET MVC 5 - C# (Part-3)
--------------------------------------------------------------------------------------------------------
Source Code: [ Ссылка ]
Blog: [ Ссылка ]
----------------------------------------------------------------------------------------------------------
Suggested Video link: 1) [ Ссылка ]
2) [ Ссылка ]
-----------------------------------------------------------------------
Twitter : [ Ссылка ]
-----------------------------------------------------------------------
Facebook: [ Ссылка ]
-----------------------------------------------------------------------------------------------
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
Ещё видео!