I am able to ‘create’ a file on google-Drive using the below code however I have noticed the files have 0 bytes. I am using a Service Account which is Editor. I am using the Radzen Upload Control.
<RadzenUpload Url="upload/single" Progress=@(args => TrackProgress(args, "Single file upload")) class="w-100" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "select file" }})"> </RadzenUpload>
The controller used to receive the file
[DisableRequestSizeLimit]
public partial class UploadController : Controller
{
private readonly IWebHostEnvironment environment;
private readonly GoogleDriveService _googleDriveService;
public UploadController(IWebHostEnvironment environment, GoogleDriveService googleDriveService)
{
this.environment = environment;
_googleDriveService = googleDriveService;
}
// Single file upload
[HttpPost("upload/volunteer")]
public IActionResult Volunteer(IFormFile file)
{
try
{
_googleDriveService.UploadFile(file, this.environment);
return StatusCode(200, file.FileName);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}
My Service for Working with Google Drive
public string UploadFile(IFormFile File, IWebHostEnvironment environment)
{
string uploadedFileId = "";
string RootFolderId = "xxxxxxxxxxxxxxxxxxxxxxxx"; \Target Folder
var service = Auth();
try
{
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(Path.Combine(environment.WebRootPath, File.FileName), FileMode.Create))
{
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
{
Name = File.FileName,
Parents = new List<string> { RootFolderId }
};
request = service.Files.Create(fileMetadata, memoryStream, File.ContentType);
request.Fields = "id";
request.SupportsTeamDrives = true;
request.SupportsAllDrives = true;
request.Upload();
}
}
Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
uploadedFileId = file.Id;
return uploadedFileId;
}
catch (Exception ex)
{
var x = ex;
}
return uploadedFileId;
}
public DriveService Auth()
{
var credential = GoogleCredential.FromFile(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", PathToServiceAccountKeyFile))
.CreateScoped(DriveService.ScopeConstants.Drive);
// Create the Drive service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
return service;
}
My Auth code for the API
public DriveService Auth()
{
// Load the Service account credentials and define the scope of its access.
var credential = GoogleCredential.FromFile(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", PathToServiceAccountKeyFile))
.CreateScoped(DriveService.ScopeConstants.Drive);
// Create the Drive service.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
return service;
}
The upload Looks as though it works, a file is created in Google Drive at the right location, there is just no Content (0 bytes)