/// <summary> /// Parse an S3 Error response and create an S3PostUploadException /// </summary> /// <param name="response">The response from S3</param> /// <returns>An S3PostUploadException with the information from the response</returns> public static S3PostUploadException FromResponse(HttpWebResponse response) { var serializer = new XmlSerializer(typeof(S3PostUploadError)); S3PostUploadError err = null; try { err = serializer.Deserialize(response.GetResponseStream()) as S3PostUploadError; } catch { return(new S3PostUploadException("Unknown", "Unknown error")); } var ex = new S3PostUploadException(err.ErrorCode, err.ErrorMessage) { RequestId = err.RequestId, HostId = err.HostId, }; ex.StatusCode = response.StatusCode; ex.ExtraFields = new Dictionary <string, string>(); if (err.elements != null) { foreach (var f in err.elements) { ex.ExtraFields.Add(f.LocalName, f.InnerText); } } return(ex); }
public static S3PostUploadException FromResponse(HttpWebResponse response) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(S3PostUploadError)); S3PostUploadError s3PostUploadError = null; try { s3PostUploadError = (xmlSerializer.Deserialize(response.GetResponseStream()) as S3PostUploadError); } catch { return(new S3PostUploadException("Unknown", "Unknown error")); } S3PostUploadException ex = new S3PostUploadException(s3PostUploadError.ErrorCode, s3PostUploadError.ErrorMessage) { RequestId = s3PostUploadError.RequestId, HostId = s3PostUploadError.HostId }; ex.StatusCode = response.StatusCode; ex.ExtraFields = new Dictionary <string, string>(); if (s3PostUploadError.elements != null) { XmlElement[] elements = s3PostUploadError.elements; foreach (XmlElement xmlElement in elements) { ex.ExtraFields.Add(xmlElement.LocalName, xmlElement.InnerText); } } return(ex); }
/// <summary> /// Upload data to Amazon S3 using HTTP POST. /// </summary> /// <remarks> /// For more information, <see href="http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html"/> /// </remarks> /// <param name="request">Request object which describes the data to POST</param> /// <exception cref="S3PostUploadException">Thrown if the service returns an error</exception> public static S3PostUploadResponse PostUpload(S3PostUploadRequest request) { string url; string subdomain = request.Region.Equals(RegionEndpoint.USEast1) ? "s3" : "s3-" + request.Region.SystemName; if (request.Bucket.IndexOf('.') > -1) { url = string.Format(CultureInfo.InvariantCulture, "https://{0}.amazonaws.com/{1}/", subdomain, request.Bucket); } else { url = string.Format(CultureInfo.InvariantCulture, "https://{0}.{1}.amazonaws.com", request.Bucket, subdomain); } Uri uri = new Uri(url); HttpWebRequest webRequest = WebRequest.Create(uri) as HttpWebRequest; var boundary = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Replace('=', 'z'); webRequest.ContentType = string.Format(CultureInfo.InvariantCulture, "multipart/form-data; boundary={0}", boundary); webRequest.Method = "POST"; using (var reqStream = webRequest.GetRequestStream()) { request.WriteFormData(boundary, reqStream); byte[] boundaryBytes = Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "--{0}\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n", boundary)); reqStream.Write(boundaryBytes, 0, boundaryBytes.Length); using (var inputStream = null == request.Path ? request.InputStream : File.OpenRead(request.Path)) { byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.Read(buf, 0, 1024)) > 0) { reqStream.Write(buf, 0, bytesRead); } } byte[] endBoundaryBytes = Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "\r\n--{0}--", boundary)); reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); } HttpWebResponse response = null; try { response = webRequest.GetResponse() as HttpWebResponse; } catch (WebException ex) { throw S3PostUploadException.FromResponse((HttpWebResponse)ex.Response); } return(S3PostUploadResponse.FromWebResponse(response)); }
/// <summary> /// Parse an S3 Error response and create an S3PostUploadException /// </summary> /// <param name="response">The response from S3</param> /// <returns>An S3PostUploadException with the information from the response</returns> public static S3PostUploadException FromResponse(HttpWebResponse response) { var serializer = new XmlSerializer(typeof(S3PostUploadError)); S3PostUploadError err = null; try { err = serializer.Deserialize(response.GetResponseStream()) as S3PostUploadError; } catch { return new S3PostUploadException("Unknown", "Unknown error"); } var ex = new S3PostUploadException(err.ErrorCode, err.ErrorMessage) { RequestId = err.RequestId, HostId = err.HostId, }; ex.StatusCode = response.StatusCode; ex.ExtraFields = new Dictionary<string, string>(); if (err.elements != null) { foreach (var f in err.elements) { ex.ExtraFields.Add(f.LocalName, f.InnerText); } } return ex; }
private static S3PostUploadException CreateS3PostUploadException() { var s3pue = new S3PostUploadException("ErrorCode", "Message"); s3pue.ErrorCode = "ec1"; s3pue.RequestId = "rq1"; s3pue.HostId = "hi1"; s3pue.StatusCode = HttpStatusCode.OK; s3pue.ExtraFields = new Dictionary<string, string> { { "k1", "v1" }, { "k2", "v2" }, }; return s3pue; }