0

私はちょうど興味がありました.ローカルキャッシュなしで、C#で直接ネットワーク転送を行うことは可能ですか.

たとえば、GoogleDrive ファイルを表す応答ストリームと、別の GoogleDrive アカウントにファイルをアップロードするための要求ストリームがあります。

その時点で、ファイルをローカル PC にダウンロードし、次にそれを Google ドライブにアップロードできます。しかし、あるGoogleドライブから別のGoogleドライブに直接アップロードすることは可能ですか、少なくとも、完全なダウンロードが完了する前にアップロードを開始することはできますか.

感謝

4

1 に答える 1

0

はい、できます。Google ドライブ API を使用すると、ファイルをストリームにダウンロードし、それをメモリに保持して、ログイン後に別の Google ドライブ アカウントにアップロードできます。

最初のアカウントでトークンを取得し、ストリームに保持するファイルをダウンロードします。

次に、他の Google ドライブ アカウントで認証し、ストリームを使用してファイルをアップロードします。

PS: 2 番目のドライブ アカウントにファイルを挿入する場合、ディスクからファイルを読み取る byte[] 配列を取得する代わりに、メモリ内にあるストリームからバイト配列を取得します。

ファイルのダウンロード例:

public static System.IO.Stream DownloadFile(
      IAuthenticator authenticator, File file) {
    if (!String.IsNullOrEmpty(file.DownloadUrl)) {
      try {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
            new Uri(file.DownloadUrl));
        authenticator.ApplyAuthenticationToRequest(request);
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK) {
          return response.GetResponseStream();
        } else {
          Console.WriteLine(
              "An error occurred: " + response.StatusDescription);
          return null;
        }
      } catch (Exception e) {
        Console.WriteLine("An error occurred: " + e.Message);
        return null;
      }
    } else {
      // The file doesn't have any content stored on Drive.
      return null;
    }

ファイルの挿入例:

private static File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) {
    // File's metadata.
    File body = new File();
    body.Title = title;
    body.Description = description;
    body.MimeType = mimeType;

    // Set the parent folder.
    if (!String.IsNullOrEmpty(parentId)) {
      body.Parents = new List<ParentReference>()
          {new ParentReference() {Id = parentId}};
    }

    // File's content.
    byte[] byteArray = System.IO.File.ReadAllBytes(filename);
    MemoryStream stream = new MemoryStream(byteArray);

    try {
      FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
      request.Upload();

      File file = request.ResponseBody;

      // Uncomment the following line to print the File ID.
      // Console.WriteLine("File ID: " + file.Id);

      return file;
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }
于 2012-12-03T21:07:41.900 に答える