Uploading a Real-Time File

The DIS SDK supports synchronous file upload in Net3.5 and supports both synchronous and asynchronous file uploads in Net4.5.

Synchronous File Upload

UploadFileDemo("fileUploadStream", @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");
        public static bool UploadFileDemo(string streamName, string file)
        {
            int failedRecordCount = 0;
            var dic = new DISIngestionClient();
//Max. file upload size. A maximum of 128 MB files can be uploaded. Set this parameter to 500 KB in this example.
            int splitFileSize = 500 * 1024;
            string deliverDataId = Guid.NewGuid().ToString("N");
            FileInfo fileInfo = new FileInfo(file);
//Number of uploads.
            int steps = (int)Math.Ceiling((decimal)fileInfo.Length / splitFileSize);
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    for (int i = 0; i < steps; i++)
                    {
                        byte[] input = br.ReadBytes(splitFileSize);
                        {
                            PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
//Stream name.
                            putRecordsRequest.StreamName = streamName;
                            var putRecordsRequestEntries = new List<PutRecordsRequestEntry>();
                            var putRecordsRequestEntry = new PutRecordsRequestEntry
                            {
//Data to be uploaded.
                                Data = input,
//Hash value of the data to be written into the partition.
                                ExplicitHashKey = null,
//Unique identifier of the partition.
                                //PartitionId = "shardId-0000000000",
//Partition into which data will be written.
                                //PartitionKey = "0",//i.ToString(),
//Configuration information.
                                ExtenedInfo = new PutRecordsRequestEntryExtendedInfo()
                                {
//File for storing the real-time file data.
                                    FileName = @"thisisshawnfolder/2018/07/04/14/56/Penguins2.jpg",
//ID of the real-time file data, which is specified by the user.
                                    DeliverDataId = deliverDataId,
//Block sequence number of the file to be uploaded. The start sequence number is 0. When a request with sequent number 0 is received, the system uploads a new file or re-uploads a file by default. 
                                    SequenceNumber = i,
//Specify whether the current file block is the last one. The value can be true or false.
                                    EndFlag = i == steps - 1 ? true : false,
                                }
                            };
                            putRecordsRequestEntries.Add(putRecordsRequestEntry);
                            putRecordsRequest.Records = putRecordsRequestEntries;
                            PutRecordsResult response = dic.PutRecords(putRecordsRequest);
                            failedRecordCount += response.FailedRecordCount;
                            //Thread.Sleep(5000);
                            foreach (var item in response.Records)
                            {
                                Console.WriteLine(item);
                            }
                        }
                    }
                }
            }
            return failedRecordCount > 0;
        }

Asynchronous File Upload

  var dic = new DISAsync();
            PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
//Stream name.
            putRecordsRequest.StreamName = "fileUploadStream";
        //File for storing the real-time file data.
            putRecordsRequest.file_name = @"thisismyfolder/2018/07/04/14/56/Penguins1110.jpg";
    //File to be uploaded.
            putRecordsRequest.file_path = @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";
            var bflag = dic.PutFilesAsync(putRecordsRequest);
            Console.WriteLine(bflag.Result);
  • Configure a stream for uploading real-time files.

    Use an existing stream that is used to transmit real-time files.

    //Stream name.
    String streamName = "fileUploadStream";
  • Specify the path of the real-time file to be uploaded.

    Enter the absolute path of the file to be uploaded.

    //Path of the file to be uploaded.
    putRecordsRequest.file_path = @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";
  • Specify the file for storing the uploaded real-time data.

    Configure the name for the file used to store the uploaded real-time data.

    //Name of the file for storing the uploaded real-time data.

    putRecordsRequest.file_name = @"thisismyfolder/2018/07/04/14/56/Penguins1110.jpg";

Viewing Execution Results

True: indicates that the data is uploaded successfully. False: indicates that the data is uploaded unsuccessfully.