예제 #1
0
        internal async Task HttpPostS3Async(string filepath, Signature signature)
        {
            FileInfo file = new FileInfo(filepath);

            if (file.Exists == false)
            {
                throw new VzaarApiException("File does not exsist: " + filepath);
            }

            string hostname = (string)signature["upload_hostname"];

            //move signature to Dictionary
            Dictionary<string, string> postFields = new Dictionary<string, string>();

            postFields.Add("x-amz-meta-uploader", Client.UPLOADER + Client.VERSION);
            postFields.Add("AWSAccessKeyId", (string)signature["access_key_id"]);
            postFields.Add("Signature", (string)signature["signature"]);
            postFields.Add("acl", (string)signature["acl"]);
            postFields.Add("bucket", (string)signature["bucket"]);
            postFields.Add("policy", (string)signature["policy"]);
            postFields.Add("success_action_status", (string)signature["success_action_status"]);

            string key = (string)signature["key"];
            string fileKey = "";
            if (String.IsNullOrEmpty(key) == false)
                fileKey = key.Replace("${filename}", file.Name);

            postFields.Add("key", fileKey);


            long? parts = signature["parts"] as long?;

            if (parts == null)
            {

                //upload file in POST form
                FileStream fileStream;
                using (fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                {

                    await HttpPostMfdcAsync(hostname, file.Name, postFields, fileStream).ConfigureAwait(false);
                    // response is validated in the HttpPostMfdcAsync
                }

            }
            else
            {

                string keyCache = postFields["key"];

                long chunk = 0;

                if (signature["part_size_in_bytes"] == null)
                    throw new VzaarApiException("File Upload: not valid 'part_size_in_bytes'");

                int chunkSize = Convert.ToInt32((long)signature["part_size_in_bytes"]);
                byte[] buffer = new byte[chunkSize];

                int bytesCount;

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

                    while ((bytesCount = await fileStream.ReadAsync(buffer, 0, chunkSize)) != 0)
                    {

                        string chunkKey = keyCache + "." + chunk.ToString();
                        postFields.Remove("key");
                        postFields.Add("key", chunkKey);

                        MemoryStream chunkStream;
                        using (chunkStream = new MemoryStream(buffer, 0, bytesCount))
                        {

                            await HttpPostMfdcAsync(hostname, file.Name, postFields, chunkStream);

                            // response is validated in the HttpPostMfdcAsync
                        }

                        VideoUploadProgressEventArgs progress = new VideoUploadProgressEventArgs()
                        {
                            totalParts = (long)parts,
                            uploadedChunk = chunk
                        };

                        UploadProgress(this, progress);

                        chunk++;
                    }
                }

            }

        }
예제 #2
0
 public static void CheckProgress(object sender, VideoUploadProgressEventArgs progress)
 {
     Console.WriteLine("Progress: Total Parts: " + progress.totalParts + " Uploaded chunks: " + progress.uploadedChunk);
 }