Esempio n. 1
0
        /// <summary>
        /// Take one snapshot only if there is not any snapshot for the specific blob
        /// This will prevent the blob to be deleted by a not intended delete action
        /// </summary>
        /// <param name="blob"></param>
        /// <returns></returns>
        private async Task <bool> TryTakeBlobSnapshotAsync(CloudBlockBlob blob)
        {
            if (blob == null)
            {
                //no action
                return(false);
            }

            var stopwatch = Stopwatch.StartNew();

            try
            {
                var allSnapshots = blob.Container.
                                   ListBlobs(prefix: blob.Name,
                                             useFlatBlobListing: true,
                                             blobListingDetails: BlobListingDetails.Snapshots);
                //the above call will return at least one blob the original
                if (allSnapshots.Count() == 1)
                {
                    var snapshot = await blob.CreateSnapshotAsync();

                    stopwatch.Stop();
                    Trace.WriteLine($"SnapshotCreated:milliseconds={stopwatch.ElapsedMilliseconds}:{blob.Uri.ToString()}:{snapshot.SnapshotQualifiedUri}");
                }
                return(true);
            }
            catch (StorageException storageException)
            {
                stopwatch.Stop();
                Trace.WriteLine($"EXCEPTION:milliseconds={stopwatch.ElapsedMilliseconds}:CreateSnapshot: Failed to take the snapshot for blob {blob.Uri.ToString()}. Exception{storageException.ToString()}");
                return(false);
            }
        }
        public static async Task UpdateConfigFileAsync(Policy policy, string fileId = null)
        {
            InitCloudBlobContainer();

            string policyServerConfigFileName = fileId == null ? $"policyServerConfig.json" : $"policyServerConfig_{fileId}.json";

            CloudBlockBlob cloudBlockBlob = CloudBlobContainer.GetBlockBlobReference(policyServerConfigFileName);

            cloudBlockBlob.Properties.ContentType = "application/json";

            CloudBlockBlob cloudBlockBlobSnapshot = await cloudBlockBlob.CreateSnapshotAsync();

            try
            {
                await cloudBlockBlob.SerializeObjectToBlobAsync(new DeserializePolicyResult { Policy = policy });
            }
            catch (Exception)
            {
                await cloudBlockBlob.DeleteAsync();

                cloudBlockBlob = cloudBlockBlobSnapshot;
            }
            finally
            {
                await cloudBlockBlobSnapshot.DeleteAsync();
            }
        }
        public static async Task <int> SendToAzure(AzureOptions opts)
        {
            CloudBlockBlob zipSnapshot = null;
            CloudBlockBlob zipBlob     = null;

            try
            {
                var zipFile = CreateZipFile(opts.Source, opts);

                Console.WriteLine("Upload zip file.");
                var storageCredentials  = new StorageCredentials(opts.AccountName, opts.AccountKey);
                var cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
                var cloudBlobClient     = cloudStorageAccount.CreateCloudBlobClient();

                var container = cloudBlobClient.GetContainerReference(opts.ContainerName);
                await container.CreateIfNotExistsAsync();

                zipBlob = container.GetBlockBlobReference(opts.Destination + "/software.zip");

                if (await zipBlob.ExistsAsync())
                {
                    zipSnapshot = await zipBlob.CreateSnapshotAsync();
                }
                await zipBlob.UploadFromFileAsync(zipFile);

                Console.WriteLine("Update version.");
                var xmlBlob = container.GetBlockBlobReference(opts.Destination + "/software.xml");

                var doc = CreateOrUpdateVersionFile(
                    (await xmlBlob.ExistsAsync()) ? XDocument.Parse(await xmlBlob.DownloadTextAsync()) : null,
                    zipFile,
                    opts
                    );

                //if (await xmlBlob.ExistsAsync())
                //	await xmlBlob.CreateSnapshotAsync();
                await xmlBlob.UploadTextAsync(doc.ToString());

                await zipSnapshot?.DeleteAsync();

                File.Delete(zipFile);

                return(0);
            }
            catch (Exception ex)
            {
                if (zipBlob != null && zipSnapshot != null)
                {
                    await zipBlob.StartCopyAsync(zipSnapshot);

                    await zipSnapshot.DeleteAsync();
                }
                Console.WriteLine(ex.Message);
                return(1);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Replaces a files content and takes a snapshot if take snapshots is enabled
        /// </summary>
        /// <param name="file"></param>
        /// <param name="postedFile"></param>
        /// <returns></returns>
        public async Task <BlobDto> ReplaceFile(BlobDto file, Stream postedFile)
        {
            CloudBlockBlob blob = await GetBlob(file.Path);

            if (_storageOptions.TakeSnapshots)
            {
                await blob.CreateSnapshotAsync();
            }

            await blob.UploadFromStreamAsync(postedFile);

            await blob.FetchAttributesAsync();

            file.FileSize     = blob.Properties.Length;
            file.DateModified = DateTime.UtcNow;

            return(file);
        }
        /// <summary>
        ///     查询资源。
        /// </summary>
        /// <param name="uri">资源完整路径。</param>
        public async Task <FileInfo> GetResourceAsync(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            string path = uri.AbsolutePath.Remove("/" + _blobContainer.Name + "/");

            CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(path);

            if (!await blob.ExistsAsync())
            {
                return(new NotFoundFileInfo(path));
            }

            CloudBlockBlob ss = await blob.CreateSnapshotAsync();

            Console.WriteLine(ss.SnapshotQualifiedStorageUri);

            string[] paths          = path.Split('/');
            string   fileNameBase64 = paths[paths.Length - 1];

            //string signature = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
            //    SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-2)
            //});

            return(new FileInfo
            {
                Name = fileNameBase64,
                Kind = blob.Metadata.TryGetValue("Kind", out string kind) ? kind : null,
                Exists = true,
                IsDirectory = false,
                LastModified = blob.Properties.LastModified.GetValueOrDefault(DateTimeOffset.UtcNow),
                Length = blob.Properties.Length,
                Path = blob.Uri.PathAndQuery,
                TempPath = null
            });
        /// <summary>
        /// Basic operations to work with block blobs
        /// </summary>
        /// <returns>Task<returns>
        private static async Task BasicStorageBlockBlobOperationsAsync()
        {
            const string imageToUpload          = "HelloWorld.png";
            string       blockBlobContainerName = "demoblockblobcontainer-" + Guid.NewGuid();

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference(blockBlobContainerName);

            try
            {
                await container.CreateIfNotExistsAsync();
            }
            catch (StorageException)
            {
                Console.WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. 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;
            }

            // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
            // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
            // to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
            // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
            // await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            // Upload a BlockBlob to the newly created container
            Console.WriteLine("2. Uploading BlockBlob");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageToUpload);
            await blockBlob.UploadFromFileAsync(imageToUpload, FileMode.Open);

            // List all the blobs in the container
            Console.WriteLine("3. List Blobs in Container");
            foreach (IListBlobItem blob in container.ListBlobs())
            {
                // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                // Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
                Console.WriteLine("- {0} (type: {1})", blob.Uri, blob.GetType());
            }

            // Download a blob to your file system
            Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
            await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", imageToUpload), FileMode.Create);

            // Create a read-only snapshot of the blob
            Console.WriteLine("5. Create a read-only snapshot of the blob");
            CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null);

            // Clean up after the demo
            Console.WriteLine("6. Delete block Blob and all of its snapshots");
            await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);

            Console.WriteLine("7. Delete Container");
            await container.DeleteIfExistsAsync();
        }
Esempio n. 7
0
        /// <summary>
        /// Basic operations to work with block blobs
        /// </summary>
        /// <returns>A Task object.</returns>
        private static async Task BasicStorageBlockBlobOperationsAsync()
        {
            const string ImageToUpload = "Tiger.png";
            string       containerName = ContainerPrefix + Guid.NewGuid();

            // Retrieve storage account information from connection string
            CloudStorageAccount storageAccount = Cfg.GetAccount();

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Create a container for organizing blobs within the storage account.
            Console.WriteLine("1. Creating Container");
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            try
            {
                // The call below will fail if the sample is configured to use the storage emulator in the connection string, but
                // the emulator is not running.
                // Change the retry policy for this call so that if it fails, it fails quickly.
                BlobRequestOptions requestOptions = new BlobRequestOptions()
                {
                    RetryPolicy = new NoRetry()
                };
                await container.CreateIfNotExistsAsync(requestOptions, null);
            }
            catch (StorageException)
            {
                Console.WriteLine("If you are running with the default connection string, please make sure you have started the storage emulator. 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;
            }

            // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
            // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
            // to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
            // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
            // await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

            // Upload a BlockBlob to the newly created container
            Console.WriteLine("2. Uploading BlockBlob");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(ImageToUpload);

            // Set the blob's content type so that the browser knows to treat it as an image.
            blockBlob.Properties.ContentType = "image/png";
            await blockBlob.UploadFromFileAsync(ImageToUpload);

            // List all the blobs in the container.
            /// Note that the ListBlobs method is called synchronously, for the purposes of the sample. However, in a real-world
            /// application using the async/await pattern, best practices recommend using asynchronous methods consistently.
            Console.WriteLine("3. List Blobs in Container");
            BlobResultSegment segment = await container.ListBlobsSegmentedAsync(null);

            foreach (IListBlobItem blob in segment.Results)
            {
                // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                // Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
                Console.WriteLine("- {0} (type: {1})", blob.Uri, blob.GetType());
            }

            // Download a blob to your file system
            Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
            await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", ImageToUpload), FileMode.Create);

            // Create a read-only snapshot of the blob
            Console.WriteLine("5. Create a read-only snapshot of the blob");
            CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null);

            // Clean up after the demo. This line is not strictly necessary as the container is deleted in the next call.
            // It is included for the purposes of the example.
            Console.WriteLine("6. Delete block blob and all of its snapshots");
            await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);

            // Note that deleting the container also deletes any blobs in the container, and their snapshots.
            // In the case of the sample, we delete the blob and its snapshots, and then the container,
            // to show how to delete each kind of resource.
            Console.WriteLine("7. Delete Container");
            await container.DeleteIfExistsAsync();
        }
Esempio n. 8
0
        /// <summary>
        /// Basic operations to work with block blobs
        /// </summary>
        /// <returns>A Task object.</returns>
        private static async Task BasicStorageBlockBlobOperationsWithAccountSASAsync()
        {
            const string ImageToUpload = "Tiger.png";
            string       containerName = ContainerPrefix + Guid.NewGuid();

            // Get an account SAS token.
            string sasToken = GetAccountSASToken();

            // Use the account SAS token to create authentication credentials.
            StorageCredentials accountSAS = new StorageCredentials(sasToken);

            // Informational: Print the Account SAS Signature and Token.
            Console.WriteLine();
            Console.WriteLine("Account SAS Signature: " + accountSAS.SASSignature);
            Console.WriteLine("Account SAS Token: " + accountSAS.SASToken);
            Console.WriteLine();

            // Get the URI for the container.
            Uri containerUri = GetContainerUri(containerName);

            // Get a reference to a container using the URI and the SAS token.
            CloudBlobContainer container = new CloudBlobContainer(containerUri, accountSAS);

            try
            {
                // Create a container for organizing blobs within the storage account.
                Console.WriteLine("1. Creating Container using Account SAS");

                await container.CreateIfNotExistsAsync();
            }
            catch (StorageException e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("If you are running with the default configuration, please make sure you have started the storage emulator. 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
            {
                // To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
                // access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
                // to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
                // using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
                // await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

                // Upload a BlockBlob to the newly created container
                Console.WriteLine("2. Uploading BlockBlob");
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(ImageToUpload);
                await blockBlob.UploadFromFileAsync(ImageToUpload);

                // List all the blobs in the container
                Console.WriteLine("3. List Blobs in Container");
                BlobContinuationToken token = null;
                do
                {
                    BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

                    token = resultSegment.ContinuationToken;
                    foreach (IListBlobItem blob in resultSegment.Results)
                    {
                        // Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
                        Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
                    }
                }while (token != null);

                // Download a blob to your file system
                Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
                await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", ImageToUpload), FileMode.Create);

                // Create a read-only snapshot of the blob
                Console.WriteLine("5. Create a read-only snapshot of the blob");
                CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null);

                // Delete the blob and its snapshots.
                Console.WriteLine("6. Delete block Blob and all of its snapshots");
                await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
            finally
            {
                // Clean up after the demo.
                // Note that it is not necessary to delete all of the blobs in the container first; they will be deleted
                // with the container.
                Console.WriteLine("7. Delete Container");
                await container.DeleteIfExistsAsync();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Deletes a file or folder based on the path specified
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public async Task <List <BlobDto> > DeleteFile(string path)
        {
            var container = await GetContainer();

            List <BlobDto> deletedFiles = new List <BlobDto>();

            if (Path.HasExtension(path))
            {
                CloudBlockBlob blob = await GetBlob(path);

                if (_storageOptions.TakeSnapshots)
                {
                    await blob.CreateSnapshotAsync();
                }

                await blob.FetchAttributesAsync();

                deletedFiles.Add(new BlobDto
                {
                    ContentType  = blob.Properties.ContentType,
                    DateModified = blob.Properties.LastModified,
                    DateCreated  = blob.Properties.Created,
                    FileSize     = blob.Properties.Length,
                    Name         = HttpUtility.UrlDecode(blob.Metadata["FileName"]),
                    BlobType     = BlobType.File,
                    StoragePath  = blob.Uri.ToString(),
                    Path         = blob.Name
                });

                await blob.DeleteIfExistsAsync();
            }
            else
            {
                var         directory = container.GetDirectoryReference(path);
                List <Task> tasks     = new List <Task>();
                foreach (IListBlobItem result in await directory.ListBlobsAsync())
                {
                    if (IsCloudBlob(result))
                    {
                        CloudBlob blob = (CloudBlob)result;

                        await blob.FetchAttributesAsync();

                        deletedFiles.Add(new BlobDto
                        {
                            ContentType  = blob.Properties.ContentType,
                            DateModified = blob.Properties.LastModified,
                            DateCreated  = blob.Properties.Created,
                            FileSize     = blob.Properties.Length,
                            Name         = HttpUtility.UrlDecode(blob.Metadata["FileName"]),
                            BlobType     = BlobType.File,
                            StoragePath  = result.Uri.ToString(),
                            Path         = blob.Name
                        });

                        tasks.Add(blob.DeleteIfExistsAsync());
                    }
                }

                Task.WaitAll(tasks.ToArray());
            }

            return(deletedFiles);
        }
Esempio n. 10
0
 public async Task CreateSnapshotAsync(CloudBlockBlob cloudBlockBlob)
 {
     await cloudBlockBlob.CreateSnapshotAsync();
 }