我想使用C#将一组文件上传到不同项目的Google云存储中,但是,我的一些文件已成功上传,我的一些文件抛出异常说:“Google Cloud Storage InsertMediaUpload Service BaseUri ArgumentNullException”。有谁知道导致此错误的原因,以及如何解决? 这是我正在使用的代码:
static void main(string[] args)
{
string[] paths = Directory.GetFiles(@"C:\Users\Downloads\csv");
List<Task> tasks = new List<Task>();
var sw = Stopwatch.StartNew();
foreach (string path in paths)
{
tasks.Add(Task.Factory.StartNew(() =>
{
StorageUploader st = new StorageUploader();
string filename = Path.GetFileName(path);
int i = Convert.ToInt32(filename.Substring(8,1));
string projectID = "abc" + i.ToString("00");
st.StorageUpload(path,pojectID,credentials[i]);
}));
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Stop();
Console.WriteLine("Done!");
Console.Read();
}
}
StorageUpload中的(file,projectID,Credential):
public void StorageUpload(string filePath,string project,StorageService aService)
{
var filename = Path.GetFileName(filePath);
var projectName = project;
var num = projectName.Substring(14,1);
var bucketToUpload = "myBucket" + num;
var newObject = new Google.Apis.Storage.v1.Data.Object()
{
Bucket = bucketToUpload,
Name = filename
};
FileStream fileStream = null;
try
{
fileStream = new FileStream(filePath, FileMode.Open);
fileStream.Position = 0;
var uploadRequest = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(aService, newObject,
bucketToUpload, fileStream, "application/vnd.ms-excel");
uploadRequest.UploadAsync();
var stats = uploadRequest.GetProgress().Status;
while (true)
{
if (uploadRequest.GetProgress().Status.ToString() == "Completed")
{
return;
}
else if (uploadRequest.GetProgress().Status.ToString() == "Failed")
{
Console.WriteLine(filename + uploadRequest.GetProgress().Exception.ToString());
}
Console.WriteLine("Wait");
System.Threading.Thread.Sleep(10000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (fileStream != null)
{
fileStream.Dispose();
}
}
}
提前多多感谢。