Hi,
I'm saving a lot of log files from parallel process and running into "too many operations" exceptions occasionally (I already rate-limited them to 1 per second start rate to deal with a rate limit issue) and saw mention of using batch upload to address this, so I'm trying to do so but I'm missing something somewhere after reading all the doco.
My code builds, runs, but at the end I have no files. At first it was crashing if the file didn't already exist, which I thought was odd, and my code ran when I pre-made the files, but then they were still empty at the end. I deleted them again and this time the code ran, but no files. I thought at first maybe I had something wrong with the offset - e.g. maybe wasn't uploading files because I'd prematurely set the offset to the end, so I set it to 0 instead (you'll see that in the following code), but still not working. Wondering if you could look at my code and see if you can spot what I'm doing wrong?
First my custom class for this
public class FileToBeSaved
{
public string Content {get;set;}
public string Path {get;set;}
}This is the method I've written...
public async Task<string> WriteBatchOfFilesAsync(List<FileToBeSaved> Files)
{
string asyncJobId=string.Empty;
string statusString=string.Empty;
List<UploadSessionFinishArg> entries=new List<UploadSessionFinishArg>();
foreach (FileToBeSaved file in Files) {
string sessionId=(await DxClient.Files.UploadSessionStartAsync(close:true,body:StringToStream(file.Content))).SessionId;
UploadSessionFinishArg finishArg=new UploadSessionFinishArg();
//ulong offset=(await DxClient.Files.GetMetadataAsync(new GetMetadataArg(file.Path))).AsFile.Size;
ulong offset=0;
UploadSessionCursor cursor=new UploadSessionCursor(sessionId,offset);
CommitInfo commit=new CommitInfo(file.Path,WriteMode.Overwrite.Instance,false,DateTime.Now);
entries.Add(new UploadSessionFinishArg(cursor,commit));
}
UploadSessionFinishBatchLaunch result=null;
UploadSessionFinishBatchJobStatus status=null;
int delay=5_000;
int i=0;
bool complete=false;
try {
result=await DxClient.Files.UploadSessionFinishBatchAsync(entries);
while (!complete) {
if (result.IsComplete) {
statusString="IsComplete";
complete=true;
} else {
// Is either is progress or "IsOther"
if (result.IsOther) {
statusString="IsOther";
complete=true;
} else {
asyncJobId=result.AsAsyncJobId.Value;
statusString="InProgress";
while (statusString=="InProgress") {
await Task.Delay(delay);
status=await DxClient.Files.UploadSessionFinishBatchCheckAsync(new PollArg(asyncJobId));
if (status.IsComplete) {
statusString="IsComplete";
complete=true;
}
}
}
}
}
}
catch (Exception ex) {
statusString=$"EXCEPTION FOR {nameof(WriteBatchOfFilesAsync)} is {ex.Message}\r\n";
DebugUtil.ConsoleWrite(debugOn,$"EXCEPTION IN SAVEFROMURLASYNC IS {ex.Message}");
}
return statusString;
}And this is the code I'm using to call it to test it...
List<FileToBeSaved> filesToBeSaved=new List<FileToBeSaved>();
FileToBeSaved file1=new FileToBeSaved();
file1.Content="This is file 1";
file1.Path=$"{FolderPath}File1.txt";
filesToBeSaved.Add(file1);
FileToBeSaved file2=new FileToBeSaved();
file2.Content="This is file 2";
file2.Path=$"{FolderPath}File2.txt";
filesToBeSaved.Add(file2);
FileToBeSaved file3=new FileToBeSaved();
file3.Content="This is file 3";
file3.Path=$"{FolderPath}File3.txt";
filesToBeSaved.Add(file3);
FileToBeSaved file4=new FileToBeSaved();
file4.Content="This is file 4";
file4.Path=$"{FolderPath}File4.txt";
filesToBeSaved.Add(file4);
string result=await DataService.WriteBatchOfFilesAsync(filesToBeSaved);Any help would be appreciated.
thanks,
Donald.