Skip to main content
Solved

C# SDK upload file


Forum|alt.badge.img+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.

 

 

Best answer by Everton Colling

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.

View original
Did this topic help you find an answer to your question?

4 replies

  • Practitioner
  • 68 replies
  • April 2, 2024

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


Forum|alt.badge.img+1
  • Author
  • Seasoned
  • 13 replies
  • April 4, 2024

@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));
        }
    }

}

 


Everton Colling
Seasoned Practitioner
Forum|alt.badge.img
  • Seasoned Practitioner
  • 163 replies
  • Answer
  • April 8, 2024

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.


Forum|alt.badge.img+1
  • Author
  • Seasoned
  • 13 replies
  • April 12, 2024

Hi Everton,

It’s working fine.

Regards,

Adarsh


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings