Exemplo n.º 1
0
        private async Task AbortMultiUpload(HttpClient client, string url)
        {
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
            {
                Headers =
                {
                    { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256", payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(requestMessage.Headers);

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, now, headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            // The specified multipart upload does not exist.
            // The upload ID might be invalid,
            // or the multipart upload might have been aborted or completed.
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 2
0
        public void PutContainer()
        {
            var url = GetUrl(_serverUrlForContainer, "restype=container");

            var now     = SystemTime.UtcNow;
            var content = new EmptyContent
            {
                Headers =
                {
                    { "x-ms-date",    now.ToString("R")   },
                    { "x-ms-version", AzureStorageVersion }
                }
            };

            var client = GetClient();

            SetAuthorizationHeader(client, HttpMethods.Put, url, content.Headers);

            var response = client.PutAsync(url, content, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            if (response.StatusCode == HttpStatusCode.Conflict)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 3
0
        private bool VaultExists()
        {
            var url     = GetUrl();
            var now     = SystemTime.UtcNow;
            var content = new HttpRequestMessage(HttpMethods.Get, url);

            UpdateHeaders(content.Headers, now, null);

            var headers = ConvertToHeaders(content.Headers);

            var client = GetClient();
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = client.SendAsync(content, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 4
0
        public void DeleteContainer()
        {
            var url = GetUrl(_serverUrlForContainer, "restype=container");

            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
            {
                Headers =
                {
                    { "x-ms-date",    now.ToString("R")   },
                    { "x-ms-version", AzureStorageVersion }
                }
            };

            var client = GetClient();

            SetAuthorizationHeader(client, HttpMethods.Delete, url, requestMessage.Headers);

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 5
0
        private string GetBucketLocation(bool returnWhenNotFound = false)
        {
            using (UseRegionInvariantRequest())
            {
                var url = $"{GetUrl()}?location";
                var now = SystemTime.UtcNow;

                var requestMessage = new HttpRequestMessage(HttpMethods.Get, url);
                UpdateHeaders(requestMessage.Headers, now, null);

                var headers = ConvertToHeaders(requestMessage.Headers);

                var client = GetClient();
                client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

                var response = client.SendAsync(requestMessage, CancellationToken).Result;
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    if (returnWhenNotFound)
                    {
                        return(null);
                    }

                    throw new BucketNotFoundException($"Bucket name '{_bucketName}' doesn't exist!");
                }

                if (response.IsSuccessStatusCode == false)
                {
                    var storageException = StorageException.FromResponseMessage(response);

                    if (response.StatusCode == HttpStatusCode.Forbidden)
                    {
                        throw new AwsForbiddenException(storageException.ResponseString);
                    }

                    throw storageException;
                }

                using (var stream = response.Content.ReadAsStreamAsync().Result)
                    using (var reader = new StreamReader(stream))
                    {
                        var xElement = XElement.Load(reader);
                        var value    = xElement.Value;

                        if (value.Equals(string.Empty))
                        {
                            // when the bucket's region is US East (N. Virginia - us-east-1),
                            // Amazon S3 returns an empty string for the bucket's region
                            value = DefaultRegion;
                        }
                        else if (value.Equals("EU", StringComparison.OrdinalIgnoreCase))
                        {
                            // EU (Ireland) => EU or eu-west-1
                            value = "eu-west-1";
                        }

                        return(value == string.Empty ? DefaultRegion : value);
                    }
            }
        }
Exemplo n.º 6
0
        public async Task <Blob> GetObjectAsync(string key)
        {
            var url = $"{GetUrl()}/{key}";
            var now = SystemTime.UtcNow;

            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url);

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            var response = await client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, CancellationToken);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            var data = await response.Content.ReadAsStreamAsync();

            var metadataHeaders = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault());

            return(new Blob(data, metadataHeaders));
        }
Exemplo n.º 7
0
        private void CompleteMultiUpload(string url, HttpClient client,
                                         List <Tuple <int, string> > partNumbersWithEtag)
        {
            var now       = SystemTime.UtcNow;
            var doc       = CreateCompleteMultiUploadDocument(partNumbersWithEtag);
            var xmlString = doc.OuterXml;

            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Content = new StringContent(xmlString, Encoding.UTF8, "text/plain")
            };

            UpdateHeaders(requestMessage.Headers, now, stream: null, RavenAwsHelper.CalculatePayloadHashFromString(xmlString));

            var headers = ConvertToHeaders(requestMessage.Headers);
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 8
0
        private async Task PutBlockList(string baseUrl, HttpClient client,
                                        List <string> blockIds, Dictionary <string, string> metadata)
        {
            var url       = baseUrl + "?comp=blocklist";
            var now       = SystemTime.UtcNow;
            var doc       = CreateXmlDocument(blockIds);
            var xmlString = doc.OuterXml;

            var content = new StringContent(xmlString, Encoding.UTF8, "text/plain")
            {
                Headers =
                {
                    { "x-ms-date",      now.ToString("R")                                                               },
                    { "x-ms-version",   AzureStorageVersion                                                             },
                    { "Content-Length", Encoding.UTF8.GetBytes(xmlString).Length.ToString(CultureInfo.InvariantCulture) }
                }
            };

            foreach (var metadataKey in metadata.Keys)
            {
                content.Headers.Add("x-ms-meta-" + metadataKey.ToLower(), metadata[metadataKey]);
            }

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, content.Headers);

            var response = await client.PutAsync(url, content, CancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 9
0
        private string GetUploadId(string url, string archiveDescription, long lengthPerPartPowerOf2)
        {
            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Headers =
                {
                    { "x-amz-archive-description", archiveDescription               },
                    { "x-amz-part-size",           lengthPerPartPowerOf2.ToString() }
                }
            };

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            return(response.Headers
                   .GetValues("x-amz-multipart-upload-id")
                   .First());
        }
Exemplo n.º 10
0
        public async Task DeleteContainer()
        {
            var url = _serverUrlForContainer + "?restype=container";

            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
            {
                Headers =
                {
                    { "x-ms-date",    now.ToString("R")   },
                    { "x-ms-version", AzureStorageVersion }
                }
            };

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, requestMessage.Headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 11
0
        public async Task DeleteArchive(string archiveId)
        {
            var url         = $"{GetUrl()}/archives/{archiveId}";
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var content = new HttpRequestMessage(HttpMethods.Delete, url)
            {
                Headers =
                {
                    { "x-amz-glacier-version", "2012-06-01"                        },
                    { "x-amz-date",            RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256",  payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(content.Headers);

            var client = GetClient();
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.SendAsync(content, CancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 12
0
        private async Task <string> CompleteMultiUpload(
            string url, HttpClient client, long archiveSize,
            string payloadTreeHash)
        {
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Headers =
                {
                    { "x-amz-glacier-version",  "2012-06-01"                        },
                    { "x-amz-date",             RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-archive-size",     archiveSize.ToString()              },
                    { "x-amz-content-sha256",   payloadHash                         },
                    { "x-amz-sha256-tree-hash", payloadTreeHash                     }
                }
            };

            var headers = ConvertToHeaders(requestMessage.Headers);
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            return(ReadArchiveId(response));
        }
Exemplo n.º 13
0
        private async Task <string> UploadPart(Stream baseStream, HttpClient client, string url, long length, int retryCount)
        {
            // saving the position if we need to retry
            var position = baseStream.Position;

            using (var subStream = new SubStream(baseStream, offset: 0, length: length))
            {
                var now = SystemTime.UtcNow;

                // stream is disposed by the HttpClient
                var content = new ProgressableStreamContent(subStream, Progress)
                {
                    Headers =
                    {
                        { "Content-Length", subStream.Length.ToString(CultureInfo.InvariantCulture) }
                    }
                };

                UpdateHeaders(content.Headers, now, subStream);

                var headers = ConvertToHeaders(content.Headers);
                client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, now, headers);

                try
                {
                    var response = await client.PutAsync(url, content, CancellationToken);

                    if (response.IsSuccessStatusCode)
                    {
                        var etagHeader = response.Headers.GetValues("ETag");
                        return(etagHeader.First());
                    }

                    if (retryCount == MaxRetriesForMultiPartUpload)
                    {
                        throw StorageException.FromResponseMessage(response);
                    }
                }
                catch (Exception)
                {
                    if (retryCount == MaxRetriesForMultiPartUpload)
                    {
                        throw;
                    }
                }

                // revert the uploaded count before retry
                Progress?.UploadProgress.UpdateUploaded(-content.Uploaded);
            }

            // wait for one second before trying again to send the request
            // maybe there was a network issue?
            await Task.Delay(1000);

            CancellationToken.ThrowIfCancellationRequested();

            // restore the stream position before retrying
            baseStream.Position = position;
            return(await UploadPart(baseStream, client, url, length, ++retryCount));
        }
Exemplo n.º 14
0
        private async Task <string> GetUploadId(string url,
                                                string archiveDescription, long lengthPerPartPowerOf2)
        {
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Headers =
                {
                    { "x-amz-glacier-version",     "2012-06-01"                        },
                    { "x-amz-date",                RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-archive-description", archiveDescription                  },
                    { "x-amz-part-size",           lengthPerPartPowerOf2.ToString()    },
                    { "x-amz-content-sha256",      payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            return(response.Headers
                   .GetValues("x-amz-multipart-upload-id")
                   .First());
        }
Exemplo n.º 15
0
        private string CompleteMultiUpload(string url, HttpClient client, long archiveSize, string payloadTreeHash)
        {
            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Headers =
                {
                    { "x-amz-archive-size",     archiveSize.ToString() },
                    { "x-amz-sha256-tree-hash", payloadTreeHash        }
                }
            };

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            return(ReadArchiveId(response));
        }
Exemplo n.º 16
0
        private void AbortMultiUpload(string url, HttpClient client)
        {
            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url);

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, now, headers);

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            // The specified multipart upload does not exist.
            // The upload ID might be invalid,
            // or the multipart upload might have been aborted or completed.
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 17
0
        public void DeleteObject(string key)
        {
            var url = $"{GetUrl()}/{key}";
            var now = SystemTime.UtcNow;

            var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url);

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient(TimeSpan.FromHours(1));
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 18
0
        private void DeleteBlobsWithSasToken(List <string> blobs)
        {
            foreach (var blob in blobs)
            {
                var url = GetUrl($"{_serverUrlForContainer}/{blob}");

                var now = SystemTime.UtcNow;

                var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
                {
                    Headers =
                    {
                        { "x-ms-date",    now.ToString("R")   },
                        { "x-ms-version", AzureStorageVersion }
                    }
                };

                var client   = GetClient();
                var response = client.SendAsync(requestMessage, CancellationToken).Result;
                if (response.IsSuccessStatusCode)
                {
                    continue;
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    continue;
                }

                throw StorageException.FromResponseMessage(response);
            }
        }
Exemplo n.º 19
0
        public async Task DeleteMultipleObjects(List <string> objects)
        {
            var url = $"{GetUrl()}/?delete";
            var now = SystemTime.UtcNow;

            var xml = new XElement("Delete");

            foreach (var objectPath in objects)
            {
                var obj = new XElement("Object");
                var key = new XElement("Key", objectPath);
                obj.Add(key);
                xml.Add(obj);
            }

            var xmlString      = xml.ToString();
            var md5Hash        = CalculateMD5Hash(xmlString);
            var requestMessage = new HttpRequestMessage(HttpMethods.Post, url)
            {
                Content = new StringContent(xmlString, Encoding.UTF8, "text/plain")
                {
                    Headers =
                    {
                        { "Content-Length", xmlString.Length.ToString(CultureInfo.InvariantCulture) },
                        { "Content-MD5",    md5Hash                                                 }
                    }
                }
            };

            UpdateHeaders(requestMessage.Headers, now, null, RavenAwsHelper.CalculatePayloadHashFromString(xmlString));

            var headers = ConvertToHeaders(requestMessage.Headers);

            headers.Add("Content-MD5", md5Hash);

            var client = GetClient(TimeSpan.FromHours(1));
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.SendAsync(requestMessage);

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 20
0
        private async Task <string> GetBucketLocation(bool returnWhenNotFound = false)
        {
            using (UseRegionInvariantRequest())
            {
                var url         = $"{GetUrl()}?location";
                var now         = SystemTime.UtcNow;
                var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

                var requestMessage = new HttpRequestMessage(HttpMethods.Get, url)
                {
                    Headers =
                    {
                        { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                        { "x-amz-content-sha256", payloadHash                         }
                    }
                };

                var headers = ConvertToHeaders(requestMessage.Headers);

                var client = GetClient();
                client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

                var response = await client.SendAsync(requestMessage, CancellationToken);

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    if (returnWhenNotFound)
                    {
                        return(null);
                    }

                    throw new BucketNotFoundException($"Bucket name '{_bucketName}' doesn't exist!");
                }

                if (response.IsSuccessStatusCode == false)
                {
                    throw StorageException.FromResponseMessage(response);
                }

                using (var stream = await response.Content.ReadAsStreamAsync())
                    using (var reader = new StreamReader(stream))
                    {
                        var xElement = XElement.Load(reader);
                        var value    = xElement.Value;

                        // when the bucket's region is US East (N. Virginia - us-east-1),
                        // Amazon S3 returns an empty string for the bucket's region
                        return(value == string.Empty ? DefaultRegion : value);
                    }
            }
        }
Exemplo n.º 21
0
        public async Task <string> UploadArchive(Stream stream, string archiveDescription)
        {
            await TestConnection();

            if (stream.Length > MaxUploadArchiveSizeInBytes)
            {
                // for objects over 256MB
                return(await MultiPartUpload(archiveDescription, stream));
            }

            var url = $"{GetUrl()}/archives";

            var now = SystemTime.UtcNow;

            var payloadHash     = RavenAwsHelper.CalculatePayloadHash(stream);
            var payloadTreeHash = RavenAwsHelper.CalculatePayloadTreeHash(stream);

            UploadProgress?.SetTotal(stream.Length);

            // stream is disposed by the HttpClient
            var content = new ProgressableStreamContent(stream, UploadProgress)
            {
                Headers =
                {
                    { "x-amz-glacier-version",     "2012-06-01"                                         },
                    { "x-amz-date",                RavenAwsHelper.ConvertToString(now)                  },
                    { "x-amz-content-sha256",      payloadHash                                          },
                    { "x-amz-sha256-tree-hash",    payloadTreeHash                                      },
                    { "x-amz-archive-description", archiveDescription                                   },
                    { "Content-Length",            stream.Length.ToString(CultureInfo.InvariantCulture) }
                }
            };

            var headers = ConvertToHeaders(content.Headers);

            var client = GetClient(TimeSpan.FromHours(24));
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.PostAsync(url, content, CancellationToken);

            UploadProgress?.ChangeState(UploadState.Done);
            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            return(ReadArchiveId(response));
        }
Exemplo n.º 22
0
        public async Task PutObject(string key, Stream stream, Dictionary <string, string> metadata)
        {
            await TestConnection();

            if (stream.Length > MaxUploadPutObjectSizeInBytes)
            {
                // for objects over 256MB
                await MultiPartUpload(key, stream, metadata);

                return;
            }

            var url         = $"{GetUrl()}/{key}";
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(stream);

            Progress?.UploadProgress.SetTotal(stream.Length);

            // stream is disposed by the HttpClient
            var content = new ProgressableStreamContent(stream, Progress)
            {
                Headers =
                {
                    { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256", payloadHash                         }
                }
            };

            foreach (var metadataKey in metadata.Keys)
            {
                content.Headers.Add("x-amz-meta-" + metadataKey.ToLower(), metadata[metadataKey]);
            }

            var headers = ConvertToHeaders(content.Headers);

            var client = GetClient(TimeSpan.FromHours(24));
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.PutAsync(url, content, CancellationToken);

            Progress?.UploadProgress.ChangeState(UploadState.Done);
            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 23
0
        private void ThrowError(HttpResponseMessage response)
        {
            switch (response.StatusCode)
            {
            case HttpStatusCode.Moved:
                AssertBucketLocation();
                break;

            case HttpStatusCode.Forbidden:
                AssertBucketPermissions();
                break;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 24
0
        public async Task PutBlob(string key, Stream stream, Dictionary <string, string> metadata)
        {
            await TestConnection();

            if (stream.Length > MaxUploadPutBlobInBytes)
            {
                // for blobs over 256MB
                await PutBlockApi(key, stream, metadata);

                return;
            }

            var url = _serverUrlForContainer + "/" + key;

            Progress?.UploadProgress.SetTotal(stream.Length);

            var now = SystemTime.UtcNow;
            // stream is disposed by the HttpClient
            var content = new ProgressableStreamContent(stream, Progress)
            {
                Headers =
                {
                    { "x-ms-date",      now.ToString("R")                                    },
                    { "x-ms-version",   AzureStorageVersion                                  },
                    { "x-ms-blob-type", "BlockBlob"                                          },
                    { "Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture) }
                }
            };

            foreach (var metadataKey in metadata.Keys)
            {
                content.Headers.Add("x-ms-meta-" + metadataKey.ToLower(), metadata[metadataKey]);
            }

            var client = GetClient(TimeSpan.FromHours(3));

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, content.Headers);

            var response = await client.PutAsync(url, content, CancellationToken);

            Progress?.UploadProgress.ChangeState(UploadState.Done);
            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 25
0
        public async Task PutBucket(string awsRegion = null)
        {
            // we set the bucket region in the request message
            using (UseRegionInvariantRequest())
            {
                var doc       = CreatePutBucketXmlDocument(awsRegion ?? AwsRegion);
                var xmlString = doc.OuterXml;
                var url       = GetUrl();
                var now       = SystemTime.UtcNow;

                var hasLocationConstraint = AwsRegion != DefaultRegion;
                var payloadHash           = hasLocationConstraint ?
                                            RavenAwsHelper.CalculatePayloadHashFromString(xmlString) :
                                            RavenAwsHelper.CalculatePayloadHash(null);

                var requestMessage = new HttpRequestMessage(HttpMethods.Put, url)
                {
                    Headers =
                    {
                        { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                        { "x-amz-content-sha256", payloadHash                         }
                    },
                    Content = hasLocationConstraint == false ?
                              null : new StringContent(xmlString, Encoding.UTF8, "text/plain")
                };

                var headers = ConvertToHeaders(requestMessage.Headers);

                var client = GetClient(TimeSpan.FromHours(1));
                var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, now, headers);
                client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

                var response = await client.SendAsync(requestMessage, CancellationToken);

                if (response.IsSuccessStatusCode)
                {
                    return;
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return;
                }

                throw StorageException.FromResponseMessage(response);
            }
        }
Exemplo n.º 26
0
        public void PutBlob(string key, Stream stream, Dictionary <string, string> metadata)
        {
            AsyncHelpers.RunSync(TestConnectionAsync);

            if (stream.Length > MaxUploadPutBlob.GetValue(SizeUnit.Bytes))
            {
                // for blobs over 256MB
                PutBlockApi(key, stream, metadata);
                return;
            }

            var url = GetUrl($"{_serverUrlForContainer}/{key}");

            Progress?.UploadProgress.SetTotal(stream.Length);

            var now = SystemTime.UtcNow;
            // stream is disposed by the HttpClient
            var content = new ProgressableStreamContent(stream, Progress)
            {
                Headers =
                {
                    { "x-ms-date",      now.ToString("R")                                    },
                    { "x-ms-version",   AzureStorageVersion                                  },
                    { "x-ms-blob-type", "BlockBlob"                                          },
                    { "Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture) }
                }
            };

            foreach (var metadataKey in metadata.Keys)
            {
                content.Headers.Add("x-ms-meta-" + metadataKey.ToLower(), metadata[metadataKey]);
            }

            var client = GetClient(TimeSpan.FromHours(3));

            SetAuthorizationHeader(client, HttpMethods.Put, url, content.Headers);

            var response = client.PutAsync(url, content, CancellationToken).Result;

            Progress?.UploadProgress.ChangeState(UploadState.Done);
            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
Exemplo n.º 27
0
        public async Task <List <string> > GetContainerNames(int maxResults)
        {
            var url = GetBaseServerUrl() + $"?comp=list&maxresults={maxResults}";

            var now = SystemTime.UtcNow;

            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url)
            {
                Headers =
                {
                    { "x-ms-date",    now.ToString("R")   },
                    { "x-ms-version", AzureStorageVersion }
                }
            };

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, requestMessage.Headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            var containersList = new List <string>();

            using (var stream = await response.Content.ReadAsStreamAsync())
                using (var reader = new StreamReader(stream))
                {
                    var xDocument = XDocument.Load(reader);

                    foreach (var node in xDocument.Descendants("Containers"))
                    {
                        foreach (var containerNode in node.Descendants("Container"))
                        {
                            var nodeName = containerNode.Element("Name").Value;
                            containersList.Add(nodeName);
                        }
                    }
                }

            return(containersList);
        }
Exemplo n.º 28
0
        private string GetBucketPermission()
        {
            var url = $"{GetUrl()}?acl";
            var now = SystemTime.UtcNow;

            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url);

            UpdateHeaders(requestMessage.Headers, now, null);

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new BucketNotFoundException($"Bucket name '{_bucketName}' doesn't exist!");
            }


            if (response.IsSuccessStatusCode == false)
            {
                var storageException = StorageException.FromResponseMessage(response);

                if (response.StatusCode == HttpStatusCode.Forbidden)
                {
                    throw new UnauthorizedAccessException(storageException.ResponseString);
                }

                throw storageException;
            }

            using (var stream = response.Content.ReadAsStreamAsync().Result)
                using (var reader = new StreamReader(stream))
                {
                    var xDocument         = XDocument.Load(reader);
                    var permissionElement = xDocument
                                            .Descendants()
                                            .First(x => x.Name.LocalName == "Permission");
                    return(permissionElement.Value);
                }
        }
Exemplo n.º 29
0
        public async Task DeleteBucket()
        {
            var region = await GetBucketLocation(returnWhenNotFound : true);

            if (region == null)
            {
                return;
            }

            using (UseSpecificRegion(region))
            {
                var url         = GetUrl();
                var now         = SystemTime.UtcNow;
                var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

                var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
                {
                    Headers =
                    {
                        { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                        { "x-amz-content-sha256", payloadHash                         }
                    }
                };

                var headers = ConvertToHeaders(requestMessage.Headers);

                var client = GetClient();
                client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Delete, url, now, headers);

                var response = await client.SendAsync(requestMessage, CancellationToken);

                if (response.IsSuccessStatusCode)
                {
                    return;
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return;
                }

                throw StorageException.FromResponseMessage(response);
            }
        }
Exemplo n.º 30
0
        private async Task <string> GetBucketPermission()
        {
            var url         = $"{GetUrl()}?acl";
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url)
            {
                Headers =
                {
                    { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256", payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new BucketNotFoundException($"Bucket name '{_bucketName}' doesn't exist!");
            }

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            using (var stream = await response.Content.ReadAsStreamAsync())
                using (var reader = new StreamReader(stream))
                {
                    var xDocument         = XDocument.Load(reader);
                    var permissionElement = xDocument
                                            .Descendants()
                                            .First(x => x.Name.LocalName == "Permission");
                    return(permissionElement.Value);
                }
        }