示例#1
0
        private Dictionary <int, string> UploadParts(string accessKey, string secretKey, Uri requestUri, string uploadId, string filePath, DateTime expiryDate, Action <int, int> callbackForPartDone = null)
        {
            var  partNumberETags = new Dictionary <int, string>();
            var  timeSpan        = new TimeSpan(expiryDate.Ticks - _jan1St1970Ticks);
            long expiry          = Convert.ToInt64(timeSpan.TotalSeconds);
            var  fileContents    = File.ReadAllBytes(filePath);

            int partNumber        = 1;
            int startPosition     = 0;
            int bytesToBeUploaded = fileContents.Length;

            do
            {
                int bytesToUpload = Math.Min(FiveMegaBytes, bytesToBeUploaded);

                if (callbackForPartDone != null)
                {
                    callbackForPartDone(partNumber, bytesToBeUploaded);
                }

                var    partUploadUrl          = new Uri(string.Format("{0}?uploadId={1}&partNumber={2}", requestUri.AbsoluteUri, HttpUtility.UrlEncode(uploadId), partNumber));
                string partUploadSignature    = SignatureHelper.CreateSignature(secretKey, SignatureHelper.CreateStringToSign(partUploadUrl, "PUT", string.Empty, string.Empty, expiry, null));
                var    partUploadPreSignedUrl = new Uri(string.Format("{0}?uploadId={1}&partNumber={2}&AWSAccessKeyId={3}&Signature={4}&Expires={5}", requestUri.AbsoluteUri,
                                                                      HttpUtility.UrlEncode(uploadId), partNumber, accessKey, HttpUtility.UrlEncode(partUploadSignature), expiry));

                var request = (HttpWebRequest)WebRequest.Create(partUploadPreSignedUrl);
                request.Method        = "PUT";
                request.Timeout       = 1000 * 600;
                request.ContentLength = bytesToUpload;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(fileContents, startPosition, bytesToUpload);
                }

                using (var resp = (HttpWebResponse)request.GetResponse())
                {
                    using (var streamReader = new StreamReader(resp.GetResponseStream()))
                    {
                        partNumberETags.Add(partNumber, resp.Headers["ETag"]);
                    }
                }

                bytesToBeUploaded = bytesToBeUploaded - bytesToUpload;
                startPosition     = bytesToUpload;

                partNumber = partNumber + 1;
            }while (bytesToBeUploaded > 0);

            return(partNumberETags);
        }
示例#2
0
        private string InitiateMultipartUpload(string accessKey, string secretKey, Uri requestUri, DateTime requestDate, string contentType, NameValueCollection requestHeaders)
        {
            string uploadId;
            var    uploadIdRequestUrl = new Uri(string.Format("{0}?uploads=", requestUri.AbsoluteUri));
            var    uploadIdRequestUrlRequestHeaders = new NameValueCollection();

            if (requestHeaders != null)
            {
                for (int i = 0; i < requestHeaders.Count; i++)
                {
                    var key   = requestHeaders.Keys[i];
                    var value = requestHeaders[key];
                    if (key.StartsWith("x-amz-", StringComparison.InvariantCultureIgnoreCase))
                    {
                        uploadIdRequestUrlRequestHeaders.Add(key, value);
                    }
                }
            }

            var stringToSign         = SignatureHelper.CreateStringToSign(uploadIdRequestUrl, "POST", string.Empty, contentType, requestDate, requestHeaders);
            var signatureForUploadId = SignatureHelper.CreateSignature(secretKey, stringToSign);

            uploadIdRequestUrlRequestHeaders.Add("Authorization", string.Format("AWS {0}:{1}", accessKey, signatureForUploadId));

            var request = (HttpWebRequest)WebRequest.Create(uploadIdRequestUrl);

            request.Method        = "POST";
            request.ContentLength = 0;
            request.Date          = requestDate;
            request.ContentType   = contentType;
            request.Headers.Add(uploadIdRequestUrlRequestHeaders);

            using (var resp = (HttpWebResponse)request.GetResponse())
            {
                using (var s = new StreamReader(resp.GetResponseStream()))
                {
                    var      response = s.ReadToEnd();
                    XElement xe       = XElement.Parse(response);
                    uploadId = xe.Element(XName.Get("UploadId", "http://s3.amazonaws.com/doc/2006-03-01/")).Value;
                }
            }

            return(uploadId);
        }
示例#3
0
        private string FinishMultipartUpload(string accessKey, string secretKey, Uri requestUri, string uploadId, Dictionary <int, string> partNumberETags, DateTime expiryDate)
        {
            var    timeSpan = new TimeSpan(expiryDate.Ticks - _jan1St1970Ticks);
            long   expiry   = Convert.ToInt64(timeSpan.TotalSeconds);
            var    finishOrCancelMultipartUploadUri  = new Uri(string.Format("{0}?uploadId={1}", requestUri.AbsoluteUri, uploadId));
            string signatureForFinishMultipartUpload = SignatureHelper.CreateSignature(secretKey, SignatureHelper.CreateStringToSign(finishOrCancelMultipartUploadUri, "POST", string.Empty, "text/plain", expiry, null));

            var finishMultipartUploadUrl = new Uri(string.Format("{0}?uploadId={1}&AWSAccessKeyId={2}&Signature={3}&Expires={4}", requestUri.AbsoluteUri, HttpUtility.UrlEncode(uploadId), accessKey, HttpUtility.UrlEncode(signatureForFinishMultipartUpload), expiry));

            var payload = new StringBuilder();

            payload.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><CompleteMultipartUpload>");
            foreach (var item in partNumberETags)
            {
                payload.AppendFormat("<Part><PartNumber>{0}</PartNumber><ETag>{1}</ETag></Part>", item.Key, item.Value);
            }
            payload.Append("</CompleteMultipartUpload>");

            var requestPayload = Encoding.UTF8.GetBytes(payload.ToString());

            var request = (HttpWebRequest)WebRequest.Create(finishMultipartUploadUrl);

            request.Method        = "POST";
            request.ContentType   = "text/plain";
            request.ContentLength = requestPayload.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(requestPayload, 0, requestPayload.Length);
            }
            using (var resp = (HttpWebResponse)request.GetResponse())
            {
            }

            string signatureForGet = SignatureHelper.CreateSignature(secretKey, SignatureHelper.CreateStringToSign(new Uri(requestUri.AbsoluteUri), "GET", string.Empty, string.Empty, expiry, null));
            string signedGetUrl    = string.Format("{0}?AWSAccessKeyId={1}&Signature={2}&Expires={3}", requestUri.AbsoluteUri, accessKey, HttpUtility.UrlEncode(signatureForGet), expiry);

            return(signedGetUrl);
        }