我只是好奇,有没有可能在c#中有直接的网络传输,而不需要本地缓存。
例如,我有表示GoogleDrive文件的响应流和将文件上传到另一个GoogleDrive帐户的请求流。
在那一刻,我可以下载文件到本地pc,然后上传到google驱动器。但是有没有可能直接从一个google驱动器上传到另一个驱动器,或者至少在完全下载完成之前开始上传。
谢谢
发布于 2012-12-04 05:07:42
可以,使用Google Drive api,您可以将文件下载到流中,并将其保存在内存中,以便登录后可以将其上传到另一个google drive帐户。
您在第一个帐户上获得令牌,并下载一个文件,将其保存在流中。
THen你在其他谷歌驱动器帐户上进行身份验证,并使用流上传文件。
PS:当您在第二个驱动器帐户上插入文件时,不是让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;
}
}https://stackoverflow.com/questions/13691683
复制相似问题