Example #1
0
        /// <summary>
        /// execute command
        /// </summary>
        public override void ExecuteCmdlet()
        {
            IStorageBlobManagement localChannel = Channel;

            bool foundAFolder = false;
            DataLakeFileClient      srcBlob    = null;
            DataLakeDirectoryClient srcBlobDir = null;

            if (ParameterSetName == ManualParameterSet)
            {
                DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem);
                foundAFolder = GetExistDataLakeGen2Item(fileSystem, this.Path, out srcBlob, out srcBlobDir);
            }
            else //BlobParameterSet
            {
                if (!InputObject.IsDirectory)
                {
                    srcBlob = InputObject.File;
                }
                else
                {
                    srcBlobDir   = InputObject.Directory;
                    foundAFolder = true;
                }
            }

            if (foundAFolder)
            {
                if (ShouldProcess(GetDataLakeItemUriWithoutSas(srcBlobDir), "Move Directory: "))
                {
                    DataLakeFileSystemClient destFileSystem = GetFileSystemClientByName(localChannel, this.DestFileSystem != null ? this.DestFileSystem : this.FileSystem);
                    DataLakeDirectoryClient  destBlobDir    = destFileSystem.GetDirectoryClient(this.DestPath);

                    if (this.Force || !destBlobDir.Exists() || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destBlobDir)), ""))
                    {
                        destBlobDir = srcBlobDir.Rename(this.DestPath, this.DestFileSystem).Value;
                        WriteDataLakeGen2Item(localChannel, destBlobDir);
                    }
                }
            }
            else
            {
                if (ShouldProcess(GetDataLakeItemUriWithoutSas(srcBlob), "Move File: "))
                {
                    DataLakeFileSystemClient destFileSystem = GetFileSystemClientByName(localChannel, this.DestFileSystem != null ? this.DestFileSystem : this.FileSystem);
                    DataLakeFileClient       destFile       = destFileSystem.GetFileClient(this.DestPath);

                    if (this.Force || !destFile.Exists() || ShouldContinue(string.Format("Overwrite destination {0}", GetDataLakeItemUriWithoutSas(destFile)), ""))
                    {
                        destFile = srcBlob.Rename(this.DestPath, this.DestFileSystem).Value;
                        WriteDataLakeGen2Item(localChannel, destFile);
                    }
                }
            }
        }
        /// <summary>
        /// execute command
        /// </summary>
        public override void ExecuteCmdlet()
        {
            if (AsJob.IsPresent)
            {
                DoBeginProcessing();
            }

            IStorageBlobManagement localChannel = Channel;

            fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem);

            if (this.Directory.IsPresent)
            {
                DataLakeDirectoryClient dirClient = fileSystem.GetDirectoryClient(this.Path);
                if (ShouldProcess(GetDataLakeItemUriWithoutSas(dirClient), "Create Directory: "))
                {
                    if (dirClient.Exists())
                    {
                        throw new ResourceAlreadyExistException(String.Format("Folder '{0}' already exists.", GetDataLakeItemUriWithoutSas(dirClient)));
                    }
                    DataLakeModels.PathPermissions pathPermissions = null;
                    if (this.Permission != null)
                    {
                        pathPermissions = DataLakeModels.PathPermissions.ParseSymbolicPermissions(this.Permission);
                    }

                    // Set BlobDir Properties and MetaData
                    PathHttpHeaders pathHttpHeaders       = SetDatalakegen2ItemProperties(dirClient, BlobProperties, setToServer: false);
                    IDictionary <string, string> metadata = SetDatalakegen2ItemMetaData(dirClient, BlobMetadata, setToServer: false);

                    dirClient.Create(pathHttpHeaders,
                                     metadata,
                                     this.Permission,
                                     this.Umask != null ? DataLakeModels.PathPermissions.ParseSymbolicPermissions(this.Umask).ToOctalPermissions() : null);

                    WriteDataLakeGen2Item(localChannel, dirClient);
                }
            }
            else //create File
            {
                DataLakeFileClient fileClient = fileSystem.GetFileClient(this.Path);
                if (ShouldProcess(GetDataLakeItemUriWithoutSas(fileClient), "Create File: "))
                {
                    // Use SDK to upload directly when use SAS credential, and need set permission, since set permission after upload will fail with SAS
                    if (Channel.StorageContext.StorageAccount.Credentials.IsSAS &&
                        (!string.IsNullOrEmpty(this.Permission) || !string.IsNullOrEmpty(this.Umask)))
                    {
                        Func <long, Task> taskGenerator = (taskId) => UploadDataLakeFile(taskId, fileClient, ResolvedFileName);
                        RunTask(taskGenerator);
                    }
                    else
                    {
                        CloudBlobContainer container = Channel.GetContainerReference(this.FileSystem);
                        CloudBlockBlob     blob      = container.GetBlockBlobReference(this.Path);

                        Func <long, Task> taskGenerator = (taskId) => Upload2Blob(taskId, Channel, ResolvedFileName, blob);
                        RunTask(taskGenerator);
                    }
                }
            }


            if (AsJob.IsPresent)
            {
                DoEndProcessing();
            }
        }
Example #3
0
        public bool Exists(string path)
        {
            DataLakeDirectoryClient directoryClient = GetDirectoryClient(path);

            return(directoryClient.Exists());
        }
        public async Task UploadFile(string fullPath)
        {
            var dataLakeServiceClient    = GetDataLakeServiceClient();
            var dataLakeFileSystemClient = dataLakeServiceClient.GetFileSystemClient(ContainerName);

            var directoryDate = dataLakeFileSystemClient.GetDirectoryClient(DateTime.Now.ToString("yyyy-MM-dd"));

            #region Create and Check Folder
            DataLakeDirectoryClient directoryDistributorCode = null;

            if (!directoryDate.Exists())
            {
                dataLakeFileSystemClient.CreateDirectory(DateTime.Now.ToString("yyyy-MM-dd"));
                directoryDate = dataLakeFileSystemClient.GetDirectoryClient(DateTime.Now.ToString("yyyy-MM-dd"));
            }

            if (FormFileUpload.Category.ToUpper() == "STOCK")
            {
                var directoryStock = directoryDate.GetSubDirectoryClient("Stock");

                if (!directoryStock.Exists())
                {
                    directoryDate.GetSubDirectoryClient("Stock");
                    directoryStock = directoryDate.GetSubDirectoryClient("Stock");
                }

                directoryDistributorCode = directoryStock.GetSubDirectoryClient(FormFileUpload.DistributorCode);

                if (!directoryDistributorCode.Exists())
                {
                    directoryStock.GetSubDirectoryClient(FormFileUpload.DistributorCode);
                    directoryDistributorCode = directoryStock.GetSubDirectoryClient(FormFileUpload.DistributorCode);
                }
            }

            if (FormFileUpload.Category.ToUpper() == "SALE")
            {
                var directorySales = directoryDate.GetSubDirectoryClient("Sales");

                if (!directorySales.Exists())
                {
                    directoryDate.GetSubDirectoryClient("Sales");
                    directorySales = directoryDate.GetSubDirectoryClient("Sales");
                }

                directoryDistributorCode = directorySales.GetSubDirectoryClient(FormFileUpload.DistributorCode);

                if (!directoryDistributorCode.Exists())
                {
                    directorySales.GetSubDirectoryClient(FormFileUpload.DistributorCode);
                    directoryDistributorCode = directorySales.GetSubDirectoryClient(FormFileUpload.DistributorCode);
                }
            }
            #endregion

            string fileName = Path.GetFileName(fullPath);

            DataLakeFileClient fileClient = await directoryDistributorCode.CreateFileAsync(fileName);

            using var fileStream = System.IO.File.OpenRead(fullPath);
            long fileSize = fileStream.Length;
            await fileClient.AppendAsync(fileStream, offset : 0);

            await fileClient.FlushAsync(position : fileSize);
        }