Exemplo n.º 1
0
        public async Task <IFileReference> GetFileReferenceAsync(string folderName, string fileName, string ifNoneMatch = null)
        {
            if (String.IsNullOrWhiteSpace(folderName))
            {
                throw new ArgumentNullException(nameof(folderName));
            }

            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob   = container.GetBlobReference(fileName);
            var result = await GetBlobContentAsync(folderName, fileName, ifNoneMatch);

            if (result.StatusCode == HttpStatusCode.NotModified)
            {
                return(CloudFileReference.NotModified(ifNoneMatch));
            }
            else if (result.StatusCode == HttpStatusCode.OK)
            {
                if (await blob.ExistsAsync())
                {
                    await blob.FetchAttributesAsync();
                }
                return(CloudFileReference.Modified(blob, result.Data));
            }
            else
            {
                // Not found
                return(null);
            }
        }
Exemplo n.º 2
0
        public async Task DeleteFileAsync(string folderName, string fileName)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);
            await blob.DeleteIfExistsAsync();
        }
Exemplo n.º 3
0
        public async Task SaveFileAsync(string folderName, string fileName, Stream file, bool overwrite = true)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            try
            {
                await blob.UploadFromStreamAsync(file, overwrite);
            }
            catch (StorageException ex) when(ex.IsFileAlreadyExistsException())
            {
                throw new FileAlreadyExistsException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              "There is already a blob with name {0} in container {1}.",
                              fileName,
                              folderName),
                          ex);
            }

            blob.Properties.ContentType  = GetContentType(folderName);
            blob.Properties.CacheControl = GetCacheControl(folderName);
            await blob.SetPropertiesAsync();
        }
Exemplo n.º 4
0
        public async Task SaveFileAsync(string folderName, string fileName, Stream file, IAccessCondition accessConditions)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            accessConditions = accessConditions ?? AccessConditionWrapper.GenerateIfNotExistsCondition();

            var mappedAccessCondition = new AccessCondition
            {
                IfNoneMatchETag = accessConditions.IfNoneMatchETag,
                IfMatchETag     = accessConditions.IfMatchETag,
            };

            try
            {
                await blob.UploadFromStreamAsync(file, mappedAccessCondition);
            }
            catch (StorageException ex) when(ex.IsFileAlreadyExistsException())
            {
                throw new FileAlreadyExistsException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              "There is already a blob with name {0} in container {1}.",
                              fileName,
                              folderName),
                          ex);
            }

            blob.Properties.ContentType = GetContentType(folderName);
            await blob.SetPropertiesAsync();
        }
        public async Task <Uri> GetFileReadUriAsync(string folderName, string fileName, DateTimeOffset?endOfAccess)
        {
            folderName = folderName ?? throw new ArgumentNullException(nameof(folderName));
            fileName   = fileName ?? throw new ArgumentNullException(nameof(fileName));
            if (endOfAccess.HasValue && endOfAccess < DateTimeOffset.UtcNow)
            {
                throw new ArgumentOutOfRangeException(nameof(endOfAccess), $"{nameof(endOfAccess)} is in the past");
            }
            bool isPublicFolder = IsPublicContainer(folderName);

            if (!isPublicFolder && endOfAccess == null)
            {
                throw new ArgumentNullException(nameof(endOfAccess), $"{nameof(endOfAccess)} must not be null for non-public containers");
            }

            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            if (isPublicFolder)
            {
                return(blob.Uri);
            }

            return(new Uri(blob.Uri, blob.GetSharedReadSignature(endOfAccess)));
        }
Exemplo n.º 6
0
        public async Task <bool> FileExistsAsync(string folderName, string fileName)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            return(await blob.ExistsAsync());
        }
        private async Task <ISimpleCloudBlob> GetBlobForUriAsync(string folderName, string fileName)
        {
            folderName = folderName ?? throw new ArgumentNullException(nameof(folderName));
            fileName   = fileName ?? throw new ArgumentNullException(nameof(fileName));

            ICloudBlobContainer container = await GetContainerAsync(folderName);

            return(container.GetBlobReference(fileName));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName)
        {
            ICloudBlobContainer container = await GetContainer(folderName);

            var blob = container.GetBlobReference(fileName);

            var redirectUri = GetRedirectUri(requestUrl, blob.Uri);

            return(new RedirectResult(redirectUri.OriginalString, false));
        }
        public async Task <T> GetMetaItemAsync(string name)
        {
            var br = _container.GetBlobReference(name);

            if (!await br.ExistsAsync())
            {
                return(default(T));
            }

            var ret = new T();
            await br.FetchAttributesAsync();

            SetName(ret, name);
            foreach (var keyValuePair in br.Metadata)
            {
                var prop = typeof(T).GetRuntimeProperty(keyValuePair.Key.Remove(0, 5));
                if (prop.PropertyType.GetTypeInfo().IsEnum)
                {
                    prop.SetValue(ret, Enum.Parse(prop.PropertyType, keyValuePair.Value));
                }
                else if (prop.PropertyType == typeof(string))
                {
                    prop.SetValue(ret, keyValuePair.Value);
                }
                else if (prop.PropertyType == typeof(int))
                {
                    prop.SetValue(ret, int.Parse(keyValuePair.Value));
                }
                else if (prop.PropertyType == typeof(long))
                {
                    prop.SetValue(ret, long.Parse(keyValuePair.Value));
                }
                else if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(DateTime?))
                {
                    prop.SetValue(ret, DateTime.FromFileTime(long.Parse(keyValuePair.Value)));
                }
                else if (prop.PropertyType == typeof(bool))
                {
                    prop.SetValue(ret, bool.Parse(keyValuePair.Value));
                }
            }
            return(ret);
        }
Exemplo n.º 10
0
        public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile)
        {
            ICloudBlobContainer container = await GetContainer(folderName);

            var blob = container.GetBlobReference(fileName);
            await blob.DeleteIfExistsAsync();

            await blob.UploadFromStreamAsync(packageFile);

            blob.Properties.ContentType = GetContentType(folderName);
            await blob.SetPropertiesAsync();
        }
        public async Task <Stream> GetFileAsync(string folderName, string fileName)
        {
            if (String.IsNullOrWhiteSpace(folderName))
            {
                throw new ArgumentNullException("folderName");
            }

            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            ICloudBlobContainer container = await GetContainer(folderName);

            var blob = container.GetBlobReference(fileName);

            var stream = new MemoryStream();

            try
            {
                await blob.DownloadToStreamAsync(stream);
            }
            catch (StorageException ex)
            {
                stream.Dispose();

                if (ex.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound)
                {
                    return(null);
                }

                throw;
            }
            catch (TestableStorageClientException ex)
            {
                // This is for unit test only, because we can't construct an
                // StorageException object with the required ErrorCode
                stream.Dispose();

                if (ex.ErrorCode == BlobErrorCodeStrings.BlobNotFound)
                {
                    return(null);
                }

                throw;
            }

            stream.Position = 0;
            return(stream);
        }
Exemplo n.º 12
0
        private async Task <StorageResult> GetBlobContentAsync(string folderName, string fileName, string ifNoneMatch = null)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            var stream = new MemoryStream();

            try
            {
                await blob.DownloadToStreamAsync(
                    stream,
                    accessCondition :
                    ifNoneMatch == null?
                    null :
                    AccessCondition.GenerateIfNoneMatchCondition(ifNoneMatch));
            }
            catch (StorageException ex)
            {
                stream.Dispose();

                if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified)
                {
                    return(new StorageResult(HttpStatusCode.NotModified, null));
                }
                else if (ex.RequestInformation.ExtendedErrorInformation?.ErrorCode == BlobErrorCodeStrings.BlobNotFound)
                {
                    return(new StorageResult(HttpStatusCode.NotFound, null));
                }

                throw;
            }
            catch (TestableStorageClientException ex)
            {
                // This is for unit test only, because we can't construct an
                // StorageException object with the required ErrorCode
                stream.Dispose();

                if (ex.ErrorCode == BlobErrorCodeStrings.BlobNotFound)
                {
                    return(new StorageResult(HttpStatusCode.NotFound, null));
                }

                throw;
            }

            stream.Position = 0;
            return(new StorageResult(HttpStatusCode.OK, stream));
        }
Exemplo n.º 13
0
        private async Task CopyUrlAsync(ICloudBlobContainer container, string oldBaseUrl, string newBaseUrl, string oldUrl, bool gzip)
        {
            await Throttle.WaitAsync();

            try
            {
                _logger.LogInformation("Copying {OldUrl}...", oldUrl);

                var result = await _simpleHttpClient.DeserializeUrlAsync <JToken>(oldUrl);

                var json      = result.GetResultOrThrow();
                var fixedJson = FixUrls(oldBaseUrl, newBaseUrl, json);

                if (!TryGetPath(oldBaseUrl, oldUrl, out var path))
                {
                    throw new InvalidOperationException("The URL does not start with the base URL.");
                }

                var blob = container.GetBlobReference(path);

                blob.Properties.ContentType = "application/json";
                var jsonString = fixedJson.ToString(Formatting.None);
                var bytes      = Encoding.UTF8.GetBytes(jsonString);

                if (gzip)
                {
                    blob.Properties.ContentEncoding = "gzip";
                    using (var compressedStream = new MemoryStream())
                    {
                        using (var gzipStream = new GZipStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true))
                        {
                            gzipStream.Write(bytes, 0, bytes.Length);
                        }

                        bytes = compressedStream.ToArray();
                    }
                }

                using (var memoryStream = new MemoryStream(bytes))
                {
                    await blob.UploadFromStreamAsync(memoryStream, overwrite : true);
                }
            }
            finally
            {
                Throttle.Release();
            }
        }
Exemplo n.º 14
0
        private async Task <ISimpleCloudBlob> GetBlobForUriAsync(string folderName, string fileName, DateTimeOffset?endOfAccess)
        {
            folderName = folderName ?? throw new ArgumentNullException(nameof(folderName));
            fileName   = fileName ?? throw new ArgumentNullException(nameof(fileName));
            if (endOfAccess.HasValue && endOfAccess < DateTimeOffset.UtcNow)
            {
                throw new ArgumentOutOfRangeException(nameof(endOfAccess), $"{nameof(endOfAccess)} is in the past");
            }

            if (!IsPublicContainer(folderName) && endOfAccess == null)
            {
                throw new ArgumentNullException(nameof(endOfAccess), $"{nameof(endOfAccess)} must not be null for non-public containers");
            }

            ICloudBlobContainer container = await GetContainerAsync(folderName);

            return(container.GetBlobReference(fileName));
        }
        public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile, bool overwrite = true)
        {
            ICloudBlobContainer container = await GetContainer(folderName);

            var blob = container.GetBlobReference(fileName);

            if (overwrite)
            {
                await blob.DeleteIfExistsAsync();
            }
            else if (await blob.ExistsAsync())
            {
                throw new InvalidOperationException(
                          String.Format(CultureInfo.CurrentCulture, "There is already a blob with name {0} in container {1}.",
                                        fileName, folderName));
            }

            await blob.UploadFromStreamAsync(packageFile);

            blob.Properties.ContentType = GetContentType(folderName);
            await blob.SetPropertiesAsync();
        }
        public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile, bool overwrite = true)
        {
            ICloudBlobContainer container = await GetContainerAsync(folderName);

            var blob = container.GetBlobReference(fileName);

            try
            {
                await blob.UploadFromStreamAsync(packageFile, overwrite);
            }
            catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int?)HttpStatusCode.Conflict)
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              "There is already a blob with name {0} in container {1}.",
                              fileName,
                              folderName),
                          ex);
            }

            blob.Properties.ContentType = GetContentType(folderName);
            await blob.SetPropertiesAsync();
        }
Exemplo n.º 17
0
        private ISimpleCloudBlob GetBlobReference(ICloudBlobContainer container, string path)
        {
            var blob = container.GetBlobReference(Uri.UnescapeDataString(path));

            return(blob);
        }