예제 #1
0
        internal void DownloadDirectory(CloudFileDirectory dir, string destination)
        {
            destination = Path.Combine(destination, dir.Name);
            Directory.CreateDirectory(destination);
            var items = dir.ListFilesAndDirectories();

            this.HandleItems(items,
                             (f) =>
            {
                f.DownloadToFile(PathResolver.Combine(destination, f.Name), FileMode.CreateNew);
            },
                             (d) =>
            {
                DownloadDirectory(d, destination);
            },
                             (s) => { });
        }
예제 #2
0
        public override void RemoveItem(string path, bool recurse)
        {
            var r = AzureTablePathResolver.ResolvePath(this.Client, path);

            if (r.Parts.Count == 1) //removing table
            {
                var table = this.Client.GetTableReference(r.Parts.Last());
                table.Delete();
                return;
            }
            else if (r.PathType != PathType.Invalid && r.Parts.Count > 1)
            {
                var entities = ListEntities(r.Table, r.Query);

                foreach (var e in entities)
                {
                    var o = TableOperation.Delete(e);
                    r.Table.Execute(o);
                    this.RootProvider.WriteWarning(string.Format("Entity {0} # {1} is removed.", e.PartitionKey, e.RowKey));
                }
                return;
            }

            if (r.PathType == PathType.Invalid)
            {
                var propertyName = Path.GetFileName(path);
                var entityPath   = PathResolver.GetParentPath(path);
                r = AzureTablePathResolver.ResolvePath(this.Client, entityPath); //override path

                var entities = ListEntities(r.Table, r.Query);

                foreach (var e in entities)
                {
                    if (e.Properties.ContainsKey(propertyName))
                    {
                        e.Properties.Remove(propertyName);
                        var o = TableOperation.Replace(e);
                        r.Table.Execute(o);
                        this.RootProvider.WriteWarning(string.Format("Entity {0} # {1} is updated.", e.PartitionKey, e.RowKey));
                    }
                }
                return;
            }

            this.RootProvider.WriteWarning("Nothing to do");
        }
예제 #3
0
        internal void Upload(string localPath, string targePath)
        {
            var r = AzureFilePathResolver.ResolvePath(this.Client, targePath, skipCheckExistence: false);
            var localIsDirectory = Directory.Exists(localPath);
            var local            = PathResolver.SplitPath(localPath);

            switch (r.PathType)
            {
            case PathType.AzureFileRoot:
                if (localIsDirectory)
                {
                    var share = CreateShare(local.Last());
                    var dir   = share.GetRootDirectoryReference();
                    foreach (var f in Directory.GetFiles(localPath))
                    {
                        UploadFile(f, dir);
                    }

                    foreach (var d in Directory.GetDirectories(localPath))
                    {
                        UploadDirectory(d, dir);
                    }
                }
                else
                {
                    throw new Exception("Cannot upload file as file share.");
                }
                break;

            case PathType.AzureFileDirectory:
                if (localIsDirectory)
                {
                    UploadDirectory(localPath, r.Directory);
                }
                else
                {
                    UploadFile(localPath, r.Directory);
                }
                break;

            case PathType.AzureFile:
            default:
                break;
            }
        }
예제 #4
0
        protected override bool HasChildItems(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(PathResolver.Drives.Count > 0);
            }

            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return(false);
            }

            return(drive.HasChildItems(PathResolver.GetSubpath(path)));
        }
예제 #5
0
        private void DownloadShare(CloudFileShare share, string destination)
        {
            destination = PathResolver.Combine(destination, share.Name);
            Directory.CreateDirectory(destination);

            var dir   = share.GetRootDirectoryReference();
            var items = dir.ListFilesAndDirectories();

            this.HandleItems(items,
                             (f) =>
            {
                f.DownloadToFile(PathResolver.Combine(destination, f.Name), FileMode.CreateNew);
            },
                             (d) =>
            {
                DownloadDirectory(d, destination);
            },
                             (s) => { });
        }
예제 #6
0
        public override bool ItemExists(string path)
        {
            if (PathResolver.IsLocalPath(path))
            {
                path = PathResolver.ConvertToRealLocalPath(path);
                return(File.Exists(path) || Directory.Exists(path));
            }

            try
            {
                var r      = AzureFilePathResolver.ResolvePath(this.Client, path, skipCheckExistence: false);
                var exists = r.Exists();
                return(exists);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #7
0
        public override bool IsItemContainer(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(true);
            }

            try
            {
                var r = AzureQueuePathResolver.ResolvePath(this.Client, path);
                return(r.Exists());
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #8
0
        protected override bool IsValidPath(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(true);
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return(false);
            }

            //delegate it
            return(drive.IsValidPath(PathResolver.GetSubpath(path)));
        }
예제 #9
0
        protected override void NewItem(
            string path,
            string type,
            object newItemValue)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return;
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                drive = DriveFactory.CreateInstance(type, newItemValue, parts[0]);
                if (drive == null)
                {
                    WriteError(new ErrorRecord(
                                   new Exception("Unregcognized type"),
                                   "NewItem type",
                                   ErrorCategory.InvalidArgument,
                                   ""));
                    return;
                }
                else
                {
                    PathResolver.Drives.Add(parts[0], drive);
                }
            }
            else
            {
                if (parts.Count > 1)
                {
                    //delegate it if needed
                    drive.NewItem(PathResolver.GetSubpath(path), type, newItemValue);
                }
            }
        }
예제 #10
0
        protected void CopyItemInternal(string path, string destination, bool recurse, bool deleteOriginal = false, bool exactDest = false)
        {
            var sourceDrive = PathResolver.FindDrive(path);
            var targetDrive = PathResolver.FindDrive(destination);

            if (sourceDrive is AzureTableServiceDriveInfo && targetDrive is AzureTableServiceDriveInfo)
            {
                var sd = sourceDrive as AzureTableServiceDriveInfo;
                var td = targetDrive as AzureTableServiceDriveInfo;
                sd.CopyTo(PathResolver.GetSubpath(path), td, PathResolver.GetSubpath(destination), deleteOriginal);
                return;
            }

            if (sourceDrive.IsItemContainer(PathResolver.GetSubpath(path)))
            {
                if (!recurse)
                {
                    throw new Exception(path + " is a container");
                }

                if (targetDrive.IsItemContainer(PathResolver.GetSubpath(destination)) && !exactDest)
                {
                    destination = MakePath(destination, PathResolver.SplitPath(path).Last());
                }

                targetDrive.NewItem(PathResolver.GetSubpath(destination), "Directory", null);
                foreach (var c in sourceDrive.GetChildNamesList(PathResolver.GetSubpath(path), PathType.Item))
                {
                    CopySingleItem(MakePath(path, c), MakePath(destination, c), exactDest: true);
                }
                foreach (var c in sourceDrive.GetChildNamesList(PathResolver.GetSubpath(path), PathType.Container))
                {
                    CopyItemInternal(MakePath(path, c), MakePath(destination, c), recurse, deleteOriginal, exactDest: true);
                }
            }
            else
            {
                CopySingleItem(path, destination);
            }
        }
예제 #11
0
        internal void Download(string path, string destination)
        {
            var r           = AzureFilePathResolver.ResolvePath(this.Client, path, skipCheckExistence: false);
            var targetIsDir = Directory.Exists(destination);

            switch (r.PathType)
            {
            case PathType.AzureFile:
                if (targetIsDir)
                {
                    destination = PathResolver.Combine(destination, r.Parts.Last());
                }

                r.File.DownloadToFile(destination, FileMode.CreateNew);
                break;

            case PathType.AzureFileDirectory:
                if (string.IsNullOrEmpty(r.Directory.Name))
                {
                    //at share level
                    this.DownloadShare(r.Share, destination);
                }
                else
                {
                    DownloadDirectory(r.Directory, destination);
                }
                break;

            case PathType.AzureFileRoot:
                var shares = this.Client.ListShares();
                foreach (var share in shares)
                {
                    this.DownloadShare(share, destination);
                }
                break;

            default:
                break;
            }
        }
예제 #12
0
        private void FillDataInPageBlob(string path, long count)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetPageBlobReference(PathResolver.GetSubpath(path));
                if (!blob.Exists())
                {
                    this.RootProvider.WriteWarning("PageBlob " + path + " does not exist.");
                    return;
                }

                blob.FetchAttributes();
                var total  = blob.Properties.Length / 512;
                var data   = new byte[512];
                var random = new Random();
                random.NextBytes(data);

                this.RootProvider.WriteWarning("Start writing pages...");
                var tasks = new Task[count];

                for (var i = 0; i < count; ++i)
                {
                    var p = (long)(random.NextDouble() * total);

                    var task = blob.WritePagesAsync(new MemoryStream(data), p * 512, null);
                    tasks[i] = task;
                }

                this.RootProvider.WriteWarning("Waiting writing pages...");
                Task.WaitAll(tasks);
                this.RootProvider.WriteWarning("Completed writing pages...");
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the page blob path.");
            }
        }
예제 #13
0
        public override void GetChildItems(string path, bool recurse)
        {
            var folders = recurse ? new List <string>() : null;

            var items = this.ListItems(path);

            this.HandleItems(items,
                             (f) =>
            {
                f.FetchAttributes();
                this.RootProvider.WriteItemObject(f, path, true);
            },
                             (d) =>
            {
                this.RootProvider.WriteItemObject(d, path, true);
                if (recurse)
                {
                    var p = PathResolver.Combine(path, d.Name);
                    folders.Add(p);
                }
            },
                             (s) =>
            {
                this.RootProvider.WriteItemObject(s, path, true);
                if (recurse)
                {
                    var p = PathResolver.Combine(path, s.Name);
                    folders.Add(p);
                }
            });

            if (recurse && folders != null)
            {
                foreach (var f in folders)
                {
                    GetChildItems(f, recurse);
                }
            }
        }
예제 #14
0
        public override void GetChildItems(string path, bool recurse)
        {
            var folders = recurse ? new List <string>() : null;

            var items = this.ListItems(path);

            this.HandleItems(items,
                             (b) =>
            {
                this.RootProvider.WriteItemObject(b, path, true);
            },
                             (d) =>
            {
                this.RootProvider.WriteItemObject(d, path, true);
                if (recurse)
                {
                    var name = PathResolver.SplitPath(d.Prefix).Last();
                    var p    = PathResolver.Combine(path, name);
                    folders.Add(p);
                }
            },
                             (c) =>
            {
                this.RootProvider.WriteItemObject(c, path, true);
                if (recurse)
                {
                    var p = PathResolver.Combine(path, c.Name);
                    folders.Add(p);
                }
            });

            if (recurse && folders != null)
            {
                foreach (var f in folders)
                {
                    GetChildItems(f, recurse);
                }
            }
        }
예제 #15
0
        private void ShowCopyStatus(string path)
        {
            var parts     = PathResolver.SplitPath(path);
            var container = this.Client.GetContainerReference(parts[0]);
            var destBlob  = container.GetBlobReference(PathResolver.GetSubpath(path));

            destBlob.FetchAttributes();
            var state = destBlob.CopyState;

            if (state != null)
            {
                this.RootProvider.WriteWarning(string.Format("Copy is {0}\r\nId: {1}\r\nBytes Copied: {2}/{3}",
                                                             state.Status.ToString(),
                                                             state.CopyId,
                                                             state.BytesCopied,
                                                             state.TotalBytes));
            }
            else
            {
                this.RootProvider.WriteWarning(string.Format("CopyStatus is null"));
            }
        }
예제 #16
0
        protected override void GetChildNames(string path, ReturnContainers returnContainers)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                foreach (var pair in PathResolver.Drives)
                {
                    WriteItemObject(pair.Key, "/", true);
                }
            }
            else
            {
                var drive = GetDrive(parts[0]);
                if (drive == null)
                {
                    return;
                }

                drive.GetChildNames(PathResolver.GetSubpath(path), returnContainers);
            }
        }
예제 #17
0
        internal void CopyTo(string path, AzureTableServiceDriveInfo target, string copyPath, bool deleteOriginal = false)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                this.RootProvider.WriteWarning("Not supported to copy multiple tables.");
                return;
            }

            var targetRef = AzureTablePathResolver.ResolvePath(target.Client, copyPath);

            if (targetRef.PathType == PathType.AzureTableRoot)
            {
                this.RootProvider.WriteWarning("Must specify the target table.");
                return;
            }

            var items = this.ListItems(path);

            foreach (ITableEntity i in items)
            {
                var o = TableOperation.InsertOrReplace(i);
                targetRef.Table.Execute(o);
                this.RootProvider.WriteWarning(string.Format("Entity {0} # {1} is inserted", i.PartitionKey, i.RowKey));
            }

            if (deleteOriginal)
            {
                var sourceRef = AzureTablePathResolver.ResolvePath(this.Client, path);
                foreach (ITableEntity i in items)
                {
                    var o = TableOperation.Delete(i);
                    sourceRef.Table.Execute(o);
                    this.RootProvider.WriteWarning(string.Format("Source Entity {0} # {1} is deleted", i.PartitionKey, i.RowKey));
                }
            }
        }
예제 #18
0
 public override void NewItem(
     string path,
     string type,
     object newItemValue)
 {
     if (string.Equals(type, "Directory", StringComparison.InvariantCultureIgnoreCase))
     {
         this.CreateDirectory(path);
     }
     else if (string.Equals(type, "EmptyFile", StringComparison.InvariantCultureIgnoreCase))
     {
         if (newItemValue != null)
         {
             var size = 0L;
             if (long.TryParse(newItemValue.ToString(), out size))
             {
                 this.CreateEmptyFile(path, size);
             }
             else
             {
                 this.CreateEmptyFile(path, 0);
             }
         }
     }
     else
     {
         var parts = PathResolver.SplitPath(path);
         if (parts.Count == 1)
         {
             this.CreateShare(parts[0]);
         }
         else
         {
             this.CreateFile(path, newItemValue.ToString());
         }
     }
 }
예제 #19
0
        public override bool IsItemContainer(string path)
        {
            if (PathResolver.IsLocalPath(path))
            {
                return(true);
            }

            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(true);
            }

            try
            {
                var r = AzureFilePathResolver.ResolvePath(this.Client, path, hint: PathType.AzureFileDirectory, skipCheckExistence: false);
                return(r.Exists());
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #20
0
        protected void CopySingleItem(string path, string destination, bool exactDest = false)
        {
            var    sourceDrive = PathResolver.FindDrive(path);
            var    targetDrive = PathResolver.FindDrive(destination);
            var    sourcePath = PathResolver.GetSubpath(path);
            string targetDir, targetFile;

            if (targetDrive.IsItemContainer(PathResolver.GetSubpath(destination)) && !exactDest)
            {
                targetDir  = PathResolver.GetSubpath(destination);
                targetFile = PathResolver.SplitPath(path).Last();
            }
            else
            {
                targetDir  = PathResolver.GetSubpathDirectory(destination);
                targetFile = PathResolver.SplitPath(destination).Last();
            }
            using (var sourceStream = sourceDrive.CopyFrom(sourcePath))
                using (var targetStream = targetDrive.CopyTo(targetDir, targetFile))
                {
                    sourceStream.Seek(0, SeekOrigin.Begin);
                    sourceStream.CopyTo(targetStream);
                }
        }
예제 #21
0
        private void ShowEtag(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetBlobReference(PathResolver.GetSubpath(path));
                blob.FetchAttributes();
                this.RootProvider.WriteItemObject(blob.Properties.ETag, path, false);
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the target blob.");
            }
        }
예제 #22
0
        public override void NewItem(
            string path,
            string type,
            object newItemValue)
        {
            if (string.Equals(type, "Directory", StringComparison.InvariantCultureIgnoreCase))
            {
                this.CreateDirectory(path);
            }
            else if (string.Equals(type, "PageBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                if (newItemValue != null)
                {
                    var size = 0L;
                    if (long.TryParse(newItemValue.ToString(), out size))
                    {
                        this.CreateEmptyFile(path, size);
                    }
                    else
                    {
                        this.CreateEmptyFile(path, 0);
                    }
                }
            }
            else if (string.Equals(type, "RandomPages", StringComparison.InvariantCultureIgnoreCase))
            {
                //fill page blob with random data, each page data is 512Byte, and count is required
                //e.g. ni PageBlob -type RandomPages -value <count>
                if (newItemValue != null)
                {
                    var size = 0L;
                    if (long.TryParse(newItemValue.ToString(), out size))
                    {
                        this.FillDataInPageBlob(path, size);
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Value is required.");
                    }
                }
            }
            else if (string.Equals(type, "ListPages", StringComparison.InvariantCultureIgnoreCase))
            {
                //List page ranges in page blob
                //e.g. ni pageBlob -type ListPages
                this.ListPageRanges(path);
            }
            else if (string.Equals(type, "ContainerSAStoken", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var containerName = parts[0];
                    var container     = this.Client.GetContainerReference(containerName);
                    var policyName    = string.Empty;
                    var policy        = CreateBlobPolicy(newItemValue as string, ref policyName);

                    if (policyName != string.Empty) //policy-based SAStoken
                    {
                        var token = container.GetSharedAccessSignature(policy, policyName);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                    else
                    {
                        var token = container.GetSharedAccessSignature(policy);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                }
            }
            else if (string.Equals(type, "BlobSAStoken", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    var containerName = parts[0];
                    var container     = this.Client.GetContainerReference(containerName);
                    var blob          = container.GetBlobReference(PathResolver.GetSubpath(path));
                    var policyName    = string.Empty;
                    var policy        = CreateBlobPolicy(newItemValue as string, ref policyName);

                    if (policyName != string.Empty) //policy-based SAStoken
                    {
                        var token = blob.GetSharedAccessSignature(policy, policyName);
                        this.RootProvider.WriteItemObject(blob.StorageUri.PrimaryUri.ToString() + token, path, false);
                    }
                    else
                    {
                        var token = blob.GetSharedAccessSignature(policy);
                        this.RootProvider.WriteItemObject(blob.StorageUri.PrimaryUri.ToString() + token, path, false);
                    }
                }
            }
            else if (string.Equals(type, "Policy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var containerName     = parts[0];
                    var container         = this.Client.GetContainerReference(containerName);
                    var policyName        = parts.Last();
                    var policyPlaceHolder = string.Empty;
                    var policy            = CreateBlobPolicy(newItemValue as string, ref policyPlaceHolder);

                    var permissions = container.GetPermissions();
                    if (permissions.SharedAccessPolicies.ContainsKey(policyName))
                    {
                        if (!this.RootProvider.ShouldContinue(string.Format("Should continue to update existing policy {0}?", policyName), "Policy existed"))
                        {
                            this.RootProvider.WriteWarning("Cancelled");
                            return;
                        }
                        else
                        {
                            permissions.SharedAccessPolicies[policyName] = policy;
                        }
                    }
                    else
                    {
                        permissions.SharedAccessPolicies.Add(policyName, policy);
                    }

                    this.RootProvider.WriteWarning(string.Format("Policy {0} updated or added.", policyName));
                }

                return;
            }
            else if (string.Equals(type, "BlockBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count == 1)
                {
                    this.CreateContainerIfNotExists(parts[0]);
                }
                else
                {
                    this.CreateBlockBlob(path, newItemValue.ToString());
                }
            }
            else if (string.Equals(type, "AppendBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count == 1)
                {
                    this.CreateContainerIfNotExists(parts[0]);
                }
                else
                {
                    this.CreateAppendBlob(path, newItemValue.ToString());
                }
            }
            else if (string.Equals(type, "Permission", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    switch (newItemValue.ToString().ToLowerInvariant())
                    {
                    case "publiccontainer":
                        this.SetContainerPermission(containerName: parts[0], toBePublic: true);
                        this.RootProvider.WriteWarning(string.Format("Done setting container {0} to be public", parts[0]));
                        break;

                    case "privatecontainer":
                        this.SetContainerPermission(containerName: parts[0], toBePublic: false);
                        this.RootProvider.WriteWarning(string.Format("Done setting container {0} to be private", parts[0]));
                        break;

                    default:
                        this.RootProvider.WriteWarning("Invalid value. Supported values: PublicContainer, PrivateContainer");
                        break;
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container.");
                }
            }
            else if (string.Equals(type, "AsyncCopy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    if (newItemValue != null && newItemValue.ToString().Length > 0)
                    {
                        this.AsyncCopy(path, newItemValue.ToString());
                        this.RootProvider.WriteWarning("Started Async copy");
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Must specify the source url in -value.");
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "CopyStatus", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    this.ShowCopyStatus(path);
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "CancelCopy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    if (newItemValue != null && newItemValue.ToString().Length > 0)
                    {
                        this.CancelCopy(path, newItemValue.ToString());
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Must specify the copy ID in -value.");
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "Etag", StringComparison.InvariantCultureIgnoreCase))
            {
                this.ShowEtag(path);
            }
            else
            {
                this.RootProvider.WriteWarning("No operation type is specified by <-type>.");
                this.RootProvider.WriteWarning("Supported operation type: ");
                this.RootProvider.WriteWarning("\tDirectory:            Create directory <-path>");
                this.RootProvider.WriteWarning("\tPageBlob:             Create page blob <-path> with size <-value>");
                this.RootProvider.WriteWarning("\tRandomPages:          Fill page blob <-path> with size <-value> using random data");
                this.RootProvider.WriteWarning("\tListPages:            List page ranges in page blob <-path>");
                this.RootProvider.WriteWarning("\tBlockBlob:            Create block blob <-path> with contents <-value>");
                this.RootProvider.WriteWarning("\tAppendBlob:           Create append blob <-path> with contents <-value>");
                this.RootProvider.WriteWarning("\tContainerSAStoken:    Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tBlobSAStoken:         Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tPolicy:               Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tPermission:           Supported <-value>: PublicContainer, PrivateContainer");
                this.RootProvider.WriteWarning("\tAsyncCopy:            AsyncCopy blob from url <-value> to <-path>");
                this.RootProvider.WriteWarning("\tCopyStatus:           Show copy status of blob <-path>");
                this.RootProvider.WriteWarning("\tCancelCopy:           Specify the destBlob in <-path> and the copy ID in <-value>.");
                this.RootProvider.WriteWarning("\tEtag:                 Show the Etag of the blob <-path> ");
            }
        }
예제 #23
0
        /// <summary>
        /// It creates a table or a table entity, or update table entities
        /// </summary>
        public override void NewItem(
            string path,
            string type,
            object newItemValue)
        {
            var r = AzureTablePathResolver.ResolvePath(this.Client, path);

            if (r.Parts.Count == 0)
            {
                this.RootProvider.WriteWarning("Nothing to create.");
                return;
            }

            if (type == null)
            {
                type = string.Empty;
            }

            switch (type.ToLowerInvariant())
            {
            case "entity":
            {
                var keys = newItemValue as string;
                if (keys == null || !keys.Contains("#"))
                {
                    this.RootProvider.WriteWarning("It requires value formatted as <PartitionKey>#<RowKey>");
                    return;
                }

                var parts = keys.Split('#');
                var pk    = parts[0];
                var rk    = parts[1];

                var e = new TableEntity(pk, rk);
                var o = TableOperation.Insert(e);
                r.Table.Execute(o);
                break;
            }

            case "insertentity":
                CreateEntity(r, newItemValue, TableOperation.Insert);
                break;

            case "replaceentity":
                CreateEntity(r, newItemValue, TableOperation.Replace);
                break;

            case "mergeentity":
                CreateEntity(r, newItemValue, TableOperation.Merge);
                break;

            case "insertorreplaceentity":
                CreateEntity(r, newItemValue, TableOperation.InsertOrReplace);
                break;

            case "insertormergeentity":
                CreateEntity(r, newItemValue, TableOperation.InsertOrMerge);
                break;

            case "sastoken":
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var tableName  = parts[0];
                    var table      = this.Client.GetTableReference(tableName);
                    var policyName = string.Empty;
                    var policy     = CreateTablePermission(newItemValue as string, ref policyName);

                    if (policyName != string.Empty)         //policy-based SAStoken
                    {
                        var token = table.GetSharedAccessSignature(policy, policyName);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                    else
                    {
                        var token = table.GetSharedAccessSignature(policy);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                }
            }

            break;

            case "policy":
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var tableName         = parts[0];
                    var table             = this.Client.GetTableReference(tableName);
                    var policyName        = parts.Last();
                    var policyPlaceHolder = string.Empty;
                    var policy            = CreateTablePermission(newItemValue as string, ref policyPlaceHolder);

                    var permissions = table.GetPermissions();
                    if (permissions.SharedAccessPolicies.ContainsKey(policyName))
                    {
                        if (!this.RootProvider.ShouldContinue(string.Format("Should continue to update existing policy {0}?", policyName), "Policy existed"))
                        {
                            this.RootProvider.WriteWarning("Cancelled");
                            return;
                        }
                        else
                        {
                            permissions.SharedAccessPolicies[policyName] = policy;
                        }
                    }
                    else
                    {
                        permissions.SharedAccessPolicies.Add(policyName, policy);
                    }

                    table.SetPermissions(permissions);

                    this.RootProvider.WriteWarning(string.Format("Policy {0} updated or added.", policyName));
                }
            }
            break;

            default:
                if (r.Parts.Count == 1)     //create a table
                {
                    r.Table.Create();
                    this.RootProvider.WriteWarning("Table is created.");
                }
                else
                {
                    UpdateEntities(path, type, newItemValue);
                }
                break;
            }
        }
예제 #24
0
        private void UpdateEntities(string path, string type, object newItemValue)
        {
            //update existing entities
            this.RootProvider.WriteWarning("update existing entities");
            var propertyName = Path.GetFileName(path);
            var entityPath   = PathResolver.GetParentPath(path);
            var r            = AzureTablePathResolver.ResolvePath(this.Client, entityPath); //override path
            var isNull       = newItemValue == null;

            var entities = this.ListEntities(r.Table, r.Query);

            foreach (DynamicTableEntity e in entities)
            {
                if (isNull)
                {
                    //removing the property and save
                    if (e.Properties.ContainsKey(propertyName))
                    {
                        e.Properties.Remove(propertyName);
                        var op = TableOperation.InsertOrReplace(e);
                        r.Table.Execute(op);
                        this.RootProvider.WriteWarning(string.Format("Property {2} is removed from entity {0} # {1}.", e.PartitionKey, e.RowKey, propertyName));
                    }
                    else
                    {
                        this.RootProvider.WriteWarning(string.Format("Skipping entity {0} # {1} is .", e.PartitionKey, e.RowKey));
                    }

                    continue;
                }

                //Type choice:
                //0. if specified explicitly in type
                //1. if there is existing property
                //2. default to string

                var targetType = EdmType.String;

                if (!string.IsNullOrEmpty(type))
                {
                    if (!Enum.TryParse <EdmType>(type, true, out targetType))
                    {
                        throw new Exception("Failed to parse type " + type + ", valid values are:" + string.Join(",", Enum.GetNames(typeof(EdmType))));
                    }
                }
                else if (e.Properties.ContainsKey(propertyName))
                {
                    targetType = e.Properties[propertyName].PropertyType;
                }

                //Check property existence:
                var existing = e.Properties.ContainsKey(propertyName);

                //set value
                switch (targetType)
                {
                case EdmType.String:
                    if (existing)
                    {
                        e.Properties[propertyName].StringValue = newItemValue.ToString();
                    }
                    else
                    {
                        var value = new EntityProperty(newItemValue.ToString());
                        e.Properties.Add(propertyName, value);
                    }
                    break;

                case EdmType.Binary:
                    var bytes = newItemValue as byte[];
                    if (existing)
                    {
                        if (bytes != null)
                        {
                            e.Properties[propertyName].BinaryValue = bytes;
                        }
                        else
                        {
                            e.Properties[propertyName].BinaryValue = Utils.FromBase64(newItemValue.ToString());
                        }
                    }
                    else
                    {
                        if (bytes != null)
                        {
                            var value = new EntityProperty(bytes);
                            e.Properties.Add(propertyName, value);
                        }
                        else
                        {
                            var value = new EntityProperty(newItemValue.ToString());
                            e.Properties.Add(propertyName, value);
                        }
                    }
                    break;

                case EdmType.Boolean:
                    var boolValue = true as bool?;


                    // Check target value:
                    // 1. the input value is a bool
                    // 2. the input value is a string, can be parsed to be a bool
                    if (newItemValue is bool)
                    {
                        boolValue = (bool)newItemValue;
                    }
                    else if (newItemValue is string)
                    {
                        var boolString = newItemValue as string;
                        var tempValue  = true;
                        if (Boolean.TryParse(boolString, out tempValue))
                        {
                            boolValue = tempValue;
                        }
                        else
                        {
                            throw new Exception("Failed to parse boolean value " + boolString);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].BooleanValue = boolValue;
                    }
                    else
                    {
                        var value = new EntityProperty(boolValue);
                        e.Properties.Add(propertyName, value);
                    }

                    break;

                case EdmType.DateTime:
                    var dateTime = null as DateTime?;

                    // Check target value:
                    // 1. the input value is a DateTime
                    // 2. the input value is a string, can be parsed to be a DateTime
                    if (newItemValue is DateTime)
                    {
                        dateTime = (DateTime)newItemValue;
                    }
                    else if (newItemValue is string)
                    {
                        var dateTimeString = newItemValue as string;
                        var tempDateTime   = DateTime.Now;
                        if (DateTime.TryParse(dateTimeString, out tempDateTime))
                        {
                            dateTime = tempDateTime;
                        }
                        else
                        {
                            throw new Exception("Failed to parse DateTime value " + dateTimeString);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].DateTime = dateTime;
                    }
                    else
                    {
                        var value = new EntityProperty(dateTime);
                        e.Properties.Add(propertyName, value);
                    }

                    break;

                case EdmType.Double:
                    var doubleValue = null as double?;

                    if (newItemValue is double)
                    {
                        doubleValue = (double)newItemValue;
                    }
                    else if (newItemValue is string)
                    {
                        var doubleString = newItemValue as string;
                        var tempValue    = 0.0;
                        if (Double.TryParse(doubleString, out tempValue))
                        {
                            doubleValue = tempValue;
                        }
                        else
                        {
                            throw new Exception("Failed to parse double value " + doubleString);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].DoubleValue = doubleValue;
                    }
                    else
                    {
                        var value = new EntityProperty(doubleValue);
                        e.Properties.Add(propertyName, value);
                    }

                    break;

                case EdmType.Guid:
                    var guidValue = null as Guid?;

                    if (newItemValue is Guid)
                    {
                        guidValue = (Guid)newItemValue;
                    }
                    else
                    {
                        var guidString = newItemValue as string;
                        var tempValue  = Guid.NewGuid();
                        if (Guid.TryParse(guidString, out tempValue))
                        {
                            guidValue = tempValue;
                        }
                        else
                        {
                            throw new Exception("Failed to parse Guid value " + guidString);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].GuidValue = guidValue;
                    }
                    else
                    {
                        var value = new EntityProperty(guidValue);
                        e.Properties.Add(propertyName, value);
                    }
                    break;

                case EdmType.Int32:
                    var int32Value = null as int?;

                    if (newItemValue is int)
                    {
                        int32Value = (int)newItemValue;
                    }
                    else
                    {
                        var int32String = newItemValue as string;
                        var tempValue   = 0;
                        if (int.TryParse(int32String, out tempValue))
                        {
                            int32Value = tempValue;
                        }
                        else
                        {
                            throw new Exception("Failed to parse int32 value " + int32String);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].Int32Value = int32Value;
                    }
                    else
                    {
                        var value = new EntityProperty(int32Value);
                        e.Properties.Add(propertyName, value);
                    }
                    break;

                case EdmType.Int64:
                    var int64Value = null as Int64?;

                    if (newItemValue is Int64)
                    {
                        int64Value = (Int64)newItemValue;
                    }
                    else
                    {
                        var int64String = newItemValue as string;
                        var tempValue   = (Int64)0;
                        if (Int64.TryParse(int64String, out tempValue))
                        {
                            int64Value = tempValue;
                        }
                        else
                        {
                            throw new Exception("Failed to parse int64 value " + int64String);
                        }
                    }

                    //set value
                    if (existing)
                    {
                        e.Properties[propertyName].Int64Value = int64Value;
                    }
                    else
                    {
                        var value = new EntityProperty(int64Value);
                        e.Properties.Add(propertyName, value);
                    }
                    break;
                } //end of switch

                var o = TableOperation.Merge(e);
                r.Table.Execute(o);
                this.RootProvider.WriteWarning(string.Format("Entity {0} # {1} is merged.", e.PartitionKey, e.RowKey));
            }
        }
예제 #25
0
        private void ListPageRanges(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetPageBlobReference(PathResolver.GetSubpath(path));
                if (!blob.Exists())
                {
                    this.RootProvider.WriteWarning("PageBlob " + path + " does not exist.");
                    return;
                }

                blob.FetchAttributes();
                var totalLength = blob.Properties.Length;

                var count  = 0L;
                var offset = 0L;
                var length = 4 * 1024 * 1024L; //4MB
                while (true)
                {
                    PageRange page  = null;
                    var       round = 0L;

                    length = (offset + length > totalLength) ? (totalLength - offset) : length;
                    foreach (var r in blob.GetPageRanges(offset, length))
                    {
                        page = r;
                        round++;
                        this.RootProvider.WriteWarning(string.Format("[{3}]\t[{0} - {1}] {2}", r.StartOffset, r.EndOffset, r.EndOffset - r.StartOffset + 1, count++));
                    }

                    if (offset + length >= totalLength)
                    {
                        //reach the end
                        break;
                    }

                    //1. move offset
                    offset += length;

                    //2. calculate next length
                    if (round < 200)
                    {
                        length *= 2;
                    }
                    else if (round > 500)
                    {
                        length /= 2;
                    }
                }
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the page blob path.");
            }
        }