Beispiel #1
0
        static bool UploadFileWithAzcopyDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName)
        {
            bool bResult = false;

            try
            {
                System.Net.ServicePointManager.Expect100Continue      = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions option = new Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions();

                //option.ParallelOperations = 64;
                //option.MaximumCacheSize = 500000000;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager manager = new Microsoft.WindowsAzure.Storage.DataMovement.TransferManager(option);

                var fileStream = System.IO.File.OpenRead(filesource);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation source      = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(fileStream);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation destination = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(cloudBlob);

                //source.SourceUri = new Uri("file://" + sourceFileName);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferJob job = new Microsoft.WindowsAzure.Storage.DataMovement.TransferJob(source, destination, Microsoft.WindowsAzure.Storage.DataMovement.TransferMethod.SyncCopy);
                System.Threading.CancellationToken token = new System.Threading.CancellationToken();


                //Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UseV1MD5 = false;

                job.ContentType      = contentType;
                job.Starting        += Job_Starting;
                job.ProgressUpdated += Job_ProgressUpdated;
                job.Finished        += Job_Finished;

                Task t = manager.ExecuteJobAsync(job, token);
                t.Wait();
                if (job.IsCompleted == true)
                {
                    bResult = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Exception: " + e.InnerException.Message);
                }
            }
            return(bResult);
        }
Beispiel #2
0
        public async Task Upload(IFormFile file, string name)
        {
            try
            {
                Microsoft.WindowsAzure.Storage.CloudStorageAccount cloudStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(_constr);
                CloudBlobClient    cloudBlobClient    = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(_container);

                if (await cloudBlobContainer.CreateIfNotExistsAsync())
                {
                    await cloudBlobContainer.SetPermissionsAsync(
                        new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
                }

                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(name);
                cloudBlockBlob.Properties.ContentType = file.ContentType;
                await cloudBlockBlob.UploadFromStreamAsync(file.OpenReadStream());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #3
0
        public async Task <Uri> UploadBlob(Stream blobContent, bool isVideo, string reviewId, UploadProgress progressUpdater)
        {
            Uri blobAddress = null;

            try
            {
                var writeCredentials = await ObtainStorageCredentials(StoragePermissionType.Write);

                var csa = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(writeCredentials, APIKeys.StorageAccountName, APIKeys.StorageAccountUrlSuffix, true);

                var blobClient = csa.CreateCloudBlobClient();

                var container = blobClient.GetContainerReference(APIKeys.PhotosContainerName);

                var extension = isVideo ? "mp4" : "png";
                var blockBlob = container.GetBlockBlobReference($"{Guid.NewGuid()}.{extension}");

                blockBlob.Metadata.Add("reviewId", reviewId);
                await blockBlob.UploadFromStreamAsync(blobContent, null, null, null, progressUpdater, new CancellationToken());

                blobAddress = blockBlob.Uri;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"*** Error {ex.Message}");

                return(null);
            }

            return(blobAddress);
        }
        public CloudBlobContainer GetCloudBlobContainer()
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCreds;
            if (!string.IsNullOrEmpty(SasToken))
            {
                storageCreds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(SasToken);
            }
            else
            {
                storageCreds = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(StorageAccountName, StorageAccountKey);
            }

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCreds, StorageAccountName, null, true);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Get container reference
            var containerRef = blobClient.GetContainerReference(ContainerName);

            if (containerRef == null)
            {
                throw new Exception($"The container {ContainerName} cannot be found in storage account {StorageAccountName}");
            }

            return(containerRef);
        }
 static BlobUtility()
 {
     CloudStorage       = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Startup.AzureStorageConnectionString);
     CloudBlobClient    = CloudStorage.CreateCloudBlobClient();
     CloudBlobContainer = CloudBlobClient.GetContainerReference(Startup.BlobName);
     var res = CloudBlobContainer.CreateIfNotExistsAsync().Result;
 }
Beispiel #6
0
        /// <summary>
        /// Exports the list of containers in a given storage account
        /// </summary>
        /// <param name="account">Storage account to get the name and key of the storage account whose blobs are to be exported</param>
        /// <returns>List of storage account containers</returns>
        private List <Container> ExportStorageAccountContainers(Microsoft.WindowsAzure.Management.Storage.Models.StorageAccount account)
        {
            string           methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            List <Container> containers = new List <Container>();

            try
            {
                string srcStorageAccountKey = GetStorageAccountKeysFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials, account.Name).PrimaryKey;

                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                    new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                        new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(account.Name, srcStorageAccountKey), true);
                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                foreach (var item in cloudBlobClient.ListContainers())
                {
                    List <Blob> blobs = new List <Blob>();
                    containers.Add(new Container
                    {
                        ContainerName = item.Name,
                        BlobDetails   = ExportBlobs(item.ListBlobs(null, false), blobs)
                    });
                }

                return(containers);
            }
            catch (Exception ex)
            {
                Logger.Error(methodName, ex);
                throw;
            }
        }
        public FileStreamResult File(string filename)
        {
            var stockpickFormsAzurequeueConnectionstring = "Stockpick.Forms.AzureQueue.Connectionstring";
            var connectionstring = Sitecore.Configuration.Settings.GetSetting(stockpickFormsAzurequeueConnectionstring);

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionstring);

            var cloudBlobClient = storageAccount.CreateCloudBlobClient();
            var blobContainer   = cloudBlobClient.GetContainerReference("stockpickformsblob");

            blobContainer.CreateIfNotExists();

            var cloudResolver = new KeyVaultKeyResolver(Keyvault.GetToken);
            var url           = Sitecore.Configuration.Settings.GetSetting("Stockpick.Forms.KeyFault.Key.URL");

            if (string.IsNullOrEmpty(url))
            {
                Log.Error("config key Stockpick.Forms.KeyFault.Key.URL is emty", this);
            }
            var key = cloudResolver.ResolveKeyAsync(url, CancellationToken.None).GetAwaiter().GetResult();

            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);


            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
            BlobRequestOptions   options = new BlobRequestOptions()
            {
                EncryptionPolicy = policy
            };

            Stream blobStream = blob.OpenRead(null, options);

            return(new FileStreamResult(blobStream, "application/x-binary"));
        }
Beispiel #8
0
        private static Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient InitializeClient()
        {
            var accountInfo = AccountInfo.Instance;
            var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountInfo.AccountName, accountInfo.SharedKey);
            var account     = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, false);

            return(account.CreateCloudBlobClient());
        }
 public AzureBlobHelper(string accountName, string accessKey, string groupName, bool useHttps = false)
 {
     var credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accessKey);
     var storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, useHttps);
     this.CloudBlobClient = storageAccount.CreateCloudBlobClient();
     this.Container = new ContainerLogic(this, groupName);
     this.Blob = new BlobLogic(this, groupName);
 }
Beispiel #10
0
        public AzureBlobHelper(string accountName, string accessKey, string groupName, bool useHttps = false)
        {
            var credentials    = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accessKey);
            var storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, useHttps);

            this.CloudBlobClient = storageAccount.CreateCloudBlobClient();
            this.Container       = new ContainerLogic(this, groupName);
            this.Blob            = new BlobLogic(this, groupName);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AzureServerPackageRepository"/> class.
 /// </summary>
 /// <param name="packageLocator">The package locator.</param>
 /// <param name="packageSerializer">The package serializer.</param>
 public AzureServerPackageRepository(IPackageLocator packageLocator, IAzurePackageSerializer packageSerializer)
 {
     _packageLocator = packageLocator;
     _packageSerializer = packageSerializer;
     var azureConnectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
     _storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(azureConnectionString);
     _blobClient = _storageAccount.CreateCloudBlobClient();
     _helper = Microsoft.WindowsAzure.CloudStorageAccount.Parse(azureConnectionString);
 }
Beispiel #12
0
        static bool UploadFileWithAzcopyDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName )
        {
            bool bResult = false;
            try
            {

                System.Net.ServicePointManager.Expect100Continue = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions option = new Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions();

                //option.ParallelOperations = 64;
                //option.MaximumCacheSize = 500000000;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager manager = new Microsoft.WindowsAzure.Storage.DataMovement.TransferManager(option);

                var fileStream = System.IO.File.OpenRead(filesource);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation source = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(fileStream);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation destination = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(cloudBlob);

                //source.SourceUri = new Uri("file://" + sourceFileName);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferJob job = new Microsoft.WindowsAzure.Storage.DataMovement.TransferJob(source, destination, Microsoft.WindowsAzure.Storage.DataMovement.TransferMethod.SyncCopy);
                System.Threading.CancellationToken token = new System.Threading.CancellationToken();


                //Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UseV1MD5 = false;

                job.ContentType = contentType;
                job.Starting += Job_Starting;
                job.ProgressUpdated += Job_ProgressUpdated;
                job.Finished += Job_Finished;

                Task t = manager.ExecuteJobAsync(job, token);
                t.Wait();
                if (job.IsCompleted == true)
                    bResult = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if(e.InnerException!= null)
                    Console.WriteLine("Exception: " + e.InnerException.Message);
            }
            return bResult;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureServerPackageRepository"/> class.
        /// </summary>
        /// <param name="packageLocator">The package locator.</param>
        /// <param name="packageSerializer">The package serializer.</param>
        public AzureServerPackageRepository(IPackageLocator packageLocator, IAzurePackageSerializer packageSerializer)
        {
            _packageLocator    = packageLocator;
            _packageSerializer = packageSerializer;
            var azureConnectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

            _storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(azureConnectionString);
            _blobClient     = _storageAccount.CreateCloudBlobClient();
            _helper         = Microsoft.WindowsAzure.CloudStorageAccount.Parse(azureConnectionString);
        }
        public AzureServerPackageRepository(IPackageLocator packageLocator, 
                                            IAzurePackageSerializer packageSerializer,
                                            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount)
        {
            _packageLocator = packageLocator;
            _packageSerializer = packageSerializer;

            _storageAccount = storageAccount;
            _blobClient = _storageAccount.CreateCloudBlobClient();
        }
        public AzureServerPackageRepository(IPackageLocator packageLocator,
                                            IAzurePackageSerializer packageSerializer,
                                            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount)
        {
            _packageLocator    = packageLocator;
            _packageSerializer = packageSerializer;

            _storageAccount = storageAccount;
            _blobClient     = _storageAccount.CreateCloudBlobClient();
        }
Beispiel #16
0
        private static CloudBlobContainer GetBlobContainer(Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount)
        {
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container  = blobClient.GetContainerReference(new T().BlobContainer.ToString());

            container.CreateIfNotExistsAsync();
            container.SetPermissionsAsync(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
            return(container);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //These are not part of .NET Core... they are seperate libraries that must be installed via a program called NuGet.
            //Right click on your Project -> Manage NuGet Packages
            //Configuration.GetConnectionString("AdventureWorks2016");


            string countryClubConnectionString = Configuration.GetConnectionString("CountryClub");

            //using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
            //using Microsoft.EntityFrameworkCore;
            //using Microsoft.AspNetCore.Identity;
            services.AddDbContext <CountryClubDbContext>(opt => opt.UseSqlServer(countryClubConnectionString));

            services.AddIdentity <CountryClubUser, IdentityRole>(options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <CountryClubDbContext>()
            .AddDefaultTokenProviders();

            services.AddMvc().AddJsonOptions(o =>
            {
                o.SerializerSettings.ContractResolver      = new CamelCasePropertyNamesContractResolver();
                o.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            });

            services.AddTransient((x) => { return(new EmailService(Configuration["SendGridKey"])); });
            services.AddTransient((x) => { return(new Braintree.BraintreeGateway(
                                                      Configuration["BraintreeEnvironment"],
                                                      Configuration["BraintreeMerchantId"],
                                                      Configuration["BraintreePublicKey"],
                                                      Configuration["BraintreePrivateKey"])); });
            services.AddTransient((x) =>
            {
                SmartyStreets.ClientBuilder builder = new SmartyStreets.ClientBuilder(Configuration["SmartyStreetsAuthId"], Configuration["SmartyStreetsAuthToken"]);
                return(builder.BuildUsStreetApiClient());
            });

            services.AddTransient((x) =>
            {
                Microsoft.WindowsAzure.Storage.CloudStorageAccount account = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Configuration.GetConnectionString("CountryClubBlob"));
                return(account.CreateCloudBlobClient());
            });
        }
Beispiel #18
0
        static bool UploadFileWithNuggetDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName)
        {
            bool bResult = false;

            try
            {
                System.Net.ServicePointManager.Expect100Continue      = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;
//                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.Configurations.ParallelOperations = 64;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions options = new Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions();
                options.ContentType = contentType;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferContext context = new Microsoft.WindowsAzure.Storage.DataMovement.TransferContext();
                context.ProgressHandler = new Progress <Microsoft.WindowsAzure.Storage.DataMovement.TransferProgress>((progress) =>
                {
                    Console.WriteLine("Bytes Uploaded: {0}", progress.BytesTransferred);
                });
                StartTime = DateTime.Now;
                Console.WriteLine(string.Format("Starting upload at {0:d/M/yyyy HH:mm:ss.fff}", StartTime));
                // Start the upload
                Task t = Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UploadAsync(filesource, cloudBlob, options, context);
                t.Wait();
                bResult = true;
                Console.WriteLine(string.Format("Upload finished at {0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now));
                Console.WriteLine(string.Format("Time Elapsed in seconds: " + (DateTime.Now - StartTime).TotalSeconds.ToString()));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Exception: " + e.InnerException.Message);
                }
            }
            return(bResult);
        }
Beispiel #19
0
        public void Login(string storageName, string key)
        {
            var credentials = new StorageCredentials(storageName, key);
            var account     = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, false);

            Containers.Clear();
            Blobs.Clear();
            account.CreateCloudBlobClient().ListContainers().ToList().ForEach(Containers.Add);
            var lastUsed = Preferences.Instance.LastUsedContainer;
            CloudBlobContainer container;

            if (lastUsed != null && (container = Containers.FirstOrDefault(c => c.Name == lastUsed)) != null)
            {
                CurrentContainer = container;
            }
        }
Beispiel #20
0
        static bool UploadFileWithNuggetDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName)
        {
            bool bResult = false;
            try
            { 
                System.Net.ServicePointManager.Expect100Continue = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;
//                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.Configurations.ParallelOperations = 64;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions options = new Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions();
                options.ContentType = contentType;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferContext context = new Microsoft.WindowsAzure.Storage.DataMovement.TransferContext();
                context.ProgressHandler = new Progress<Microsoft.WindowsAzure.Storage.DataMovement.TransferProgress>((progress) =>
                {
                    Console.WriteLine("Bytes Uploaded: {0}", progress.BytesTransferred);
                });
                StartTime = DateTime.Now;
                Console.WriteLine(string.Format("Starting upload at {0:d/M/yyyy HH:mm:ss.fff}", StartTime));
                // Start the upload
                Task t = Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UploadAsync(filesource, cloudBlob, options, context);
                t.Wait();
                bResult = true;
                Console.WriteLine(string.Format("Upload finished at {0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now));
                Console.WriteLine(string.Format("Time Elapsed in seconds: " + (DateTime.Now - StartTime).TotalSeconds.ToString()));

            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if(e.InnerException!= null)
                    Console.WriteLine("Exception: " + e.InnerException.Message);
            }
            return bResult;
        }
Beispiel #21
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            System.Data.Common.DbConnectionStringBuilder builder =
                new System.Data.Common.DbConnectionStringBuilder();

            services.AddMvc();
            services.AddDistributedMemoryCache();
            services.AddAntiforgery();
            services.AddSession();

            services.Configure <Models.ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.AddOptions();

            services.AddDbContext <Models.DavidTestContext>(opt =>
                                                            opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            //			, sqlOptions => sqlOptions.MigrationsAssembly(this.GetType().Assembly.FullName))
            //			);

            // Options for Password reqirements
            services.AddIdentity <ApplicationUser, IdentityRole>(
                options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequiredLength         = 5;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <Models.DavidTestContext>()
            .AddDefaultTokenProviders();


            services.AddTransient <SendGrid.SendGridClient>((x) =>
            {
                return(new SendGrid.SendGridClient(Configuration["sendgrid"]));
            });

            services.AddTransient <Braintree.BraintreeGateway>((x) =>
            {
                return(new Braintree.BraintreeGateway(
                           Configuration["braintree.environment"],
                           Configuration["braintree.merchantid"],
                           Configuration["braintree.publickey"],
                           Configuration["braintree.privatekey"]
                           ));
            });

            services.AddTransient <Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient>((x) =>
            {
                Microsoft.WindowsAzure.Storage.CloudStorageAccount account = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Configuration["StorageConnectionString"]);
                return(account.CreateCloudBlobClient());
            });

            //		services.AddTransient<SmartyStreets.USStreetApi.Client>((x) =>
            //		{
            //			var client = new SmartyStreets.ClientBuilder(
            //				Configuration["smartystreets.authid"],
            //				Configuration["smartystreets.authtoken"])
            //					.BuildUsStreetApiClient();
            //
            //			return client;
            //		});
        }
Beispiel #22
0
        public async Task <IActionResult> Index(PowerpointFile powerpointFile)
        {
            // Create a local file in the ./data/ directory for uploading and downloading
            string localPath = "";
            //powerpointFile.FileTitle = powerpointFile.MyFile.FileName;
            string results_fileName  = "results_" + Guid.NewGuid().ToString() + ".txt";
            string localFilePath_txt = Path.Combine(localPath, results_fileName);



            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageacc = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString);



            Microsoft.Azure.Storage.CloudStorageAccount queueStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);



            // Create a BlobServiceClient object which will be used to create a container client
            CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();

            CloudQueueClient queueClient = queueStorageAccount.CreateCloudQueueClient();

            // Retrieve a reference to a queue.
            CloudQueue queue = queueClient.GetQueueReference(queueName);

            // Create the queue if it doesn't already exist.
            queue.CreateIfNotExists();


            // Create the container and return a container client object
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync();

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(results_fileName);

            blockBlob.Properties.ContentType = "text/plain";

            // Write text to the file
            await System.IO.File.WriteAllTextAsync(localFilePath_txt, "Working on ppt_xx.pptx");

            using (var filestream = System.IO.File.OpenRead(localFilePath_txt))
            {
                await blockBlob.UploadFromStreamAsync(filestream);

                //send message to Queue
                CloudQueueMessage message = new CloudQueueMessage(blockBlob.Uri.ToString());
                queue.AddMessage(message);
            }

            /*
             * https://github.com/jayesh-tanna/azure-blob/blob/master/AzureBlob/AzureBlob.API/Controllers/UserController.cs
             */

            if (Microsoft.Azure.Storage.CloudStorageAccount.TryParse(connectionString, out Microsoft.Azure.Storage.CloudStorageAccount storageAccount))
            {
                await container.CreateIfNotExistsAsync();

                //MS: Don't rely on or trust the FileName property without validation. The FileName property should only be used for display purposes.
                var picBlob = container.GetBlockBlobReference(powerpointFile.MyFile.FileName);

                await picBlob.UploadFromStreamAsync(powerpointFile.MyFile.OpenReadStream());

                //send message to Queue
                CloudQueueMessage message = new CloudQueueMessage(picBlob.Uri.ToString());
                queue.AddMessage(message);

                // return Ok(picBlob.Uri);
            }



            return(Redirect("/Home/LinkPage"));
        }
Beispiel #23
0
    /// <summary>
    /// Copy the blobs to blob destination endpoint
    /// </summary>
    /// <param name="storageAccounts">storage accounts from where the blobs need to be migrated</param>
    public void CopyVMIndependentBlob(List<Azure.DataCenterMigration.Models.StorageAccount> storageAccounts)
    {
      string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
      ////Stopwatch for tracking time taken to copy all the storage accounts.
      Stopwatch swAllCopyStorageAccounts = new Stopwatch();
      swAllCopyStorageAccounts.Start();

      Parallel.ForEach(storageAccounts, storageAccount =>
      {
        string sourceStorageAccountName = GetSourceResourceName(ResourceType.StorageAccount, storageAccount.StorageAccountDetails.Name);
        string destStorageAccountName = GetDestinationResourceName(ResourceType.StorageAccount, sourceStorageAccountName);

        string sourceStorageAccountKey = GetStorageAccountKeysFromMSAzure(importParameters.SourceSubscriptionSettings.Credentials,
             sourceStorageAccountName).PrimaryKey;
        string destStorageAccountKey = GetStorageAccountKeysFromMSAzure(importParameters.DestinationSubscriptionSettings.Credentials,
           destStorageAccountName).PrimaryKey;

        var containers = storageAccount.Containers.ToList();
        ////Stopwatch for tracking time taken to copy all the blobs in each storage account.
        Stopwatch swCopyEachStorageAccount = new Stopwatch();
        swCopyEachStorageAccount.Start();

        BlobRequestOptions requestOptions = Retry.GetBlobRequestOptions(importParameters.DeltaBackOff, importParameters.RetryCount);
        foreach (var container in containers)
        {
          ////Stopwatch for tracking time taken to copy all the blobs in each container.
          Stopwatch swCopyContainer = new Stopwatch();
          swCopyContainer.Start();

          //// if the container has no blobs create the empty container
          if (container.BlobDetails.Count == 0)
          {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccountObj =
              new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                  new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(destStorageAccountName, destStorageAccountKey), true);

            CloudBlobClient cloudBlobClient = storageAccountObj.CreateCloudBlobClient();
            CloudBlobContainer emptyContainer = cloudBlobClient.GetContainerReference(container.ContainerName);

            if (!emptyContainer.Exists())
            {
              lock (thisLockContainer)
              {
                if (!emptyContainer.Exists())
                {
                  emptyContainer.Create();

                }
              }
            }
          }
          foreach (var item in container.BlobDetails.Where(a => ! a.IsExcluded && ! ExcludeVMVHDList.Contains(a.BlobURI)))
          {
            try
            {
              ////Stopwatch for tracking time taken to copy each blob
              Stopwatch swCopyEachBlob = new Stopwatch();
              swCopyEachBlob.Start();

              bool deletedPendingBlob = false;
              if (item.BlobType == BlobType.PageBlob.ToString())
              {
                //// get all details of destination blob.
                CloudPageBlob destBlob = GetCloudBlob(item.BlobName, container.ContainerName, destStorageAccountKey, destStorageAccountName, false);
                //// Check the status of blob if it is already present. Delete the blob if the status is pending.
                if (destBlob.Exists())
                {
                  CloudPageBlob destBlobInfo = (CloudPageBlob)destBlob.Container.GetBlobReferenceFromServer(item.BlobName);
                  if (destBlobInfo.CopyState.Status == CopyStatus.Pending)
                  {
                    Logger.Info(methodName, string.Format(ProgressResources.DeleteNonSuccessBlob, destBlobInfo.CopyState.Status),
                  ResourceType.Blob.ToString(), item.BlobName);
                    destBlobInfo.AbortCopy(destBlobInfo.CopyState.CopyId, null, requestOptions);
                    destBlobInfo.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, requestOptions, null);
                    deletedPendingBlob = true;
                  }
                }

                if (!destBlob.Exists() || (deletedPendingBlob))
                {
                  Logger.Info(methodName, String.Format(ProgressResources.CopyBlobToDestinationStarted, container.ContainerName, item.BlobName, destStorageAccountName),
                    ResourceType.Blob.ToString(), item.BlobName);

                  //// get all details of source blob.
                  Microsoft.WindowsAzure.Storage.Blob.CloudPageBlob sourceBlob = GetCloudBlob(item.BlobName, container.ContainerName,
                      sourceStorageAccountKey, sourceStorageAccountName, true);
                  destBlob = GetCloudBlob(item.BlobName, container.ContainerName, destStorageAccountKey, destStorageAccountName, false);

                  //// get Shared Access Signature for private containers.
                  var sas = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                  {
                    SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
                    SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
                    Permissions = SharedAccessBlobPermissions.Read,
                  });

                  var srcBlobSasUri = string.Format("{0}{1}", sourceBlob.Uri, sas);
                  string destUri = string.Format(Constants.StorageAccountMediaLink, destStorageAccountName, container.ContainerName, item.BlobName);

                  //// copy blob from source to destination.
                  string copyId = destBlob.StartCopyFromBlob(new Uri(srcBlobSasUri), null, null, requestOptions, null);

                  dcMigration.ReportProgress(string.Format(ProgressResources.BlobCopyStarted, item.BlobURI, destUri));
                  WaitForBlobCopy(destBlob.Container, item.BlobName, item.BlobType);

                }
              }
              else if (item.BlobType == BlobType.BlockBlob.ToString())
              {
                // get all details of destination blob.
                CloudBlockBlob destBlob = GetCloudBlockBlob(item.BlobName, container.ContainerName, destStorageAccountKey, destStorageAccountName, false);
                // Check the status of blob if it is already present. Delete the blob if the status is pending.
                if (destBlob.Exists())
                {
                  CloudBlockBlob destBlobInfo = (CloudBlockBlob)destBlob.Container.GetBlobReferenceFromServer(item.BlobName);
                  if (destBlobInfo.CopyState.Status == CopyStatus.Pending)
                  {
                    Logger.Info(methodName, string.Format(ProgressResources.DeleteNonSuccessBlob, destBlobInfo.CopyState.Status),
                    ResourceType.Blob.ToString(), item.BlobName);

                    destBlobInfo.AbortCopy(destBlobInfo.CopyState.CopyId, null, requestOptions);
                    destBlobInfo.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, requestOptions, null);
                    deletedPendingBlob = true;
                  }
                }

                if (!destBlob.Exists() || (deletedPendingBlob))
                {
                  Logger.Info(methodName, String.Format(ProgressResources.CopyBlobToDestinationStarted, container.ContainerName, item.BlobName, destStorageAccountName),
                ResourceType.Blob.ToString(), item.BlobName);

                  // get all details of source blob.
                  Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob sourceBlob = GetCloudBlockBlob(item.BlobName, container.ContainerName,
                      sourceStorageAccountKey, sourceStorageAccountName, true);
                  destBlob = GetCloudBlockBlob(item.BlobName, container.ContainerName, destStorageAccountKey, destStorageAccountName, false);

                  // get Shared Access Signature for private containers.
                  var sas = sourceBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                  {
                    SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
                    SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
                    Permissions = SharedAccessBlobPermissions.Read,
                  });

                  var srcBlobSasUri = string.Format("{0}{1}", sourceBlob.Uri, sas);
                  string destUri = string.Format(Constants.StorageAccountMediaLink, destStorageAccountName, container.ContainerName, item.BlobName);

                  // copy blob from source to destination.
                  string copyId = destBlob.StartCopyFromBlob(new Uri(srcBlobSasUri), null, null, requestOptions, null);

                  dcMigration.ReportProgress(string.Format(ProgressResources.BlobCopyStarted, item.BlobURI, destUri));
                  WaitForBlobCopy(destBlob.Container, item.BlobName, item.BlobType);

                }
              }
              swCopyEachBlob.Stop();
              Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swCopyEachBlob.Elapsed.Days, swCopyEachBlob.Elapsed.Hours,
                  swCopyEachBlob.Elapsed.Minutes, swCopyEachBlob.Elapsed.Seconds), ResourceType.Blob.ToString());
            }
            catch (AggregateException exAgg)
            {
              foreach (var ex in exAgg.InnerExceptions)
              {
                Logger.Error(methodName, exAgg, ResourceType.StorageAccount.ToString(), item.BlobName);
              }
              throw;
            }
            catch (Exception ex)
            {
              Logger.Error(methodName, ex, ResourceType.StorageAccount.ToString(), item.BlobName);
              throw;
            }
          }
          swCopyContainer.Stop();
          Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swCopyContainer.Elapsed.Days, swCopyContainer.Elapsed.Hours,
              swCopyContainer.Elapsed.Minutes, swCopyContainer.Elapsed.Seconds), ResourceType.Blob.ToString());
        }

        swCopyEachStorageAccount.Stop();
        Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swCopyEachStorageAccount.Elapsed.Days, swCopyEachStorageAccount.Elapsed.Hours,
              swCopyEachStorageAccount.Elapsed.Minutes, swCopyEachStorageAccount.Elapsed.Seconds), ResourceType.Blob.ToString());

      });

      swAllCopyStorageAccounts.Stop();
      Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swAllCopyStorageAccounts.Elapsed.Days, swAllCopyStorageAccounts.Elapsed.Hours,
            swAllCopyStorageAccounts.Elapsed.Minutes, swAllCopyStorageAccounts.Elapsed.Seconds), ResourceType.Blob.ToString());

    }
Beispiel #24
0
        /// <summary>
        /// Used to pull back the cloud blob that should be copied from or to
        /// </summary>
        /// <param name="blobName">blob name</param>
        /// <param name="containerName">container name</param>
        /// <param name="storageKey">storage key</param>
        /// <param name="storageAccountName">storage account name</param>
        /// <param name="sourceSubscription">true if it is getting blob value for source subscription</param>
        /// <returns>the cloud blob that should be copied from or to</returns>
        private CloudPageBlob GetCloudBlob(string blobName, string containerName, string storageKey,
            string storageAccountName, bool sourceSubscription)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            Logger.Info(methodName, string.Format(ProgressResources.GetCloudBlobStarted, blobName, storageAccountName),
                ResourceType.Blob.ToString(), blobName);

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageKey), true);
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
            if (!container.Exists() && sourceSubscription == false)
            {
                lock (thisLockContainer)
                {
                    if (!container.Exists())
                    {
                        container.Create();
                    }
                }
            }
            Logger.Info(methodName, string.Format(ProgressResources.CloudBlobInfoRecieved, blobName, storageAccountName),
                ResourceType.Blob.ToString(), blobName);
            return (container.GetPageBlobReference(blobName));
        }
Beispiel #25
0
        public static async Task ProcessQueueMessage([QueueTrigger("queuebrandingpolice")] string message, ILogger logger)
        {
            logger.LogInformation(message);

            try
            {
                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageacc = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString);
                // Create a BlobServiceClient object which will be used to create a container client
                CloudBlobClient blobCliente = storageacc.CreateCloudBlobClient();
                // Create the container and return a container client object
                CloudBlobContainer container = blobCliente.GetContainerReference(containerName);
                await container.CreateIfNotExistsAsync();

                // Retrieve storage account from connection string.
                Microsoft.Azure.Storage.CloudStorageAccount queueStorageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);
                CloudQueueClient queueClient = queueStorageAccount.CreateCloudQueueClient();
                // Retrieve a reference to a queue.
                CloudQueue queue = queueClient.GetQueueReference(queueName);

                // Create a BlobServiceClient object which will be used to create a container client
                BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
                // Create the container and return a container client object
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

                // Get the message from the queue and update the message contents.
                CloudQueueMessage messagequeue = queue.GetMessage();
                // Get the next message
                //CloudQueueMessage retrievedMessage = queue.GetMessage();


                string localPath     = "";
                string filename_pptx = null;
                string GetExtension  = null;

                Uri uri = new Uri(message);

                GetExtension = System.IO.Path.GetExtension(uri.LocalPath);

                if (GetExtension == ".txt")
                {
                    filename_txt      = System.IO.Path.GetFileName(uri.LocalPath);
                    localFilePath_txt = Path.Combine(localPath, filename_txt);
                }
                if (GetExtension != ".txt")
                {
                    filename_pptx = System.IO.Path.GetFileName(uri.LocalPath);
                    CloudBlockBlob blockBlob_txt = container.GetBlockBlobReference(filename_txt);
                    blockBlob_txt.Properties.ContentType = "text/plain";

                    // Get a reference to a blob
                    BlobClient blobClient_pptx = containerClient.GetBlobClient(filename_pptx);

                    BlobDownloadInfo download = blobClient_pptx.Download();
                    using (FileStream file1 = File.OpenWrite(filename_pptx))
                    {
                        download.Content.CopyTo(file1);
                        file1.Close();
                        int    numberOfSlides = CountSlides(file1.Name);
                        string result = "", search = "Windows Azure";
                        System.Console.WriteLine("Number of slides = {0}", numberOfSlides);
                        string slideText;
                        for (int i = 0; i < numberOfSlides; i++)
                        {
                            GetSlideIdAndText(out slideText, file1.Name, i);
                            System.Console.WriteLine("Slide #{0} contains: {1}", i + 1, slideText);
                            // append text to the file
                            //await System.IO.File.AppendAllTextAsync(localFilePath_txt, slideText);
                            if (slideText.Contains(search))
                            {
                                System.Console.WriteLine("\nRETURN 0\n", i + 1, slideText);
                            }
                            result += result + String.Format("In der {0}.Slide benutzen Sie den Begriff Windows Azure statt Microsoft Azure\n", i + 1);
                        }
                        await System.IO.File.AppendAllTextAsync(localFilePath_txt, result);

                        using (var filestream = System.IO.File.OpenRead(localFilePath_txt))
                        {
                            await blockBlob_txt.UploadFromStreamAsync(filestream);
                        }
                    }

                    //Delete .pptx blob
                    await blobClient_pptx.DeleteIfExistsAsync();


                    filename_txt      = null;
                    localFilePath_txt = null;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
Beispiel #26
0
        /// <summary>
        /// Exports the list of containers in a given storage account
        /// </summary>
        /// <param name="account">Storage account to get the name and key of the storage account whose blobs are to be exported</param>
        /// <returns>List of storage account containers</returns>
        private List<Container> ExportStorageAccountContainers(Microsoft.WindowsAzure.Management.Storage.Models.StorageAccount account)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            List<Container> containers = new List<Container>();
            try
            {
                string srcStorageAccountKey = GetStorageAccountKeysFromMSAzure(exportParameters.SourceSubscriptionSettings.Credentials, account.Name).PrimaryKey;

                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                           new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                               new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(account.Name, srcStorageAccountKey), true);
                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                foreach (var item in cloudBlobClient.ListContainers())
                {

                    List<Blob> blobs = new List<Blob>();
                    containers.Add(new Container
                    {
                        ContainerName = item.Name,
                        BlobDetails = ExportBlobs(item.ListBlobs(null, false), blobs)
                    });
                }

                return containers;
            }
            catch (Exception ex)
            {
                Logger.Error(methodName, ex);
                throw;
            }
        }
        private async Task <(bool, string)> UploadToBlob(string filename, byte[] imageBuffer = null, Stream stream = null)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount     storageAccount     = null;
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cloudBlobContainer = null;
            string storageConnectionString = _configuration["storageconnectionstring"];

            // Check whether the connection string can be parsed.
            if (Microsoft.WindowsAzure.Storage.CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    // Create a container called 'uploadblob' and append a GUID value to it to make the name unique.
                    cloudBlobContainer = cloudBlobClient.GetContainerReference("womeninworkforce");// ("uploadblob" + Guid.NewGuid().ToString());
                    await cloudBlobContainer.CreateIfNotExistsAsync();

                    // Set the permissions so the blobs are public.
                    BlobContainerPermissions permissions = new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    };
                    await cloudBlobContainer.SetPermissionsAsync(permissions);

                    // Get a reference to the blob address, then upload the file to the blob.
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
                    List <Task>    tasks          = new List <Task>();
                    int            count          = 0;
                    if (imageBuffer != null)
                    {
                        // OPTION A: use imageBuffer (converted from memory stream)
                        await cloudBlockBlob.UploadFromByteArrayAsync(imageBuffer, 0, imageBuffer.Length);

                        //tasks.Add(cloudBlockBlob.UploadFromByteArrayAsync(imageBuffer, 0, options, null).ContinueWith((t) =>
                        //{
                        //    sem.Release();
                        //    Interlocked.Increment(ref completed_count);
                        //}));
                        //count++;
                    }
                    else if (stream != null)
                    {
                        // OPTION B: pass in memory stream directly
                        await cloudBlockBlob.UploadFromStreamAsync(stream);
                    }
                    else
                    {
                        return(false, null);
                    }

                    return(true, cloudBlockBlob.SnapshotQualifiedStorageUri.PrimaryUri.ToString());
                }
                catch (Microsoft.WindowsAzure.Storage.StorageException ex)
                {
                    return(false, null);
                }
                finally
                {
                    // OPTIONAL: Clean up resources, e.g. blob container
                    //if (cloudBlobContainer != null)
                    //{
                    //    await cloudBlobContainer.DeleteIfExistsAsync();
                    //}
                }
            }
            else
            {
                return(false, null);
            }
        }
Beispiel #28
0
        /// <summary>
        /// Creates the storage and gets a reference (once)
        /// </summary>
        private static void InitializeStorage(string blobContainer)
        {
            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
            {
                return;
            }

            lock (gate)
            {
                if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
                {
                    return;
                }

                try
                {
                    Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName,
                        Sample.Azure.Common.Setting.SettingService.CloudStorageKey);

                    Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint));

                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();

                    int    blobSaveTimeoutInMinutes = DEFAULT_SAVE_AND_READ_BLOB_TIMEOUT_IN_MINUTES;
                    string timeOutOverRide          = Sample.Azure.Common.Setting.SettingService.SaveAndReadBlobTimeoutInMinutes;
                    if (timeOutOverRide != null)
                    {
                        blobSaveTimeoutInMinutes = int.Parse(timeOutOverRide);
                    }
                    blobStorage.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(blobSaveTimeoutInMinutes);

                    blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cloudBlobContainer = blobStorage.GetContainerReference(containerName);
                    cloudBlobContainer.CreateIfNotExists();

                    Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions permissions = new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions();
                    permissions.PublicAccess = Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off;
                    cloudBlobContainer.SetPermissions(permissions);

                    blobStorageDictionary.Add(containerName, blobStorage);
                    storageInitializedDictionary.Add(containerName, true);
                }
                catch (Exception ex)
                {
                    throw new Exception("Storage services initialization failure. "
                                        + "Check your storage account configuration settings. If running locally, "
                                        + "ensure that the Development Storage service is running. \n"
                                        + ex.Message);
                }
            } // lock
        }     // InitializeStorage