Esempio n. 1
0
        /// <summary>
        /// Gets from blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public BlobResultModel GetBlob(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            if (blob.Exists())
            {
                BlobResultModel blobResultModel = new BlobResultModel();

                blobResultModel.Stream = new MemoryStream();
                blob.DownloadToStream(blobResultModel.Stream);
                blobResultModel.eTag = blob.Properties.ETag;

                return(blobResultModel);
            }
            else
            {
                return(null);
            }
        } // GetBlob
 static BlobUtility()
 {
     CloudStorage       = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Startup.AzureStorageConnectionString);
     CloudBlobClient    = CloudStorage.CreateCloudBlobClient();
     CloudBlobContainer = CloudBlobClient.GetContainerReference(Startup.BlobName);
     var res = CloudBlobContainer.CreateIfNotExistsAsync().Result;
 }
Esempio n. 3
0
        } // SaveBlob

        /// <summary>
        /// Verifies the eTag when saving
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <param name="text"></param>
        /// <param name="eTag"></param>
        public void SaveBlob(string blobContainer, string fileName, string text, string eTag)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            // save
            if (fileName.Contains("."))
            {
                blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf(".")));
            }
            else
            {
                blob.Properties.ContentType = "application/octet-stream";
            }

            Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = new Microsoft.WindowsAzure.Storage.AccessCondition();
            accessCondition.IfMatchETag = eTag;

            PutText(blob, text, accessCondition);
        } // SaveBlob
Esempio n. 4
0
        /// <summary>
        /// Saves to blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <param name="memoryStream"></param>
        public void SaveBlob(string blobContainer, string fileName, MemoryStream memoryStream)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = blobStorageDictionary[containerName];
            blobStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 30, 0);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob      = container.GetBlockBlobReference(fileName);

            // save
            if (fileName.Contains("."))
            {
                blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf(".")));
            }
            else
            {
                blob.Properties.ContentType = "application/octet-stream";
            }

            // save
            memoryStream.Position = 0; // rewind
            blob.UploadFromStream(memoryStream);
        } // SaveBlob
Esempio n. 5
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);
        }
Esempio n. 6
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);
 }
Esempio n. 7
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
Esempio n. 8
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);
        }
Esempio n. 9
0
        } // DeleteBlob

        /// <summary>
        /// Gets a list of blobs in the container
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <returns></returns>
        public IEnumerable <Microsoft.WindowsAzure.Storage.Blob.IListBlobItem> ListBlobs(string blobContainer)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            return(container.ListBlobs());
        }
Esempio n. 10
0
        } // GetBlob

        /// <summary>
        ///  Saves to blob storage
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        public void DeleteBlob(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            blob.DeleteIfExists();
        } // DeleteBlob
Esempio n. 11
0
        /// <summary>
        /// Gets from blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public BlobResultModel GetBlobAsText(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            return(GetText(blob));
        } // GetBlob
        } // Run

        private static string GetSASToken(string containerName, string customerWhitelistIPAddressMinimum, string customerWhitelistIPAddressMaximum,
                                          int tokenExpireTimeInMinutes, string cloudAccountName, string cloudKey)
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(cloudAccountName, cloudKey);
            Microsoft.WindowsAzure.Storage.CloudStorageAccount     storageAccount     = null;

            if (cloudAccountName == "devstoreaccount1")
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                        new Uri("http://127.0.0.1:10000/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10001/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10002/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10003/devstoreaccount1"));
            }
            else
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
            blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
            container.CreateIfNotExistsAsync().Wait();

            Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy policy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy();
            policy.Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Write | Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.List;
            policy.SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5); // always do in the past to prevent errors
            policy.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(tokenExpireTimeInMinutes);


            string sasToken = null;

            if (string.IsNullOrWhiteSpace(customerWhitelistIPAddressMinimum) ||
                string.IsNullOrWhiteSpace(customerWhitelistIPAddressMaximum) ||
                cloudAccountName == "devstoreaccount1")
            {
                sasToken = container.GetSharedAccessSignature(policy);
            }
            else
            {
                Microsoft.WindowsAzure.Storage.IPAddressOrRange iPAddressOrRange = new Microsoft.WindowsAzure.Storage.IPAddressOrRange(customerWhitelistIPAddressMinimum, customerWhitelistIPAddressMaximum);
                sasToken = container.GetSharedAccessSignature(policy, null, Microsoft.WindowsAzure.Storage.SharedAccessProtocol.HttpsOnly, iPAddressOrRange);
            }

            //  string url = "https://" + cloudAccountName + ".blob.core.windows.net" + sasToken;

            return(sasToken);
        }
Esempio n. 13
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);
        }
Esempio n. 14
0
        } // GetBlob

        /// <summary>
        /// Returns the raw blob handle so you can obtain leases, read data, delete, etc...
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob GetBlobHandle(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            if (blob.Exists())
            {
                return(blob);
            }
            else
            {
                return(null);
            }
        } // GetBlobHandle
Esempio n. 15
0
        public static async Task WriteToBLOB(List <Track> allTracks, string username)
        {
            var blobCreds         = new StorageCredentials(storageAccount, storageKey);
            var storageUri        = new Uri(@"https://" + storageAccount + ".blob.core.windows.net/");
            var blobstorageclient = new Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient(storageUri, blobCreds);
            var containerRef      = blobstorageclient.GetContainerReference("lastfmdata");

            await containerRef.CreateIfNotExistsAsync();

            var blobRef = containerRef.GetBlockBlobReference(string.Format("data/{0}.json", username));


            var allTracksSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(allTracks);

            var blobstream = await blobRef.OpenWriteAsync();

            using (var sw = new System.IO.StreamWriter(blobstream))
            {
                sw.Write(allTracksSerialized);
                sw.Close();
            }
            blobstream.Close();
        }