, 구글 드라이브/다운로드 파일을 업로드하는 방법 문서가있다.
대부분의 Google API와 마찬가지로 인증을 받아야 연결할 수 있습니다. 이렇게하려면 먼저 Google 개발자 콘솔에 애플리케이션을 등록해야합니다. API에서는 항상 Google Drive API
및 Google Drive SDK
을 사용 설정해야합니다. 항상 동의서 화면 양식에 제품 이름과 이메일 주소를 추가하는 것을 잊지 마십시오.
프로젝트가 .net 4.0 이상으로 설정되어 있는지 확인하십시오.
는
download하기 위해 우리는 파일 resorce 파일 ID가 우리가 이전에 사용 된
Files.List()
명령에서이다 얻을 수있는 유일한 방법을 알 필요가 파일을 다음
NuGet 패키지
PM> Install-Package Google.Apis.Drive.v2
를 추가합니다.
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
_service.HttpClient.GetByteArrayAsync
을 사용하면 다운로드하려는 파일의 다운로드 URL을 전달할 수 있습니다. 파일이 다운로드되면 디스크에 파일을 기록하는 간단한 문제입니다.
파일을 upload으로 만들려면 디렉토리를 생성해야하며 Google에 그 파일의 mime-type
이 무엇인지 알려줄 수 있어야합니다. 나는 그걸 알아 내려고 노력하는 작은 방법이있다. 파일 이름 만 보내면됩니다. 참고 : 파일 이름이 이미있는 파일과 동일한 이름 인 경우 Google 드라이브에 파일을 업로드 할 때. Google 드라이브는 어쨌든 파일을 업로드하기 때문에 거기에 있던 파일은 업데이트되지 않고 이름이 같은 두 개의 파일로 끝납니다. 파일 이름을 기반으로하지 않고 fileId
을 기반으로 만 검사합니다. 업데이트 명령을 사용해야하는 파일을 업데이트하려면 나중에 확인할 것입니다.
public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {
if (System.IO.File.Exists(_uploadFile))
{
File body = new File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File uploaded by Diamto Drive Sample";
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List() { new ParentReference() { Id = _parent } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
return request.ResponseBody;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
else {
Console.WriteLine("File does not exist: " + _uploadFile);
return null;
}
}
나는이 자습서를 통해 갔다 그러나 여기에서 문제는/업로드 파일을 다운로드하는 데 사용됩니다 AuthorizeAsync API를 호출하여 생성이 튜토리얼 DriveService 객체입니다. 하지만 나와 함께 액세스 토큰을 가지고 있습니다 (http://www.daimto.com/google-api-and-oath2/). 액세스 토큰 만 사용하여 업로드/다운로드를 계속 진행하려면 어떻게해야합니까? – user2734779