private string GetFolder(string path, bool createIfNotExists)
        {
            if (path == null)
            {
                return(_directory.FullName);
            }
            string[] parts = StoragePath.Split(path);

            string fullPath = _directory.FullName;

            foreach (string part in parts)
            {
                fullPath = Path.Combine(fullPath, part);
            }

            if (!Directory.Exists(fullPath))
            {
                if (createIfNotExists)
                {
                    Directory.CreateDirectory(fullPath);
                }
                else
                {
                    return(null);
                }
            }

            return(fullPath);
        }
示例#2
0
        private async Task <CloudFile> GetFileReferenceAsync(string fullPath, bool createParents, CancellationToken cancellationToken)
        {
            string[] parts = StoragePath.Split(fullPath);
            if (parts.Length == 0)
            {
                return(null);
            }

            string shareName = parts[0];

            CloudFileShare share = _client.GetShareReference(shareName);

            if (createParents)
            {
                await share.CreateIfNotExistsAsync(cancellationToken).ConfigureAwait(false);
            }

            CloudFileDirectory dir = share.GetRootDirectoryReference();

            for (int i = 1; i < parts.Length - 1; i++)
            {
                string sub = parts[i];
                dir = dir.GetDirectoryReference(sub);

                if (createParents)
                {
                    await dir.CreateIfNotExistsAsync().ConfigureAwait(false);
                }
            }

            return(dir.GetFileReference(parts[parts.Length - 1]));
        }
示例#3
0
文件: Blob.cs 项目: myso42/storage
        /// <summary>
        /// Create a new instance
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="kind"></param>
        public Blob(string fullPath, BlobItemKind kind = BlobItemKind.File)
        {
            string path = StoragePath.Normalize(fullPath);

            string[] parts = StoragePath.Split(path);

            Name       = parts.Last();
            FolderPath = StoragePath.GetParent(path);

            Kind = kind;
        }
示例#4
0
        private static void GetScopeAndKey(string path, out string scope, out string key)
        {
            string[] parts = StoragePath.Split(path);
            if (parts.Length != 2)
            {
                throw new ArgumentException($"path should contain exactly scope and secret name", nameof(path));
            }

            scope = parts[0];
            key   = parts[1];
        }
示例#5
0
        private async Task <IReadOnlyCollection <Blob> > ListPathAsync(string path, int?maxResults, ListOptions options, CancellationToken cancellationToken)
        {
            //get filesystem name and folder path
            string[] parts = StoragePath.Split(path);

            string fs           = parts[0];
            string relativePath = StoragePath.Combine(parts.Skip(1));

            var list = new List <Gen2Path>();

            try
            {
                string continuation = null;
                do
                {
                    string continuationParam = continuation == null ? null : $"&continuation={continuation}";

                    (PathList pl, IDictionary <string, string> responseHeaders) = await InvokeExtraAsync <PathList>(
                        $"{fs}?resource=filesystem&directory={relativePath.UrlEncode()}&recursive={options.Recurse}{continuationParam}",
                        RequestMethod.Get,
                        cancellationToken).ConfigureAwait(false);

                    list.AddRange(pl.Paths);

                    responseHeaders.TryGetValue("x-ms-continuation", out continuation);
                } while(continuation != null);
            }
            catch (RequestFailedException ex) when(ex.ErrorCode == "PathNotFound" || ex.ErrorCode == "FilesystemNotFound")
            {
                // trying to list a path which doesn't exist, just return an empty result
                return(new List <Blob>());
            }

            IEnumerable <Blob> result = list.Select(p => AzConvert.ToBlob(fs, p));

            if (options.FilePrefix != null)
            {
                result = result.Where(b => b.IsFolder || b.Name.StartsWith(options.FilePrefix));
            }

            if (options.BrowseFilter != null)
            {
                result = result.Where(b => options.BrowseFilter(b));
            }

            if (maxResults != null)
            {
                result = result.Take(maxResults.Value);
            }

            return(result.ToList());
        }
示例#6
0
        /// <summary>
        /// Create a new instance
        /// </summary>
        /// <param name="fullId"></param>
        /// <param name="kind"></param>
        public BlobId(string fullId, BlobItemKind kind = BlobItemKind.File)
        {
            string path = StoragePath.Normalize(fullId);

            string[] parts = StoragePath.Split(path);

            Id         = parts.Last();
            FolderPath = parts.Length > 1
            ? StoragePath.Combine(parts.Take(parts.Length - 1))
            : StoragePath.PathStrSeparator;

            Kind = kind;
        }
示例#7
0
        public async Task GetBlob_Root_valid_returns_some()
        {
            string id = RandomBlobPath();

            string root = StoragePath.Split(id)[0];

            try
            {
                Blob rb = await _storage.GetBlobAsync(root);
            }
            catch (NotSupportedException)
            {
            }
        }
        private void DecomposePath(string path, out string filesystemName, out string relativePath)
        {
            GenericValidation.CheckBlobFullPath(path);
            string[] parts = StoragePath.Split(path);

            if (parts.Length == 1)
            {
                throw new ArgumentException($"path {path} must include filesystem name as root folder", nameof(path));
            }

            filesystemName = parts[0];

            relativePath = StoragePath.Combine(parts.Skip(1));
        }
示例#9
0
        private void DecomposePath(string path, out string filesystemName, out string relativePath, bool requireRelativePath = true)
        {
            GenericValidation.CheckBlobFullPath(path);
            string[] parts = StoragePath.Split(path);

            if (requireRelativePath && parts.Length < 2)
            {
                throw new ArgumentException($"path '{path}' must include filesystem name as root folder, i.e. 'filesystem/path'", nameof(path));
            }

            filesystemName = parts[0];

            relativePath = StoragePath.Combine(parts.Skip(1));
        }
示例#10
0
        /// <summary>
        /// Changes full path of this blob without modifying any other property
        /// </summary>
        public void SetFullPath(string fullPath)
        {
            string path = StoragePath.Normalize(fullPath);

            if (StoragePath.IsRootPath(path))
            {
                Name       = StoragePath.RootFolderPath;
                FolderPath = StoragePath.RootFolderPath;
            }
            else
            {
                string[] parts = StoragePath.Split(path);

                Name       = parts.Last();
                FolderPath = StoragePath.GetParent(path);
            }
        }
示例#11
0
        protected override async Task <IReadOnlyCollection <Blob> > ListAtAsync(
            string path, ListOptions options, CancellationToken cancellationToken)
        {
            var r = new List <Blob>();

            // listing from "/secrets"
            if (StoragePath.IsRootPath(path))
            {
                IEnumerable <SecretScope> scopes = await _api.ListScopes().ConfigureAwait(false);

                foreach (SecretScope scope in scopes)
                {
                    var scopeBlob = new Blob(scope.Name, BlobItemKind.Folder);
                    scopeBlob.TryAddProperties(
                        "Backend", scope.BackendType,
                        "ObjectType", "secretScope");

                    IEnumerable <AclItem> acl = await _api.ListSecretAcl(scope.Name).ConfigureAwait(false);

                    scopeBlob.Properties.Add("ACL", string.Join(";", acl.Select(a => $"{a.Principal}:{a.Permission}")));
                    r.Add(scopeBlob);
                }
                return(r);
            }

            // listing from "/secrets/[scope name]

            string scopeName   = StoragePath.Split(path)[0];
            var    secretNames = (await _api.ListSecrets(scopeName).ConfigureAwait(false)).ToList();

            foreach (SecretMetadata sm in secretNames)
            {
                var sb = new Blob(scopeName, sm.Key, BlobItemKind.File);
                sb.LastModificationTime = sm.LastUpdatedTimestamp;
                sb.TryAddProperties(
                    "ObjectType", "secret");
                r.Add(sb);
            }
            return(r);
        }
示例#12
0
        private Task <CloudFileDirectory> GetDirectoryReferenceAsync(string fullPath, CancellationToken cancellationToken)
        {
            string[] parts = StoragePath.Split(fullPath);
            if (parts.Length == 0)
            {
                return(null);
            }

            string shareName = parts[0];

            CloudFileShare share = _client.GetShareReference(shareName);

            CloudFileDirectory dir = share.GetRootDirectoryReference();

            for (int i = 1; i < parts.Length; i++)
            {
                string sub = parts[i];
                dir = dir.GetDirectoryReference(sub);
            }

            return(Task.FromResult(dir));
        }
示例#13
0
        private async Task <IReadOnlyCollection <Blob> > ListPathAsync(string path, int?maxResults, ListOptions options, CancellationToken cancellationToken)
        {
            //get filesystem name and folder path
            string[] parts = StoragePath.Split(path);

            string fs           = parts[0];
            string relativePath = StoragePath.Combine(parts.Skip(1));

            PathList list;

            try
            {
                list = await _api.ListPathAsync(fs, relativePath, recursive : options.Recurse).ConfigureAwait(false);
            }
            catch (ApiException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // specified path is not found, nothing serious
                return(new List <Blob>());
            }

            IEnumerable <Blob> result = list.Paths.Select(p => LConvert.ToBlob(fs, p));

            if (options.FilePrefix != null)
            {
                result = result.Where(b => b.IsFolder || b.Name.StartsWith(options.FilePrefix));
            }

            if (options.BrowseFilter != null)
            {
                result = result.Where(b => options.BrowseFilter(b));
            }

            if (maxResults != null)
            {
                result = result.Take(maxResults.Value);
            }

            return(result.ToList());
        }
示例#14
0
        /// <summary>
        /// Mounts a storage to virtual path
        /// </summary>
        /// <param name="path"></param>
        /// <param name="storage"></param>
        public void Mount(string path, IBlobStorage storage)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (storage is null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            path = StoragePath.Normalize(path);

            _mountPoints.Add(new Blob(path)
            {
                Tag = storage
            });

            string absPath = null;

            string[] parts = StoragePath.Split(path);

            if (parts.Length == 0) //mount at root
            {
                MountPath(path, storage, true);
            }
            else
            {
                for (int i = 0; i < parts.Length; i++)
                {
                    absPath = StoragePath.Combine(absPath, parts[i]);

                    MountPath(absPath, storage, i == parts.Length - 1);
                }
            }
        }
示例#15
0
        private async Task <(BlobContainerClient, string)> GetPartsAsync(string fullPath, bool createContainer = true)
        {
            GenericValidation.CheckBlobFullPath(fullPath);

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

            string containerName, relativePath;

            if (_containerName == null)
            {
                string[] parts = StoragePath.Split(fullPath);

                if (parts.Length == 1)
                {
                    containerName = parts[0];
                    relativePath  = string.Empty;
                }
                else
                {
                    containerName = parts[0];
                    relativePath  = StoragePath.Combine(parts.Skip(1)).Substring(1);
                }
            }
            else
            {
                containerName = _containerName;
                relativePath  = fullPath;
            }

            if (!_containerNameToContainerClient.TryGetValue(containerName, out BlobContainerClient container))
            {
                container = _client.GetBlobContainerClient(containerName);
                if (_containerName == null)
                {
                    try
                    {
                        //check if container exists
                        await container.GetPropertiesAsync().ConfigureAwait(false);
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == "ContainerNotFound")
                    {
                        if (createContainer)
                        {
                            await container.CreateIfNotExistsAsync().ConfigureAwait(false);
                        }
                        else
                        {
                            return(null, null);
                        }
                    }
                }

                _containerNameToContainerClient[containerName] = container;
            }

            return(container, relativePath);
        }