File upload using C# not working....help!

Hi all,

I’m new to using Kaltura but not to using APIs to achieve things in C# so I was hopeful of being able to get up to speed quite quickly. However I’m hitting an issue when I try and upload a video. I’ve based my code on these examples:

http://knowledge.kaltura.com/how-start-kaltura-session-using-testme-console
http://knowledge.kaltura.com/uploading-media-using-kaltura-api-console

When I then check the KMC I see an entry but the status of the file is “uploading” and not running time etc. is held against the video.

I’ve posted below my code, I’m sure I can’t be a million miles away from getting it right as I don’t get any error messages so any help would be very gratefully received,

=========CODE START===========
KalturaConfiguration config = new KalturaConfiguration(Convert.ToInt16(ConfigurationManager.AppSettings[“PARTNER_ID”]));
config.ServiceUrl = ConfigurationManager.AppSettings[“SERVICE_URL”];
KalturaClient client = new KalturaClient(config);
String secret = ConfigurationManager.AppSettings[“ADMIN_SECRET”];
String userId = null;
KalturaSessionType type = KalturaSessionType.ADMIN;
int partnerId = Convert.ToInt16(ConfigurationManager.AppSettings[“PARTNER_ID”]);
int expiry = 86400;
String privileges = null;
Object result = client.SessionService.Start(secret, userId, type, partnerId, expiry, privileges);

                client.KS = result.ToString();
                KalturaUploadToken uploadToken = new KalturaUploadToken();

                KalturaUploadToken result2 = client.UploadTokenService.Add(uploadToken);
                
                string uploadTokenID = result2.Id;
                uploadToken.Id = uploadTokenID;


                FileStream fileData = File.Create(context.Server.MapPath("~") + "//CourseUploadTemp//" + fileName);
                bool resume = false;
                bool finalChunk = false;
                float resumeAt = 0;
                KalturaUploadToken resultUp = client.UploadTokenService.Upload(uploadTokenID, fileData, resume, finalChunk, resumeAt);

                string fName = resultUp.FileName;

                KalturaMediaEntry entry = new KalturaMediaEntry();
                entry.MediaType = KalturaMediaType.VIDEO;
                KalturaMediaEntry resultFE = client.MediaService.Add(entry);

                string entryID = resultFE.Id;

                
                KalturaUploadedFileTokenResource resource = new KalturaUploadedFileTokenResource();
                resource.Token = uploadTokenID;
                Object resultTR = client.MediaService.AddContent(entryID, resource);

=========CODE ENDED============

Many thanks,
Steve

Hello,

Try:

    FileStream fileStream = new FileStream("DemoVideo.flv", FileMode.Open, FileAccess.Read);
    KalturaUploadToken uploadToken = client.UploadTokenService.Add();
    client.UploadTokenService.Upload(uploadToken.Id, fileStream);
    KalturaUploadedFileTokenResource mediaResource = new KalturaUploadedFileTokenResource();
    mediaResource.Token = uploadToken.Id;
    KalturaMediaEntry mediaEntry = new KalturaMediaEntry();
    mediaEntry.Name = "Media Entry Using C#.Net Client";
    mediaEntry.MediaType = KalturaMediaType.VIDEO;
    mediaEntry = client.MediaService.Add(mediaEntry);
    mediaEntry = client.MediaService.AddContent(mediaEntry.Id, mediaResource);

This is an example from our unittests.
I think the main thing is how you get the file resource, i.e, try:
FileStream fileStream = new FileStream(“DemoVideo.flv”, FileMode.Open, FileAccess.Read);

Thanks for your reply.

I am running my code locally to VS and not on a server at the moment so I don’t know if that has an influence but I’ve tried replacing the file location with a hardcoded video filename and it has brought up the following.

With just the filename in place the code is looking for the file somewhere on my hard drive which isn’t the end of the word as I just copied the file to that location for now. I then tried again and got the same result as I did in my original post.

Thanks,
Steve

Hi Steve,

Sorry, not quite sure I understand the current situation… you are trying to upload a file from your local disk onto Kaltura, no?
What exactly does the code I pasted above produces? of course, I assume you changed:

FileStream fileStream = new FileStream("DemoVideo.flv", FileMode.Open, FileAccess.Read);

to reflect the file path on your local disk.

Hi Jess,

Yes that is correct at the moment I just want to upload a file from my hard drive.

If I pasted in your code I got an error in the line KalturaUploadToken uploadToken = client.UploadTokenService.Add();

The error was:
"n exception of type ‘Kaltura.KalturaAPIException’ occurred in KalturaClient.dll but was not handled in user code

Additional information: The access to service [uploadToken->add] is forbidden"

When I went back to my own code and made sure the file had the full path I had no errors and the following row is visible in the KMC:

Steve

Can you just try to change your code so that it reads the file stream like this:

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);

and test?

Hi,

Unfortunately that doesn’t seem to have made a difference. Sorry.

Steve

Hi,

I suggest you try running the unittests in csharp/KalturaClientTester/KalturaClientTester.cs
you can do that by editing the file, replacing the tokens with actual creds and then building the KalturaClientTester.csproj

        private const int PARTNER_ID = @YOUR_PARTNER_ID@; //enter your partner id
        private const string ADMIN_SECRET = "@YOUR_ADMIN_SECRET@"; //enter your admin secret
        private const string SERVICE_URL = "@SERVICE_URL@";

and then execute KalturaClientTester.exe
Assuming that works well, look at the code for static KalturaMediaEntry StartSessionAndUploadMedia(FileStream fileStream)

If it does not work, lets see how exactly it fails and we can continue from there

Hi,

Thanks for the further ideas.

I have some more to report which may or may not be helpful in getting things working. When I ran the unittests just replacing my credentials I got no errors but there were two entries created on the content dashboard (see below) both of them with a status of converting and no running time against them:

I tried then replacing the call to SampleReplaceVideoFlavorAndAddCaption with a call to StartSessionAndUploadMedia. This generated an error in MediaService.Upload on the line this.ThrowExceptionOnAPIError(result);in Client.DoQueue. The error returned was:

Invalid KS - “ODRlOGE1ZmM0OWNjOGY4NmI1ZmYzZmY1MDk4ZDc5NTE1MjU3ZDVhOXwxMDE7MTAxOzE0NDMxNzkyMjQ7MDs2MzU3ODY5MzIyNDA3NTQ5ODU7dGVzdFVzZXI7Ow==”

I hope this all makes sense?

Thanks for your help so far.

Steve

Hi,

Converting is good:) it means the entry was successfully uploaded and is currently being transcoded.
I imagine it ended up being ready. Are you working against SaaS BTW?

I would suggest just using the exact same code the unittests use in your code.
Invalid KS [Kaltura Session] can happen if the partner ID, secret and session type do not match… in which case, you won’t be able to do anything else.

Hi,

Well the good news is by using exactly the code from the unittests I am now consistently able to upload files.

It seems, embarrassingly, that one of the files I was tested with, although it copied to the location ok, was 0kb in size. Once I recopied the file and made sure it was properly copied things began to happen.

I’m still having issues loading a file from a location other than the root of the project in VS. This I’m sure will sort itself out once we are running on a server as I’m sure I’ve had similar issues with file uploads before.

Thanks very much for your help on this.

Steve

Welcome, Steve. Do let me know if you require further assistance but I think you’re set:)