Reputation: 1
I have been following the APS (Autodesk Platform Services)text tutorial for downloading files from an URL in Autodesk Construction Cloud (ACC), and everything works smoothly in most cases. However, I've encountered an issue when the files being downloaded contain dependencies or links to other files. In these scenarios, the downloaded files often end up corrupted or incomplete, resulting in errors when trying to open them in Revit. I'm seeking advice on how to properly handle and download Revit files with dependencies or linked files in ACC to avoid these issues.
private static void DownloadFilesFromACCS3(string folderId, string downloadPath)
{
var accessToken = APSHelper.GetAccessTokenSynchronously();
// Get the list of Revit files in the folder
List<string> revitFileUrns = APSHelper.GetRevitModelsOnFolderSynchronously(accessToken, folderId);
// Download each file
List<string> downloadedFiles = new List<string>();
if (revitFileUrns.Any())
{
foreach (string fileUrn in revitFileUrns)
{
try
{
var (downloadUrl, fileName) = GetDownloadUrlFromItemResponseS3(accessToken, fileUrn, downloadPath);
var itemResponse = client.GetAsync(downloadUrl).Result;
if (!itemResponse.IsSuccessStatusCode)
{
throw new Exception($"Failed to get item details: {itemResponse.ReasonPhrase}");
}
var itemResponseString = itemResponse.Content.ReadAsStringAsync().Result;
var itemResponseObject = JObject.Parse(itemResponseString);
string downloadS3Url = itemResponseObject["url"]?.ToString();
// Download the main file
DownloadFile(downloadS3Url, fileName, downloadPath);
downloadedFiles.Add(fileName);
}
catch (Exception ex)
{
GenerateLogFile(downloadPath, downloadedFiles, $"Failed to download file: {fileUrn}, Error: {ex}");
throw;
}
}
}
// Generate a log file
GenerateLogFile(downloadPath, downloadedFiles, "Download completed successfully.");
}
private static (string downloadUrl, string fileName) GetDownloadUrlFromItemResponseS3(string accessToken, string itemId, string downloadPath)
{
var itemDetailsUrl = $"{baseUrl}/data/v1/projects/{ProjectId}/items/{itemId}";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var itemResponse = client.GetAsync(itemDetailsUrl).Result;
if (!itemResponse.IsSuccessStatusCode)
{
throw new Exception($"Failed to get item details: {itemResponse.ReasonPhrase}");
}
var itemResponseString = itemResponse.Content.ReadAsStringAsync().Result;
var itemResponseObject = JObject.Parse(itemResponseString);
// Log the entire itemResponseObject for debugging
GenerateLogFile(downloadPath, new List<string>(), $"Full Item Details Response: {itemResponseObject}");
// Extract the download URL from the response
string downloadUrl = null;
string fileName = itemResponseObject["data"]?["attributes"]?["displayName"]?.ToString();
string bucketKey = null;
string objectId = null;
var includedArray = itemResponseObject["included"] as JArray;
if (includedArray != null)
{
foreach (var includedItem in includedArray)
{
downloadUrl = includedItem["relationships"]?["storage"]?["data"]?["id"]?.ToString();
if (!string.IsNullOrEmpty(downloadUrl))
{
string lastPart = downloadUrl.Split(':').Last();
string[] parts = lastPart.Split('/');
if (parts.Length >= 2)
{
bucketKey = parts[0];
objectId = parts[1];
}
break;
}
}
}
if (string.IsNullOrEmpty(downloadUrl))
{
throw new Exception("Download URL not found in the item response.");
}
string s3Url = $"{baseUrl}/oss/v2/buckets/{bucketKey}/objects/{objectId}/signeds3download";
return (s3Url, fileName);
}
private static void DownloadFile(string downloadUrl, string fileName, string downloadPath)
{
// Sanitize fileName to create a valid file name
string sanitizedFileName = SanitizeFileName(fileName);
string filePath = Path.Combine(downloadPath, sanitizedFileName);
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = client.GetAsync(downloadUrl).Result;
if (response.IsSuccessStatusCode)
{
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
response.Content.CopyToAsync(fs).Wait();
}
}
else
{
throw new Exception($"Failed to download file. Status code: {response.StatusCode}");
}
}
}
Upvotes: 0
Views: 146