Solved

C# SDK upload file

  • 1 April 2024
  • 4 replies
  • 65 views

Badge +1

Hi All,

In C# SDK Upload link description says it provide link to upload file in GKS. Does it work if CDF is hosted on Azure as well.

 

 

icon

Best answer by Everton Colling 8 April 2024, 17:40

View original

4 replies

Userlevel 2

This documentation is clearly a bit outdated. It should work fine on azure.

Badge +1

@Einar Omang I am using below code to upload the file but getting the exception.

 

    var fileCreate = new FileCreate
    {
        Name = fileName,
        DataSetId = dataSetId,
        SourceCreatedTime = unixTimeMilliSeconds,
        ExternalId = string.Concat(confModel.CompletionName, "-", Guid.NewGuid().ToString()),
    };
    var fileUploadReq = SdkClient.Files.UploadAsync(fileCreate);
    var fileUploadRes = fileUploadReq.Result;
    int retryCount = 0;
    int maxRetries = 5;
    while (retryCount < maxRetries)
    {
        try
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers.Add("Content-Type", "application/json");
                webClient.Headers.Add("Bearer", _token);
                webClient.UploadFile(fileUploadRes.UploadUrl, filePath);
            }
            break;
        }
        catch (Exception e)
        {
            retryCount++;
            if (retryCount > 4)
            {
                return false;
            }
            Console.WriteLine($"Error: {e.Message}");
            // wait for 10 seconds before retrying
            Task.Delay(TimeSpan.FromSeconds(10));
        }
    }

}

 

Userlevel 4

Hi Adarsh,

Here's a minimal example on how to upload a file (in this case equation.png) using the dotnet SDK:

1) Start by creating a file resource in CDF

var fileDto = new FileCreate {
Name = "equation.png",
Source = "equation.png",
MimeType = "image/png",
ExternalId = "dotnet-sdk-test",
};
var fileCreate = await client.Files.UploadAsync(file: fileDto);

The fileCreate object will have an UploadUrl property which you can use to upload your file.

2) Use an HttpClient instance to upload the file (in this example, I’m using the same httpClient I used to instantiate the CogniteClient):

var fileBytes = System.IO.File.ReadAllBytes("equation.png");
var byteContent = new ByteArrayContent(fileBytes);
byteContent.Headers.ContentType = new MediaTypeHeaderValue(fileCreate.MimeType);
var response = await httpClient.PutAsync(fileCreate.UploadUrl, byteContent);

Ensure that the HTTP response status is successful to confirm that the file has been uploaded correctly.

3) Once your file is successfully uploaded, you can retrieve the file from CDF and check the Uploaded property. If it's set to True, that means the file has been successfully uploaded.

var res = await client.Files.RetrieveAsync(ids: new List<Identity>{ new Identity(fileCreate.Id) });

I hope this helps to clarify the process of uploading files with the dotnet SDK.

Badge +1

Hi Everton,

It’s working fine.

Regards,

Adarsh

Reply