When trying to generate the public url of any folder using MegaApiClient then API taking too longer time and not responding anything.
Is there any settings needed to be done before this ? As i am able to generate the link from Mega.IO UI
// Initialize MegaApiClient
var client = new MegaApiClient();
try
{
client.Login(email, password);
var rootNode = await client.GetNodesAsync();
var seasonFolder = rootNode.FirstOrDefault(n => n.Type == NodeType.Directory && n.Name == eventSeason);
var eventFolder = await client.GetNodesAsync(seasonFolder);
var studioFolder = client.GetNodes(eventFolder.Where(x=>x.Name == eventName).FirstOrDefault());
List<(string FileName, string Url, string ThumbnailUrl, string DownloadUrl, bool isFolder)> lstStudioFolder = new List<(string FileName, string Url, string ThumbnailUrl, string DownloadUrl, bool isFolder)>();
foreach (var tmpStudio in studioFolder)
{
// Generate a public link for the folder
var publicLink = client.GetDownloadLink(tmpStudio);
lstStudioFolder.Add((tmpStudio.Name, publicLink.AbsoluteUri, string.Empty, string.Empty, true));
}
return View(lstStudioFolder);
}
finally
{
await client.LogoutAsync();
}
Mitesh MachhiPosted May 24, 2024, 7:07 AM
Thank you @vikas singh.
I have tried with your given solution but still result is the same. Taking too much time when GetDownloadLinkAsync() called.
Vikas SinghPosted May 24, 2024, 4:18 AM
It seems like there are a few potential issues that could be causing the MegaApiClient to take too long or not respond when trying to generate public URLs for folders. Here are some steps and considerations that might help resolve the problem:
Ensure Asynchronous Methods are Awaited Correctly
Increase Timeout:
Check for Throttling or Rate Limits:
Proper Exception Handling
Here’s a revised version of your code with asynchronous calls and better handling:
// Initialize MegaApiClient
var client = new MegaApiClient();
try
{
await client.LoginAsync(email, password); // Use LoginAsync instead of Login
var rootNode = await client.GetNodesAsync();
var seasonFolder = rootNode.FirstOrDefault(n => n.Type == NodeType.Directory && n.Name == eventSeason);
if (seasonFolder == null)
{
throw new Exception($"Season folder '{eventSeason}' not found.");
}
var eventFolderNodes = await client.GetNodesAsync(seasonFolder);
var eventFolder = eventFolderNodes.FirstOrDefault(x => x.Name == eventName && x.Type == NodeType.Directory);
if (eventFolder == null)
{
throw new Exception($"Event folder '{eventName}' not found.");
}
var studioFolderNodes = await client.GetNodesAsync(eventFolder);
List<(string FileName, string Url, string ThumbnailUrl, string DownloadUrl, bool isFolder)> lstStudioFolder = new List<(string FileName, string Url, string ThumbnailUrl, string DownloadUrl, bool isFolder)>();
foreach (var tmpStudio in studioFolderNodes)
{
if (tmpStudio.Type == NodeType.Directory)
{
// Generate a public link for the folder
var publicLink = await client.GetDownloadLinkAsync(tmpStudio);
lstStudioFolder.Add((tmpStudio.Name, publicLink.AbsoluteUri, string.Empty, string.Empty, true));
}
}
return View(lstStudioFolder);
}
catch (Exception ex)
{
// Log the exception
Console.WriteLine($"An error occurred: {ex.Message}");
throw;
}
finally
{
await client.LogoutAsync(); // Use LogoutAsync instead of Logout
}
These changes should help in properly managing asynchronous operations and improving the reliability of your MegaApiClient usage.