예제 #1
0
        //
        // Execute GetBlob
        //

        public string Execute(string blobURL, string filePath, string identity = null, bool ifNewer = false, bool deleteAfterCopy = false)
        {
            // method start

            // Connection
            var Cred       = new ManagedIdentityCredential(identity);
            var blobClient = new BlobClient(new Uri(blobURL), Cred);

            if (ifNewer && File.Exists(filePath) && !IsNewer(blobClient, filePath))
            {
                return("Skipped. Blob is not newer than file.");
            }

            try
            {
                string absolutePath = Path.GetFullPath(filePath);
                string dirName      = Path.GetDirectoryName(absolutePath);
                Directory.CreateDirectory(dirName);

                blobClient.DownloadTo(filePath);

                if (deleteAfterCopy)
                {
                    blobClient.Delete();
                }
                return("Success");
            } catch (Azure.RequestFailedException)
            {
                throw;
            } catch (Exception ex)
            {
                throw AzmiException.IDCheck(identity, ex);
            }
        }
예제 #2
0
        public void deleteFile(string container, string fileName)
        {
            BlobServiceClient   blobServiceClient = new BlobServiceClient(AZURE_STORAGE_CONNECTION_STRING);
            BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient(container);
            BlobClient          blobClient        = containerClient.GetBlobClient(fileName);

            blobClient.Delete();
        }
        public static void Delete(string uriString, string blobContainerName)
        {
            string     uriFullString = GetFullUriString(uriString, blobContainerName);
            Uri        uri           = new Uri(uriFullString + SasTokenDict[blobContainerName]);
            BlobClient client        = new BlobClient(uri);

            client.Delete();
        }
예제 #4
0
        public static void CleanupBlobFile(WebApplicationFactory <Startup> factory,
                                           Guid documentVersionId,
                                           Guid fileContentId)
        {
            CleanupDocumentVersionAndFileContent(factory, documentVersionId, fileContentId);

            BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(BLOB_CONTAINER_NAME);
            BlobClient          blobClient      = containerClient.GetBlobClient($"doc-{fileContentId}");

            blobClient.Delete();
        }
예제 #5
0
        private static void DeletePdfBlob(object blobName)
        {
            BlobClient blobClient = pdfBlobContainer.GetBlobClient(blobName.ToString());

            Thread.Sleep(1000 * 90);

            if (blobClient.Exists())
            {
                blobClient.Delete();
            }
        }
예제 #6
0
        public void Delete(string blobName)
        {
            Debug.WriteLine("Deleting blob:\n\t {0}\n", blobName);
            BlobClient blobClient = GetBlobClient(blobName);

            if (blobClient.Exists().Value)
            {
                blobClient.Delete();
            }
            else
            {
                throw new InvalidOperationException("Blob not found");
            }
        }
예제 #7
0
        private void ProcessMulti()
        {
            WriteVerbose($"Starting to process a container {Container}");
            // Connection
            var cred            = new ManagedIdentityCredential(Identity);
            var containerClient = new BlobContainerClient(new Uri(Container), cred);

            // get list of blobs
            WriteVerbose("Obtaining list of blobs...");
            List <string> blobListing = containerClient.GetBlobs(prefix: Prefix).Select(i => i.Name).ToList();

            WriteVerbose($"Obtained {blobListing.Count} blobs");

            // apply -Exclude regular expression
            if (!String.IsNullOrEmpty(Exclude))
            {
                WriteVerbose("Filtering list of blobs...");
                Regex excludeRegEx = new Regex(Exclude);
                blobListing = blobListing.Where(blob => !excludeRegEx.IsMatch(blob)).ToList();
                WriteVerbose($"Filtered to {blobListing.Count} blobs");
            }

            // fix path
            Directory ??= Container.Split('/').Last();
            Directory = Path.GetFullPath(Directory, SessionState.Path.CurrentLocation.Path);
            WriteVerbose($"Using destination: '{Directory}'");
            System.IO.Directory.CreateDirectory(Directory);

            //Task.WhenAll(blobListing.Select(async blob => {
            //    BlobClient blobClient = containerClient.GetBlobClient(blob);
            //    string filePath = Path.Combine(directory, blob);
            //    await blobClient.DownloadToAsync(filePath);
            //}));

            Parallel.ForEach(blobListing, blobItem =>
            {
                BlobClient blobClient = containerClient.GetBlobClient(blobItem);
                string filePath       = Path.Combine(Directory, blobItem);
                string absolutePath   = Path.GetFullPath(filePath);
                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath)); // required for missing sub-directories
                blobClient.DownloadTo(filePath);
                if (DeleteAfterCopy)
                {
                    blobClient.Delete();
                }
            });
            WriteVerbose("Download completed");
        }
예제 #8
0
        // Download the blob to a local file
        public string getBlob(string blobURL, string filePath, string identity = null, bool ifNewer = false, bool deleteAfterCopy = false)
        {
            // Connection
            var Cred       = new ManagedIdentityCredential(identity);
            var blobClient = new BlobClient(new Uri(blobURL), Cred);

            if (ifNewer && File.Exists(filePath))
            {
                var blobProperties = blobClient.GetProperties();
                // Any operation that modifies a blob, including an update of the blob's metadata or properties, changes the last modified time of the blob
                var blobLastModified = blobProperties.Value.LastModified.UtcDateTime;

                // returns date of local file was last written to
                DateTime fileLastWrite = File.GetLastWriteTimeUtc(filePath);

                int value = DateTime.Compare(blobLastModified, fileLastWrite);
                if (value < 0)
                {
                    return("Skipped. Blob is not newer than file.");
                }
            }

            try
            {
                string absolutePath = Path.GetFullPath(filePath);
                string dirName      = Path.GetDirectoryName(absolutePath);
                Directory.CreateDirectory(dirName);

                blobClient.DownloadTo(filePath);

                if (deleteAfterCopy)
                {
                    blobClient.Delete();
                }
                return("Success");
            }
            catch (Azure.RequestFailedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw IdentityError(identity, ex);
            }
        }
예제 #9
0
        private void ProcessSingle()
        {
            WriteVerbose("Starting to process a single blob");
            // Connection
            var cred       = new ManagedIdentityCredential(Identity);
            var blobClient = new BlobClient(new Uri(Blob), cred);

            // Fix path
            File ??= Blob.Split('/').Last();
            File = Path.GetFullPath(File, SessionState.Path.CurrentLocation.Path);
            WriteVerbose($"Using destination: '{File}'");
            // Download
            blobClient.DownloadTo(File);
            if (DeleteAfterCopy)
            {
                blobClient.Delete();
            }
            WriteVerbose("Download completed");
        }
        /// <summary>
        /// Deletes a file from the specified container
        /// </summary>
        /// <param name="fileName">Name of the file to delete</param>
        /// <param name="container">Name of the container to delete the file from</param>
        public override void DeleteFile(string fileName, string container)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("fileName");
            }

            if (String.IsNullOrEmpty(container))
            {
                throw new ArgumentException("container");
            }
            container = container.Replace('\\', '/');

            if (!initialized)
            {
                Initialize();
            }

            try
            {
                var containerTuple = ParseContainer(container);

                container = containerTuple.Item1;
                fileName  = string.Concat(containerTuple.Item2, fileName);

                BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(container);
                BlobClient          blob = blobContainerClient.GetBlobClient(fileName);

                if (blob.Exists())
                {
                    blob.Delete();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #11
0
        /// <summary>
        /// Tests a blob SAS to determine which operations it allows.
        /// </summary>
        /// <param name="sasUri">A string containing a URI with a SAS appended.</param>
        /// <param name="blobContent">A string content content to write to the blob.</param>
        static void TestBlobSAS(Uri sasUri, string blobContent)
        {
            //Try performing blob operations using the SAS provided.

            //Return a reference to the blob using the SAS URI.
            BlobClient blob = new BlobClient(sasUri);

            //Create operation: Upload a blob with the specified name to the container.
            //If the blob does not exist, it will be created. If it does exist, it will be overwritten.
            try
            {
                //string blobContent = "This blob was created with a shared access signature granting write permissions to the blob. ";
                blob.Upload(BinaryData.FromString(blobContent));
                Console.WriteLine("Create operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Create operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }

            // Write operation: Add metadata to the blob
            try
            {
                IDictionary <string, string> metadata = new Dictionary <string, string>();
                metadata.Add("name", "value");
                blob.SetMetadata(metadata);
                Console.WriteLine("Write operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Write operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }

            //Read operation: Read the contents of the blob.
            try
            {
                BlobDownloadResult download = blob.DownloadContent();
                string             content  = download.Content.ToString();
                Console.WriteLine(content);
                Console.WriteLine();

                Console.WriteLine("Read operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Read operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }

            //Delete operation: Delete the blob.
            try
            {
                blob.Delete();
                Console.WriteLine("Delete operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Delete operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }
        }
예제 #12
0
        /// <summary>
        /// Tests a container SAS to determine which operations it allows.
        /// </summary>
        /// <param name="sasUri">A string containing a URI with a SAS appended.</param>
        /// <param name="blobName">A string containing the name of the blob.</param>
        /// <param name="blobContent">A string content content to write to the blob.</param>
        static void TestContainerSAS(Uri sasUri, string blobName, string blobContent)
        {
            //Try performing container operations with the SAS provided.
            //Note that the storage account credentials are not required here; the SAS provides the necessary
            //authentication information on the URI.

            //Return a reference to the container using the SAS URI.
            BlobContainerClient container = new BlobContainerClient(sasUri);

            //Return a reference to a blob to be created in the container.
            BlobClient blob = container.GetBlobClient(blobName);

            //Write operation: Upload a new blob to the container.
            try
            {
                blob.Upload(BinaryData.FromString(blobContent));
                Console.WriteLine("Write operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Write operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }

            //List operation: List the blobs in the container.
            try
            {
                foreach (BlobItem blobItem in container.GetBlobs())
                {
                    Console.WriteLine(blobItem.Name);
                }
                Console.WriteLine("List operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("List operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }

            //Read operation: Read the contents of the blob we created above.
            try
            {
                BlobDownloadInfo download = blob.Download();
                Console.WriteLine(download.ContentLength);
                Console.WriteLine();
                Console.WriteLine("Read operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Read operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }
            Console.WriteLine();

            //Delete operation: Delete the blob we created above.
            try
            {
                blob.Delete();
                Console.WriteLine("Delete operation succeeded for SAS " + sasUri);
                Console.WriteLine();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("Delete operation failed for SAS " + sasUri);
                Console.WriteLine("Additional error information: " + e.Message);
                Console.WriteLine();
            }
        }
예제 #13
0
        //
        // GetBlobs main method
        //

        public List <string> Execute(string containerUri, string directory, string identity = null, string prefix = null, string[] exclude = null, bool ifNewer = false, bool deleteAfterCopy = false)
        {
            // authentication
            string containerUriTrimmed = containerUri.TrimEnd(blobPathDelimiter);
            var    cred            = new ManagedIdentityCredential(identity);
            var    containerClient = new BlobContainerClient(new Uri(containerUriTrimmed), cred);

            // get list of blobs
            List <string> blobListing = containerClient.GetBlobs(prefix: prefix).Select(i => i.Name).ToList();

            // apply --exclude regular expression
            if (exclude != null)
            {
                var rx = new Regex(String.Join('|', exclude));
                blobListing = blobListing.Where(b => !rx.IsMatch(b)).ToList();
            }

            // create root folder for blobs
            Directory.CreateDirectory(directory);

            // download blobs
            var results = new List <string>();

            Parallel.ForEach(blobListing, blobItem =>
            {
                BlobClient blobClient = containerClient.GetBlobClient(blobItem);

                string filePath = Path.Combine(directory, blobItem);
                if (ifNewer && File.Exists(filePath) && !IsNewer(blobClient, filePath))
                {
                    lock (results)
                    {
                        results.Add($"Skipped. Blob '{blobClient.Uri}' is not newer than file.");
                    }
                }

                string absolutePath = Path.GetFullPath(filePath);
                string dirName      = Path.GetDirectoryName(absolutePath);
                Directory.CreateDirectory(dirName);

                try
                {
                    blobClient.DownloadTo(filePath);

                    lock (results)
                    {
                        results.Add($"Success '{blobClient.Uri}'");
                    }

                    if (deleteAfterCopy)
                    {
                        blobClient.Delete();
                    }
                }
                catch (Azure.RequestFailedException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw AzmiException.IDCheck(identity, ex);
                }
            });
            return(results);
        }
예제 #14
0
        public void DeletePhoto(string name)
        {
            BlobClient blobClient = _blobContainerClient.GetBlobClient(name);

            blobClient.Delete();
        }
        private static async Task SoftDeleteTest()
        {
            // Retrieve a CloudBlobClient object and enable soft delete
            BlobServiceClient blobClient = GetCloudBlobClient();

            try
            {
                BlobServiceProperties serviceProperties = blobClient.GetProperties();
                serviceProperties.DeleteRetentionPolicy.Enabled = true;
                serviceProperties.DeleteRetentionPolicy.Days    = RetentionDays;
                blobClient.SetProperties(serviceProperties);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Error returned from the service: {0}", ex.Message);
                throw;
            }

            // Create a container
            BlobContainerClient container = blobClient.GetBlobContainerClient("softdelete-" + System.Guid.NewGuid().ToString());

            try
            {
                await container.CreateIfNotExistsAsync();
            }
            catch (RequestFailedException)
            {
                Console.WriteLine("If you are using the storage emulator, please make sure you have started it. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            try
            {
                // Upload
                Console.WriteLine("\nUpload:");
                BlobClient blockBlob = container.GetBlobClient("HelloWorld");
                await blockBlob.UploadAsync(ImageToUpload, overwrite : true);

                PrintBlobsInContainer(container, BlobTraits.All);

                // Overwrite
                Console.WriteLine("\nOverwrite:");
                await blockBlob.UploadAsync(TextToUpload, overwrite : true);

                PrintBlobsInContainer(container, BlobTraits.All);

                // Snapshot
                Console.WriteLine("\nSnapshot:");
                await blockBlob.CreateSnapshotAsync();

                PrintBlobsInContainer(container, BlobTraits.All);

                // Delete (including snapshots)
                Console.WriteLine("\nDelete (including snapshots):");
                blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
                PrintBlobsInContainer(container, BlobTraits.All);

                // Undelete
                Console.WriteLine("\nUndelete:");
                await blockBlob.UndeleteAsync();

                PrintBlobsInContainer(container, BlobTraits.All);

                // Recover
                Console.WriteLine("\nCopy the most recent snapshot over the base blob:");
                blockBlob.StartCopyFromUri(blockBlob.Uri);
                PrintBlobsInContainer(container, BlobTraits.All);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Error returned from the service: {0}", ex.Message);
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("\nDone.\n\nEnter 'd' to cleanup resources. Doing so will also turn off the soft delete feature. Enter any other key to leave the container intact.");
            String cleanup = Console.ReadLine();

            if (cleanup == "d")
            {
                try
                {
                    // Delete the container
                    await container.DeleteIfExistsAsync();

                    Console.WriteLine("\nContainer deleted.");

                    // Turn off soft delete
                    BlobServiceProperties serviceProperties = blobClient.GetProperties();
                    serviceProperties.DeleteRetentionPolicy.Enabled = false;
                    blobClient.SetProperties(serviceProperties);
                }
                catch (RequestFailedException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                    throw;
                }
            }
        }