Beispiel #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-credential", (string)signature["x-amz-credential"]);
			postFields.Add ("x-amz-algorithm", (string)signature["x-amz-algorithm"]);
			postFields.Add ("x-amz-date", (string)signature["x-amz-date"]);
			postFields.Add ("x-amz-signature", (string)signature["x-amz-signature"]);
			postFields.Add ("x-amz-meta-uploader", Client.UPLOADER + Client.VERSION);
			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);
					// 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++;
					}
				}
					
			}

		}