Beispiel #1
0
        public async Task <Stream> OpenReadAsync(string fullPath, CancellationToken cancellationToken = default)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

            fullPath = StoragePath.Normalize(fullPath, false);
            GetObjectResponse response = await GetObjectAsync(fullPath).ConfigureAwait(false);

            if (response == null)
            {
                return(null);
            }

            return(new FixedStream(response.ResponseStream, length: response.ContentLength, (Action <FixedStream>)null));
        }
        private Stream CreateStream(string fullPath, bool overwrite = true)
        {
            GenericValidation.CheckBlobFullPath(fullPath);
            if (!Directory.Exists(_directoryFullName))
            {
                Directory.CreateDirectory(_directoryFullName);
            }
            string path = GetFilePath(fullPath);

            Stream s = overwrite ? File.Create(path) : File.OpenWrite(path);

            s.Seek(0, SeekOrigin.End);
            return(s);
        }
Beispiel #3
0
        /// <summary>
        /// S3 doesnt support this natively and will cache everything in MemoryStream until disposed.
        /// </summary>
        public async Task WriteAsync(string fullPath, Stream dataStream, bool append = false,
                                     CancellationToken cancellationToken             = default)
        {
            if (append)
            {
                throw new NotSupportedException();
            }
            GenericValidation.CheckBlobFullPath(fullPath);
            fullPath = StoragePath.Normalize(fullPath, false);

            //http://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

            await _fileTransferUtility.UploadAsync(dataStream, _bucketName, fullPath, cancellationToken).ConfigureAwait(false);
        }
        public async Task <Stream> OpenReadAsync(string id, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(id);

            id = StoragePath.Normalize(id, false);
            GetObjectResponse response = await GetObjectAsync(id);

            if (response == null)
            {
                return(null);
            }

            return(new FixedStream(response.ResponseStream, length: response.ContentLength));
        }
Beispiel #5
0
        public async Task WriteAsync(string id, Stream sourceStream, bool append, CancellationToken cancellationToken)
        {
            if (append)
            {
                throw new NotSupportedException();
            }

            GenericValidation.CheckBlobId(id);
            GenericValidation.CheckSourceStream(sourceStream);

            //http://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

            id = StoragePath.Normalize(id, false);
            await _fileTransferUtility.UploadAsync(sourceStream, _bucketName, id, cancellationToken);
        }
        /// <summary>
        /// Get native Azure blob absolute URI by blob ID
        /// </summary>
        /// <param name="id">Blob ID</param>
        /// <returns>URI of the blob or null if blob doesn't exist</returns>
        public async Task <Uri> GetNativeBlobUriAsync(string id)
        {
            GenericValidation.CheckBlobId(id);

            CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(StoragePath.Normalize(id, false));

            bool exists = await blob.ExistsAsync();

            if (!exists)
            {
                return(null);
            }

            return(blob.Uri);
        }
        public async Task WriteAsync(string fullPath, Stream dataStream, bool append, CancellationToken cancellationToken)
        {
            if (dataStream is null)
            {
                throw new ArgumentNullException(nameof(dataStream));
            }
            GenericValidation.CheckBlobFullPath(fullPath);

            fullPath = StoragePath.Normalize(fullPath, false);

            using (Stream stream = CreateStream(fullPath, !append))
            {
                await dataStream.CopyToAsync(stream).ConfigureAwait(false);
            }
        }
        public async Task <IEnumerable <bool> > ExistsAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            var result = new List <bool>();

            foreach (string id in ids)
            {
                GenericValidation.CheckBlobId(id);
                CloudBlockBlob blob   = _blobContainer.GetBlockBlobReference(StoragePath.Normalize(id, false));
                bool           exists = await blob.ExistsAsync();

                result.Add(exists);
            }

            return(result);
        }
Beispiel #9
0
        public async Task <Stream> OpenReadAsync(string id, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(id);

            DataLakeStoreFileSystemManagementClient client = await GetFsClient();

            try
            {
                return(await client.FileSystem.OpenAsync(_accountName, id));
            }
            catch (CloudException ex) when(ex.Response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new StorageException(ErrorCode.NotFound, ex);
            }
        }
Beispiel #10
0
        public async Task <AccessControl> GetAccessControlAsync(string fullPath, bool getUpn)
        {
            GenericValidation.CheckBlobFullPath(fullPath);
            DecomposePath(fullPath, out string fs, out string rp);

            ApiResponse <string> response = await _restApi.GetPathPropertiesAsync(fs, rp, "getAccessControl", upn : getUpn).ConfigureAwait(false);

            await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);

            return(new AccessControl(
                       response.GetHeader("x-ms-owner"),
                       response.GetHeader("x-ms-group"),
                       response.GetHeader("x-ms-permissions"),
                       response.GetHeader("x-ms-acl")));
        }
Beispiel #11
0
        private async Task <(CloudBlobContainer, string)> GetPartsAsync(string path, bool createContainer = true)
        {
            GenericValidation.CheckBlobId(path);

            path = StoragePath.Normalize(path);
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (_fixedContainer != null)
            {
                return(_fixedContainer, path);
            }

            if (_fixedContainerName != null)
            {
                CloudBlobContainer fxc = _client.GetContainerReference(_fixedContainerName);
                await fxc.CreateIfNotExistsAsync();

                return(fxc, path);
            }

            int idx = path.IndexOf(StoragePath.PathSeparator);

            if (idx == -1)
            {
                throw new ArgumentException("blob path must contain container name", nameof(path));
            }

            string containerName = path.Substring(0, idx);
            string relativePath  = path.Substring(idx + 1);

            if (!_containerNameToContainer.TryGetValue(containerName, out CloudBlobContainer container))
            {
                if (!createContainer)
                {
                    return(null, null);
                }

                container = _client.GetContainerReference(containerName);
                await container.CreateIfNotExistsAsync();

                _containerNameToContainer[containerName] = container;
            }

            return(container, relativePath);
        }
        private async Task DeleteAsync(string fullPath, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPath(fullPath);
            var info = new PathInformation(fullPath);

            Properties properties = await Client.GetPropertiesAsync(info.Filesystem, info.Path, cancellationToken);

            if (properties.IsDirectory)
            {
                await Client.DeleteDirectoryAsync(info.Filesystem, info.Path, true, cancellationToken);

                return;
            }

            await Client.DeleteFileAsync(info.Filesystem, info.Path, cancellationToken);
        }
        public async Task DeleteAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(ids);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync();

                foreach (string id in ids)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToId(id));
                }

                await tx.CommitAsync();
            }
        }
      public Task<Stream> OpenWriteAsync(string fullPath, bool append, CancellationToken cancellationToken)
      {
         GenericValidation.CheckBlobFullPath(fullPath);
         ValidateSecretName(fullPath);
         if (append) throw new ArgumentException("appending to secrets is not supported", nameof(append));

         var callbackStream = new FixedStream(new MemoryStream(), null, async fx =>
         {
            string value = Encoding.UTF8.GetString(((MemoryStream)fx.Parent).ToArray());

            await _vaultClient.SetSecretAsync(_vaultUri, fullPath, value).ConfigureAwait(false);
         });

         return Task.FromResult<Stream>(callbackStream);
         
      }
        public async Task DeleteAsync(IEnumerable <string> fullPaths, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPaths(fullPaths);

            using (ServiceFabricTransaction tx = GetTransaction())
            {
                IReliableDictionary <string, byte[]> coll = await OpenCollectionAsync().ConfigureAwait(false);

                foreach (string fullPath in fullPaths)
                {
                    await coll.TryRemoveAsync(tx.Tx, ToFullPath(fullPath)).ConfigureAwait(false);
                }

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
        private async Task <bool> ExistsAsync(string fullPath)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

            fullPath = NormaliseSecretName(fullPath);

            try
            {
                await _client.GetSecretAsync(fullPath).ConfigureAwait(false);
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                return(false);
            }

            return(true);
        }
Beispiel #17
0
        private async Task <bool> ExistsAsync(AmazonS3Client client, string fullPath, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

            try
            {
                fullPath = StoragePath.Normalize(fullPath, true);
                await client.GetObjectMetadataAsync(_bucketName, fullPath, cancellationToken).ConfigureAwait(false);

                return(true);
            }
            catch (AmazonS3Exception ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
            }

            return(false);
        }
Beispiel #18
0
        public async Task <IReadOnlyCollection <bool> > ExistsAsync(IEnumerable <string> fullPaths, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPaths(fullPaths);

            AdlsClient client = await GetAdlsClientAsync().ConfigureAwait(false);

            var result = new List <bool>();

            foreach (string fullPath in fullPaths)
            {
                bool exists = client.CheckExists(fullPath);

                result.Add(exists);
            }

            return(result);
        }
Beispiel #19
0
        public async Task <Stream> OpenReadAsync(string fullPath, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

            AdlsClient client = await GetAdlsClientAsync().ConfigureAwait(false);

            try
            {
                AdlsInputStream response = await client.GetReadStreamAsync(fullPath, cancellationToken).ConfigureAwait(false);

                return(response);
            }
            catch (AdlsException ex) when(ex.HttpStatus == HttpStatusCode.NotFound)
            {
                return(null);
            }
        }
        /// <summary>
        /// Checks if files exist on disk
        /// </summary>
        public Task <IReadOnlyCollection <bool> > ExistsAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            var result = new List <bool>();

            if (ids != null)
            {
                GenericValidation.CheckBlobId(ids);

                foreach (string id in ids)
                {
                    bool exists = File.Exists(GetFilePath(StoragePath.Normalize(id, false)));
                    result.Add(exists);
                }
            }

            return(Task.FromResult((IReadOnlyCollection <bool>)result));
        }
Beispiel #21
0
        public VendaItem(Produto produto, decimal valorUnitario, int quantidade)
        {
            /* Produto */
            GenericValidation.ObjectNotNull(produto, EXCEPTION_MESSAGE_VENDA_ITEM_PRODUTO_REQUIRED);

            /* Valor unitário */
            GenericValidation.ObjectNotNull(valorUnitario, EXCEPTION_MESSAGE_VENDA_ITEM_VALOR_UNITARIO_REQUIRED);
            GenericValidation.DecimalMinValue(valorUnitario, (decimal)0.01, EXCEPTION_MESSAGE_VENDA_ITEM_VALOR_UNITARIO_MIN_VALUE);

            /* Quantidade */
            GenericValidation.ObjectNotNull(quantidade, EXCEPTION_MESSAGE_VENDA_ITEM_QUANTIDADE_REQUIRED);
            GenericValidation.IntMinValue(quantidade, 1, EXCEPTION_MESSAGE_VENDA_ITEM_QUANTIDADE_MIN_VALUE);

            Produto       = produto;
            ValorUnitario = valorUnitario;
            Quantidade    = quantidade;
        }
        /// <summary>
        /// Returns the list of blob names in this storage, optionally filtered by prefix
        /// </summary>
        public Task <IReadOnlyCollection <Blob> > ListAsync(ListOptions options, CancellationToken cancellationToken)
        {
            if (options == null)
            {
                options = new ListOptions();
            }

            GenericValidation.CheckBlobPrefix(options.FilePrefix);

            if (!Directory.Exists(_directoryFullName))
            {
                return(Task.FromResult <IReadOnlyCollection <Blob> >(new List <Blob>()));
            }

            string fullPath = GetFolder(options?.FolderPath, false);

            if (fullPath == null)
            {
                return(Task.FromResult <IReadOnlyCollection <Blob> >(new List <Blob>()));
            }

            string[] fileIds = Directory.GetFiles(
                fullPath,
                string.IsNullOrEmpty(options.FilePrefix)
               ? "*"
               : options.FilePrefix + "*",
                options.Recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

            string[] directoryIds = Directory.GetDirectories(
                fullPath,
                string.IsNullOrEmpty(options.FilePrefix)
                  ? "*"
                  : options.FilePrefix + "*",
                options.Recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

            var result = new List <Blob>();

            result.AddRange(directoryIds.Select(id => ToBlobItem(id, BlobItemKind.Folder, options.IncludeAttributes)));
            result.AddRange(
                fileIds.Where(fid => !fid.EndsWith(AttributesFileExtension)).Select(id => ToBlobItem(id, BlobItemKind.File, options.IncludeAttributes)));
            result = result
                     .Where(i => options.BrowseFilter == null || options.BrowseFilter(i))
                     .Take(options.MaxResults == null ? int.MaxValue : options.MaxResults.Value)
                     .ToList();
            return(Task.FromResult <IReadOnlyCollection <Blob> >(result));
        }
Beispiel #23
0
        private async Task <Blob?> GetBlobAsync(string fullPath)
        {
            GenericValidation.CheckBlobFullPath(fullPath);
            fullPath = StoragePath.Normalize(fullPath);
            AmazonS3Client amazonS3Client = await this.GetClientAsync().ConfigureAwait(false);

            try
            {
                return((await amazonS3Client.GetObjectMetadataAsync(this._bucketName, fullPath)
                        .ConfigureAwait(false)).ToBlob(fullPath));
            }
            catch (AmazonS3Exception ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
            }

            return(null);
        }
        public async Task <Stream> OpenReadAsync(string id, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(id);

            AdlsClient client = await GetAdlsClient();

            try
            {
                AdlsInputStream response = await client.GetReadStreamAsync(id, cancellationToken);

                return(response);
            }
            catch (AdlsException ex) when(ex.HttpStatus == HttpStatusCode.NotFound)
            {
                return(null);
            }
        }
        public Task WriteAsync(string fullPath, Stream dataStream, bool append, CancellationToken cancellationToken)
        {
            if (dataStream is null)
            {
                throw new ArgumentNullException(nameof(dataStream));
            }
            GenericValidation.CheckBlobFullPath(fullPath);

            fullPath = StoragePath.Normalize(fullPath);

            using (Stream stream = CreateStream(fullPath, !append))
            {
                dataStream.CopyTo(stream);
            }

            return(Task.FromResult(true));
        }
        public async Task <IReadOnlyCollection <bool> > ExistsAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(ids);

            AdlsClient client = await GetAdlsClient();

            var result = new List <bool>();

            foreach (string id in ids)
            {
                bool exists = client.CheckExists(id);

                result.Add(exists);
            }

            return(result);
        }
        /// <summary>
        /// Gets file metadata
        /// </summary>
        public Task <IEnumerable <BlobMeta> > GetMetaAsync(IEnumerable <string> ids, CancellationToken cancellationToken)
        {
            if (ids == null)
            {
                return(null);
            }

            GenericValidation.CheckBlobId(ids);

            var result = new List <BlobMeta>();

            foreach (string id in ids)
            {
                result.Add(BlobMetaFromId(id));
            }

            return(Task.FromResult((IEnumerable <BlobMeta>)result));
        }
        protected override async Task <bool> ExistsAsync(string fullPath, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

            try
            {
                await _client.GetObjectAsync(
                    _bucketName, NormalisePath(fullPath),
                    null,
                    cancellationToken).ConfigureAwait(false);

                return(true);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }
        }
        public Task <Stream> OpenWriteAsync(string id, bool append, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobId(id);
            ValidateSecretName(id);
            if (append)
            {
                throw new ArgumentException("appending to secrets is not supported", nameof(append));
            }

            var callbackStream = new FixedStream(new MemoryStream(), null, fx =>
            {
                string value = Encoding.UTF8.GetString(((MemoryStream)fx.Parent).ToArray());

                _vaultClient.SetSecretAsync(_vaultUri, id, value).Wait();
            });

            return(Task.FromResult <Stream>(callbackStream));
        }
        public async Task <Stream> OpenReadAsync(string fullPath, CancellationToken cancellationToken)
        {
            GenericValidation.CheckBlobFullPath(fullPath);
            fullPath = NormaliseSecretName(fullPath);

            try
            {
                Response <KeyVaultSecret> secret = await _client.GetSecretAsync(fullPath, cancellationToken : cancellationToken).ConfigureAwait(false);

                string value = secret.Value.Value;

                return(value.ToMemoryStream());
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                return(null);
            }
        }