public CloudBlobContainer GetBlobContainer(string container)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                StorageConnectionString);

            ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint);

            tableServicePoint.UseNagleAlgorithm = false;

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

            // Retrieve a reference to a container.
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);

            // Create the container if it doesn't already exist.
            blobContainer.CreateIfNotExistsAsync().Wait();

            blobContainer.SetPermissionsAsync(
                new BlobContainerPermissions
            {
                PublicAccess =
                    BlobContainerPublicAccessType.Blob
            }).Wait();

            return(blobContainer);
        }
 internal FunctionsController(
     CloudStorageAccount account,
     CloudBlobClient blobClient,
     IFunctionInstanceLookup functionInstanceLookup,
     IFunctionLookup functionLookup,
     IFunctionIndexReader functionIndexReader,
     IHeartbeatValidityMonitor heartbeatMonitor,
     IAborter aborter,
     IRecentInvocationIndexReader recentInvocationsReader,
     IRecentInvocationIndexByFunctionReader recentInvocationsByFunctionReader,
     IRecentInvocationIndexByJobRunReader recentInvocationsByJobRunReader,
     IRecentInvocationIndexByParentReader recentInvocationsByParentReader,
     IFunctionStatisticsReader statisticsReader,
     ILogReader reader)
 {
     _account = account;
     _blobClient = blobClient;
     _functionInstanceLookup = functionInstanceLookup;
     _functionLookup = functionLookup;
     _functionIndexReader = functionIndexReader;
     _heartbeatMonitor = heartbeatMonitor;
     _aborter = aborter;
     _recentInvocationsReader = recentInvocationsReader;
     _recentInvocationsByFunctionReader = recentInvocationsByFunctionReader;
     _recentInvocationsByJobRunReader = recentInvocationsByJobRunReader;
     _recentInvocationsByParentReader = recentInvocationsByParentReader;
     _statisticsReader = statisticsReader;
     _reader = reader;
 }
 public BlobStorageManager(string blobConnectionEndPointString)
 {
     this.blobConnectionEndPointString = blobConnectionEndPointString;
     CloudStorageAccount VP2ClientBlobStorageAccount = CloudStorageAccount.Parse(blobConnectionEndPointString);
     VP2CloudBlobClient = VP2ClientBlobStorageAccount.CreateCloudBlobClient();
     VP2CloudTableClient = VP2ClientBlobStorageAccount.CreateCloudTableClient();
 }
        public virtual Uri UploadFile(
            string storageName,
            Uri blobEndpointUri,
            string storageKey,
            string filePath,
            BlobRequestOptions blobRequestOptions)
        {
            StorageCredentials credentials = new StorageCredentials(storageName, storageKey);
            CloudBlobClient client = new CloudBlobClient(blobEndpointUri, credentials);
            string blobName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}_{1}",
                DateTime.UtcNow.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
                Path.GetFileName(filePath));

            CloudBlobContainer container = client.GetContainerReference(ContainerName);
            container.CreateIfNotExists();
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            BlobRequestOptions uploadRequestOption = blobRequestOptions ?? new BlobRequestOptions();

            if (!uploadRequestOption.ServerTimeout.HasValue)
            {
                uploadRequestOption.ServerTimeout = TimeSpan.FromMinutes(300);
            }

            using (FileStream readStream = File.OpenRead(filePath))
            {
                blob.UploadFromStream(readStream, AccessCondition.GenerateEmptyCondition(), uploadRequestOption);
            }

            return new Uri(string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", client.BaseUri, ContainerName, client.DefaultDelimiter, blobName));
        }
        public Task DownloadBlob(
            Uri uri,
            string localFile,
            FileEncryption fileEncryption,
            ulong initializationVector,
            CloudBlobClient client,
            CancellationToken cancellationToken,
            IRetryPolicy retryPolicy,
            Func<string> getSharedAccessSignature = null,
            long start = 0,
            long length = -1,
            int parallelTransferThreadCount = 10,
            int numberOfConcurrentTransfers = 2)
        {
            if (client != null && getSharedAccessSignature != null)
            {
                throw new InvalidOperationException("The arguments client and getSharedAccessSignature cannot both be non-null");
            }

            SetConnectionLimits(uri, Environment.ProcessorCount * numberOfConcurrentTransfers * parallelTransferThreadCount);

            Task task =
                Task.Factory.StartNew(
                    () =>
                        DownloadFileFromBlob(uri, localFile, fileEncryption, initializationVector, client,
                            cancellationToken, retryPolicy, getSharedAccessSignature, start: start, length: length,
                            parallelTransferThreadCount: parallelTransferThreadCount));
            return task;
        }
 public Task UploadBlob(
     Uri url,
     string localFile,
     FileEncryption fileEncryption,
     CancellationToken cancellationToken,
     CloudBlobClient client,
     IRetryPolicy retryPolicy,
     string contentType = null,
     string subDirectory = "",
     Func<string> getSharedAccessSignature = null)
 {
     SetConnectionLimits(url);
     return Task.Factory.StartNew(
         () => UploadFileToBlob(
             cancellationToken,
             url,
             localFile,
             contentType,
             subDirectory,
             fileEncryption,
             client,
             retryPolicy,
             getSharedAccessSignature),
         cancellationToken);
 }
Пример #7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="blobClient">use BlobConn to get blobClient</param>
 /// <param name="clientContainerName">This should be the ID of the User</param>
 /// <returns></returns>
 public Blob(CloudBlobClient blobClient, string clientContainerName)
 {
     //Get a reference to a container and create container for first time use user
     container = blobClient.GetContainerReference(clientContainerName);
     container.CreateIfNotExists();
     indicator = "OK";
 }
Пример #8
0
        public void TestInitialize()
        {
            this.blobClient = GenerateCloudBlobClient();

            // Create and log a new prefix for this test.
            this.prefix = Guid.NewGuid().ToString("N");
        }
Пример #9
0
        public BlobContainer(string name)
        {
            // hämta connectionsträngen från config // RoleEnviroment bestämmer settingvalue runtime
            //var connectionString = RoleEnvironment.GetConfigurationSettingValue("PhotoAppStorage");
            //var connectionString = CloudConfigurationManager.GetSetting("CloudStorageApp");
            // hämtar kontot utfrån connectionsträngens värde
            //var account = CloudStorageAccount.Parse(connectionString);

            //var account = CloudStorageAccount.DevelopmentStorageAccount;

            var cred = new StorageCredentials("jholm",
                "/bVipQ2JxjWwYrZQfHmzhaBx1p1s8BoD/wX6VWOmg4/gpVo/aALrjsDUKqzXsFtc9utepPqe65NposrXt9YsyA==");
            var account = new CloudStorageAccount(cred, true);

            // skapar en blobclient
            _client = account.CreateCloudBlobClient();

            m_BlobContainer = _client.GetContainerReference(name);

            // Om det inte finns någon container med det namnet
            if (!m_BlobContainer.Exists())
            {
                // Skapa containern
                m_BlobContainer.Create();
                var permissions = new BlobContainerPermissions()
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                // Sätter public access till blobs
                m_BlobContainer.SetPermissions(permissions);
            }
        }
Пример #10
0
        /// <summary>
        /// Occurs when a storage provider operation has completed.
        /// </summary>
        //public event EventHandler<StorageProviderEventArgs> StorageProviderOperationCompleted;

        #endregion

        // Initialiser method
        private void Initialise(string storageAccount, string containerName)
        {
            if (String.IsNullOrEmpty(containerName))
                throw new ArgumentException("You must provide the base Container Name", "containerName");
            
            ContainerName = containerName;

            _account = CloudStorageAccount.Parse(storageAccount);
            _blobClient = _account.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference(ContainerName);
            try
            {
                _container.FetchAttributes();
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information");
                _container.Create();

                // set new container's permissions
                // Create a permission policy to set the public access setting for the container. 
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // The public access setting explicitly specifies that the container is private,
                // so that it can't be accessed anonymously.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

                //Set the permission policy on the container.
                _container.SetPermissions(containerPermissions);
            }
        }
Пример #11
0
        public AzureBlobClient()
        {
            var storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            _blobClient = storageAccount.CreateCloudBlobClient();
        }
 public AzureImageUploader()
 {
     var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
       _blobClient = storageAccount.CreateCloudBlobClient();
       _container = FindOrCreateContainer(ContainerName);
       _filecontainer = FindOrCreateContainer(FileContainerName);
 }
Пример #13
0
 public static CloudBlobClient GenerateCloudBlobClient()
 {
     Uri baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
     CloudBlobClient client = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
     client.AuthenticationScheme = DefaultAuthenticationScheme;
     return client;
 }
        protected EndToEndTestFixture(string rootPath)
        {
            string connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            _queueClient = storageAccount.CreateCloudQueueClient();
            _blobClient = storageAccount.CreateCloudBlobClient();

            CreateTestStorageEntities();
            TraceWriter = new TestTraceWriter(TraceLevel.Verbose);

            ScriptHostConfiguration config = new ScriptHostConfiguration()
            {
                RootScriptPath = rootPath,
                TraceWriter = TraceWriter,
                FileLoggingEnabled = true
            };

            HostManager = new ScriptHostManager(config);

            Thread t = new Thread(_ =>
            {
                HostManager.RunAndBlock();
            });
            t.Start();

            TestHelpers.Await(() => HostManager.IsRunning).Wait();
        }
        /// <summary>
        /// Upload the generated receipt Pdf to Blob storage.
        /// </summary>
        /// <param name="file">Byte array containig the Pdf file contents to be uploaded.</param>
        /// <param name="fileName">The desired filename of the uploaded file.</param>
        /// <returns></returns>
        public string UploadPdfToBlob(byte[] file, string fileName)
        {
            // Create the blob client.
            blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            blobContainer = blobClient.GetContainerReference(receiptBlobName);

            // Create the container if it doesn't already exist.
            blobContainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);

            string fileUri = string.Empty;

            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);

            using (var stream = new MemoryStream(file))
            {
                // Upload the in-memory Pdf file to blob storage.
                blockBlob.UploadFromStream(stream);
            }

            fileUri = blockBlob.Uri.ToString();

            return fileUri;
        }
Пример #16
0
        /// <summary>
        /// Create an AzureDirectory
        /// </summary>
        /// <param name="storageAccount">storage account to use</param>
        /// <param name="containerName">name of container (folder in blob storage)</param>
        /// <param name="cacheDirectory">local Directory object to use for local cache</param>
        /// <param name="rootFolder">path of the root folder inside the container</param>
        public AzureDirectory(
            CloudStorageAccount storageAccount,
            string containerName = null,
            Lucene.Net.Store.Directory cacheDirectory = null,
            bool compressBlobs = false,
            string rootFolder = null)
        {
            if (storageAccount == null)
                throw new ArgumentNullException("storageAccount");

            if (string.IsNullOrEmpty(containerName))
                _containerName = "lucene";
            else
                _containerName = containerName.ToLower();

            if (string.IsNullOrEmpty(rootFolder))
                _rootFolder = string.Empty;
            else
            {
                rootFolder = rootFolder.Trim('/');
                _rootFolder = rootFolder + "/";
            }

            _blobClient = storageAccount.CreateCloudBlobClient();
            _initCacheDirectory(cacheDirectory);
            this.CompressBlobs = compressBlobs;
        }
Пример #17
0
        public BlobStorage(string connectionString)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            this._blobClient = storageAccount.CreateCloudBlobClient();

            
        }
Пример #18
0
        public async Task LocationModeWithMissingUriAsync()
        {
            AssertSecondaryEndpoint();

            CloudBlobClient client = GenerateCloudBlobClient();
            CloudBlobClient primaryOnlyClient = new CloudBlobClient(client.BaseUri, client.Credentials);
            CloudBlobContainer container = primaryOnlyClient.GetContainerReference("nonexistingcontainer");

            BlobRequestOptions options = new BlobRequestOptions()
            {
                LocationMode = LocationMode.SecondaryOnly,
                RetryPolicy = new NoRetry(),
            };

            Exception e = await TestHelper.ExpectedExceptionAsync<Exception>(
                async () => await container.FetchAttributesAsync(null, options, null),
                "Request should fail when an URI is not provided for the target location");
            Assert.IsInstanceOfType(e.InnerException, typeof(InvalidOperationException));

            options.LocationMode = LocationMode.SecondaryThenPrimary;
            e = await TestHelper.ExpectedExceptionAsync<Exception>(
                async () => await container.FetchAttributesAsync(null, options, null),
                "Request should fail when an URI is not provided for the target location");
            Assert.IsInstanceOfType(e.InnerException, typeof(InvalidOperationException));

            options.LocationMode = LocationMode.PrimaryThenSecondary;
            e = await TestHelper.ExpectedExceptionAsync<Exception>(
                async () => await container.FetchAttributesAsync(null, options, null),
                "Request should fail when an URI is not provided for the target location");
            Assert.IsInstanceOfType(e.InnerException, typeof(InvalidOperationException));
        }
Пример #19
0
        public BlobFileProvider(IEnumerable<string> locations)
            : base()
        {
            _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
            _blobClient = _storageAccount.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference("data");
            _container.CreateIfNotExists();

            _strings = new List<string>();
            foreach(string location in locations) {
                foreach (IListBlobItem item in _container.ListBlobs(location,true))
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        CloudBlockBlob blob = (CloudBlockBlob)item;
                        string text;
                        using (var memoryStream = new MemoryStream())
                        {
                            blob.DownloadToStream(memoryStream);
                            text = Encoding.UTF8.GetString(memoryStream.ToArray());
                            if (text[0] == _byteOrderMarkUtf8[0])
                            {
                               text= text.Remove(0,_byteOrderMarkUtf8.Length);
                            }
                            _strings.Add(text);
                        }
                    }
                }
            }
        }
Пример #20
0
        public BlobManager(){
            try
            {
                storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"));

                blobClient = storageAccount.CreateCloudBlobClient();
                container = blobClient.GetContainerReference("supstorage");
                container.CreateIfNotExists();
            }
            catch (ArgumentNullException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception null ou vide");
                // Use Application Local Storage Account String
            }
            catch (NullReferenceException)
            {
                Trace.TraceInformation("CloudBlobClient Or CloudBlobContainer Exception");
                // Create Container 
            }
            catch (FormatException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception Connection String Invalid");
            }
            catch (ArgumentException)
            {
                Trace.TraceInformation("CloudStorageAccount Exception connectionString ne peut pas être analysée");
            }
        }
Пример #21
0
 public BlobStorage()
 {
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ReadConfig.CommonCloudCoreApplicationSettings.Storage.StorageConnectionString);
     blobClient = storageAccount.CreateCloudBlobClient();
     blobClient.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(10);
  //   blobClient.ServerTimeout = TimeSpan.FromMinutes(10);
 }
 public Task UploadBlob(
     Uri url,
     string localFile,
     FileEncryption fileEncryption,
     CancellationToken cancellationToken,
     CloudBlobClient client,
     IRetryPolicy retryPolicy,
     string contentType = null,
     string subDirectory = "",
     Func<string> getSharedAccessSignature = null,
     int parallelTransferThreadCount = 10,
     int numberOfConcurrentTransfers = default(int))
 {
     SetConnectionLimits(url, numberOfConcurrentTransfers);
     return Task.Factory.StartNew(
         () => UploadFileToBlob(
             cancellationToken,
             url,
             localFile,
             contentType,
             subDirectory,
             fileEncryption,
             client,
             retryPolicy,
             getSharedAccessSignature,
             parallelTransferThreadCount),
             cancellationToken);
 }
Пример #23
0
 public AzureService(string account, string azureKey, string blobUri)
 {
     _account = account;
     var storageAccount = new CloudStorageAccount(new StorageCredentials(account, azureKey), new Uri(blobUri),
                                                  null, null);
     _client = storageAccount.CreateCloudBlobClient();
 }
Пример #24
0
 public BlobManager(string conStr)
 {
     //RoleEnvironment.GetConfigurationSettingValue("UploadCon")
     Storage = CloudStorageAccount.Parse(conStr);
     BlobClient = Storage.CreateCloudBlobClient();
     QueueClient = Storage.CreateCloudQueueClient();
 }
Пример #25
0
 private void Init()
 {
     var connectionString = ConfigurationManager.ConnectionStrings["BlobStorage"].ConnectionString;
     var storageAccount = CloudStorageAccount.Parse(connectionString);
     _blobClient = storageAccount.CreateCloudBlobClient();
     _blobContainer = _blobClient.GetContainerReference(ConfigurationManager.AppSettings["PhotoContainer"]);
 }
        public static void Run(string connectionString, bool disableLogging)
        {
            _connectionString = connectionString;
            _storageAccount = CloudStorageAccount.Parse(connectionString);
            _blobClient = _storageAccount.CreateCloudBlobClient();

            Console.WriteLine("Creating the test blob...");
            CreateTestBlob();

            try
            {
                TimeSpan azureSDKTime = RunAzureSDKTest();
                TimeSpan webJobsSDKTime = RunWebJobsSDKTest(disableLogging);

                // Convert to ulong because the measurment block does not support other data type
                ulong perfRatio = (ulong)((webJobsSDKTime.TotalMilliseconds / azureSDKTime.TotalMilliseconds) * 100);

                Console.WriteLine("--- Results ---");
                Console.WriteLine("Azure SDK:   {0} ms: ", azureSDKTime.TotalMilliseconds);
                Console.WriteLine("WebJobs SDK: {0} ms: ", webJobsSDKTime.TotalMilliseconds);

                Console.WriteLine("Perf ratio (x100, long): {0}", perfRatio);

                MeasurementBlock.Mark(
                    perfRatio,
                    (disableLogging ? BlobNoLoggingOverheadMetric : BlobLoggingOverheadMetric) + ";Ratio;Percent");
            }
            finally
            {
                Cleanup();
            }
        }
        internal static BlobRequestOptions ApplyDefaults(BlobRequestOptions options, BlobType blobType, CloudBlobClient serviceClient, bool applyExpiry = true)
        {
            BlobRequestOptions modifiedOptions = new BlobRequestOptions(options);

            modifiedOptions.RetryPolicy = modifiedOptions.RetryPolicy ?? serviceClient.RetryPolicy;
            modifiedOptions.ServerTimeout = modifiedOptions.ServerTimeout ?? serviceClient.ServerTimeout;
            modifiedOptions.MaximumExecutionTime = modifiedOptions.MaximumExecutionTime ?? serviceClient.MaximumExecutionTime;

            if (applyExpiry && !modifiedOptions.OperationExpiryTime.HasValue && modifiedOptions.MaximumExecutionTime.HasValue)
            {
                modifiedOptions.OperationExpiryTime = DateTime.Now + modifiedOptions.MaximumExecutionTime.Value;
            }

#if WINDOWS_PHONE
            modifiedOptions.DisableContentMD5Validation = true;
            modifiedOptions.StoreBlobContentMD5 = false;
            modifiedOptions.UseTransactionalMD5 = false;
#else
            modifiedOptions.DisableContentMD5Validation = modifiedOptions.DisableContentMD5Validation ?? false;
            modifiedOptions.StoreBlobContentMD5 = modifiedOptions.StoreBlobContentMD5 ?? (blobType == BlobType.BlockBlob);
            modifiedOptions.UseTransactionalMD5 = modifiedOptions.UseTransactionalMD5 ?? false;
#endif

            return modifiedOptions;
        }
        public StorageHelper()
        {
            _adminuser = "******";
            _standarduser = "******";

            try
            {
                var connstring = ConfigurationManager.AppSettings["AzureStorage"];
                var acct = CloudStorageAccount.Parse(connstring);
                client = acct.CreateCloudBlobClient();
                var dir = client.GetContainerReference("studentconnect");
               var adminpwd = dir.GetBlobReferenceFromServer("_adminpassword");
                // If you get a 403 - Forbidden warning here, its because you dont have access to SD's Azure Storage account.
                // Get your own FREE here.  http://www.windowsazure.com/en-us/pricing/free-trial/
                var adminpwdText = adminpwd.DownloadText();
                var schoolsRef = dir.GetBlobReferenceFromServer("_schools");
                var schoolsText = schoolsRef.DownloadText();

                _adminpassword = adminpwdText;
                _schools = this.ParseSchoolText(schoolsText);
            }
            finally
            {

            }
        }
Пример #29
0
        public HomeController()
        {
            storageAccount = CloudStorageAccount.Parse(
            ConfigurationManager.AppSettings["StorageConnectionString"]);

            tableClient = storageAccount.CreateCloudTableClient();

            table = tableClient.GetTableReference("fouramigos");

            table.CreateIfNotExists();

            blobClient = storageAccount.CreateCloudBlobClient();

            container = blobClient.GetContainerReference("fouramigos");

            container.CreateIfNotExists();

            BlobContainerPermissions permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);


            //lägga till nya
            //var tablemodels = new TableModel("Brutus", "Uggla") { Location = "T4", Description="Uggla i träd", Type="Foto" };
            //var tablemodels1 = new TableModel("brutus", "Örn") { Location = "T4", Description="Örn som flyger", Type = "Foto" };

            //var opreation = TableOperation.Insert(tablemodels);
            //var operation2 = TableOperation.Insert(tablemodels1);

            //table.Execute(opreation);
            //table.Execute(operation2);
        }
 public UsersForAdminCsvPublishService(CloudBlobClient blobClient,
     IAdminUserService adminUserService,
     IMapper mapper) : base(blobClient, new UserForAdminCsvFormatter())
 {
     _adminUserService = adminUserService;
     _mapper = mapper;
 }
Пример #31
0
 /// <summary>
 /// Constructor for a store backed by Azure
 /// </summary>
 /// <param name="blobStorage">The storage account that backs this store</param>
 /// <param name="enableSnapshots">Whether any save on this store should create snapshots</param>
 /// <param name="deletedKey">The metadata key to check if a store item is soft deleted</param>
 /// <param name="containerPrefix">Use this to namespace your containers if required</param>
 public AzureStore(CloudBlobClient blobStorage, bool enableSnapshots, string deletedKey = null, string containerPrefix = null)
 {
     _blobStorage = blobStorage;
     _enableSnapshots = enableSnapshots;
     _deletedKey = deletedKey ?? DefaultDeletedKey;
     _containerPrefix = containerPrefix;
 }
        internal void UploadFromString(string accountName, string accountKey, string containerName, string fileName, string sourceFileName, string fileContentType, string content)   //, string name, string fileDescription) {
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists();
            string ext = System.IO.Path.GetExtension(sourceFileName);

            //string fileName = String.Format(

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.Properties.ContentType = fileContentType;
            //blockBlob.Metadata.Add("name", name);
            blockBlob.Metadata.Add("originalfilename", sourceFileName);
            //blockBlob.Metadata.Add("userid", userId.ToString());
            //blockBlob.Metadata.Add("ownerid", userId.ToString());
            DateTime created = DateTime.UtcNow;

            // https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
            // http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format
            //blockBlob.Metadata.Add("username", userName);
            blockBlob.Metadata.Add("created", created.ToString("yyyy-MM-ddTHH:mm:ss"));  // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("modified", created.ToString("yyyy-MM-ddTHH:mm:ss")); // "yyyy-MM-ddTHH:mm:ssZ"
            blockBlob.Metadata.Add("fileext", ext);

            blockBlob.UploadText(content, Encoding.UTF8); // .UploadFromStream(fileInputStream);

            blockBlob.SetMetadata();
        }
Пример #33
0
        protected void Page_Load(object sender, EventArgs e)
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("arailproject", "zSU2JysKO4g0NmCmVSu3vsFUGnvcm8d4WsDhkNgQVVtPCyXfQprTQBWfzh72zu89Hq8vKT6v6XUZpotPTL39CQ=="), true);
            string projectattachments          = ConfigurationManager.AppSettings["StorageConnectionString"];

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

            container.CreateIfNotExists();
        }
Пример #34
0
        public string DownloadPublicBlob(string UserId, string BlobName, bool PublicAccess = true)
        {
            //Retrieve a reference to a container.
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

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


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }



            // Retrieve reference to a blob named

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName);

            //var sasToken = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            //{
            //    Permissions = SharedAccessBlobPermissions.Read,
            //    SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
            //}, new SharedAccessBlobHeaders()
            //{
            //    ContentDisposition = "attachment; filename=file-name"
            //});

            //return string.Format("{0}{1}", blockBlob.Uri, sasToken);

            return(blockBlob.Uri.ToString());
        }
Пример #35
0
        private void LogError(Exception ex, string message)
        {
            //ConfigurationManager.ConnectionStrings["CineStorageConStr"].ConnectionString
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

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

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob logBlob = container.GetBlockBlobReference(String.Format(this.LogFile, DateTime.UtcNow.ToString("dd MMM yyyy")));
            try
            {
                using (StreamReader sr = new StreamReader(logBlob.OpenRead()))
                {
                    using (StreamWriter sw = new StreamWriter(logBlob.OpenWrite()))
                    {
                        sw.Write(sr.ReadToEnd());

                        if (ex != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(ex.Message);
                            sw.WriteLine(ex.StackTrace);

                            sw.Write(System.Environment.NewLine);

                            if (ex.InnerException != null)
                            {
                                sw.Write(System.Environment.NewLine);

                                sw.WriteLine(ex.InnerException.Message);
                                sw.WriteLine(ex.InnerException.StackTrace);

                                sw.Write(System.Environment.NewLine);
                            }
                        }

                        if (message != null)
                        {
                            sw.Write(System.Environment.NewLine);

                            sw.WriteLine(message);

                            sw.Write(System.Environment.NewLine);
                        }
                    }
                }
            }
            catch { }
        }
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient GetBlobClient()
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                StorageConnectionString);

            ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint);

            tableServicePoint.UseNagleAlgorithm = false;

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

            return(blobClient);
        }
Пример #37
0
        private CloudBlobContainer GetContainer()
        {
            var client = new Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient(
                new Uri(ConfigurationManager.AppSettings[@"blobUrl"]),
                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    ConfigurationManager.AppSettings[@"storageAccount"],
                    ConfigurationManager.AppSettings[@"storageKey"]));

            var c = client.GetContainerReference(@"images");

            c.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
            return(c);
        }
Пример #38
0
        public static Tuple <bool, List <string> > listHubs(string account, string accountKey, string orgID, string studyID)
        {
            try
            {
                CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(account, accountKey), true);
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient       = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer storageContainer = blobClient.GetContainerReference(AzureConfigContainerName);

                List <String> hubList = lsDirectory(storageContainer.ListBlobs(), "/" + orgID + "/" + studyID + "/");

                return(new Tuple <bool, List <string> >(true, hubList));
            }
            catch (Exception e)
            {
                return(new Tuple <bool, List <string> >(false, new List <string>()
                {
                    e.Message
                }));
            }
        }
        public string GetTempUrl(string accountName, string accountKey, string fullPath)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy)).ToString();

            return(resultUrl);
        }
Пример #40
0
        private DateTime GetLastModified()
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

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

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            // Retrieve reference to a blob.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasUKBlob = container.GetBlockBlobReference(CinemasUKFileName);
            if (cinemasUKBlob.Exists())
            {
                DateTimeOffset?dto = cinemasUKBlob.Properties.LastModified;
                if (dto.HasValue)
                {
                    return(dto.Value.DateTime);
                }
            }

            return(DateTime.MinValue);
        }
Пример #41
0
        //Retrieve Blob Container
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer GetCloudBlobContainer(string ContainerName, Boolean PublicAccess = false)
        {
            // Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);

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


            //Set permission to public
            if (PublicAccess)
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
            }
            else
            {
                container.SetPermissions(
                    new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
                {
                    PublicAccess =
                        Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off
                });
            }

            return(container);
        }
Пример #42
0
        public string DownloadSharedBlob(string UserId, string BlobName)
        {
            //Retrieve storage account from connection string.
            Microsoft.WindowsAzure.Storage.CloudStorageAccount StorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Convert.ToString(ConfigurationManager.AppSettings["StorageConnectionString"]));

            // Create the blob client.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference(UserId);

            var blob     = container.GetBlockBlobReference(BlobName);
            var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
            {
                Permissions            = SharedAccessBlobPermissions.Read,
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(15),
            }, new SharedAccessBlobHeaders()
            {
                ContentDisposition = "attachment; filename=\"somefile.pdf\"",
            });
            var downloadUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);

            return(downloadUrl);
        }
        public string GetTempDownloadUrl(string accountName, string accountKey, string fullPath)
        {
            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount =
                Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                    string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1};BlobEndpoint=https://{0}.blob.core.windows.net/", accountName, accountKey)
                    );
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            var blob = blobClient.GetBlobReferenceFromServer(new Uri(fullPath));

            var readPolicy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy()
            {
                Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Read, // SharedAccessPermissions.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(10)
            };

            string resultUrl = new Uri(blob.Uri.AbsoluteUri + blob.GetSharedAccessSignature(readPolicy,
                                                                                            new SharedAccessBlobHeaders {
                ContentDisposition = blob.Metadata.ContainsKey("originalfilename") ? "attachment; filename=" + blob.Metadata["originalfilename"] : "attachment; filename=FileUnknown",
                ContentType        = blob.Properties.ContentType
            }
                                                                                            )).ToString();

            return(resultUrl);
        }
Пример #44
0
        public async Task CloudBlobDirectoryFlatListingAsync()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    await container.CreateAsync();

                    if (await CloudBlobDirectorySetupWithDelimiterAsync(container, delimiter))
                    {
                        BlobResultSegment segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null);

                        List <IListBlobItem> simpleList1 = new List <IListBlobItem>();
                        simpleList1.AddRange(segment.Results);
                        while (segment.ContinuationToken != null)
                        {
                            segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, segment.ContinuationToken, null, null);

                            simpleList1.AddRange(segment.Results);
                        }

                        Assert.IsTrue(simpleList1.Count == 3);
                        IListBlobItem item11 = simpleList1.ElementAt(0);
                        Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                        IListBlobItem item12 = simpleList1.ElementAt(1);
                        Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                        IListBlobItem item13 = simpleList1.ElementAt(2);
                        Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));
                        CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13;

                        BlobResultSegment segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null, null, null);

                        List <IListBlobItem> simpleList2 = new List <IListBlobItem>();
                        simpleList2.AddRange(segment2.Results);
                        while (segment2.ContinuationToken != null)
                        {
                            segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, segment2.ContinuationToken, null, null);

                            simpleList2.AddRange(segment2.Results);
                        }

                        Assert.IsTrue(simpleList2.Count == 2);

                        IListBlobItem item21 = simpleList2.ElementAt(0);
                        Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                        IListBlobItem item22 = simpleList2.ElementAt(1);
                        Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2"));

                        BlobResultSegment segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null);

                        List <IListBlobItem> simpleList3 = new List <IListBlobItem>();
                        simpleList3.AddRange(segment3.Results);
                        while (segment3.ContinuationToken != null)
                        {
                            segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, segment3.ContinuationToken, null, null);

                            simpleList3.AddRange(segment3.Results);
                        }
                        Assert.IsTrue(simpleList3.Count == 2);

                        IListBlobItem item31 = simpleList3.ElementAt(0);
                        Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter));

                        IListBlobItem item32 = simpleList3.ElementAt(1);
                        Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter));

                        BlobResultSegment segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, null, null, null);

                        List <IListBlobItem> simpleList4 = new List <IListBlobItem>();
                        simpleList4.AddRange(segment4.Results);
                        while (segment4.ContinuationToken != null)
                        {
                            segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, segment4.ContinuationToken, null, null);

                            simpleList4.AddRange(segment4.Results);
                        }

                        Assert.IsTrue(simpleList4.Count == 2);

                        IListBlobItem item41 = simpleList4.ElementAt(0);
                        Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                        IListBlobItem item42 = simpleList4.ElementAt(1);
                        Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2"));
                    }
                }
                finally
                {
                    container.DeleteIfExistsAsync().AsTask().Wait();
                }
            }
        }
Пример #45
0
 /// <summary>
 /// init cloud blob util
 /// </summary>
 /// <param name="account">storage account</param>
 public CloudBlobUtil(CloudStorageAccount account)
 {
     this.account = account;
     client       = account.CreateCloudBlobClient();
     random       = new Random();
 }
Пример #46
0
        public async Task CloudBlobContainerGetBlobReferenceFromServerAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
                {
                    Permissions            = SharedAccessBlobPermissions.Read,
                    SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                };

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb");
                await blockBlob.PutBlockListAsync(new List <string>());

                CloudPageBlob pageBlob = container.GetPageBlobReference("pb");
                await pageBlob.CreateAsync(0);

                CloudAppendBlob appendBlob = container.GetAppendBlobReference("ab");
                await appendBlob.CreateOrReplaceAsync();

                CloudBlobClient client;
                ICloudBlob      blob;

                blob = await container.GetBlobReferenceFromServerAsync("bb");

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                CloudBlockBlob blockBlobSnapshot = await((CloudBlockBlob)blob).CreateSnapshotAsync();
                await blob.SetPropertiesAsync();

                Uri blockBlobSnapshotUri = new Uri(blockBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + blockBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSnapshotUri);

                AssertAreEqual(blockBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.GetBlobReferenceFromServerAsync("pb");

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                CloudPageBlob pageBlobSnapshot = await((CloudPageBlob)blob).CreateSnapshotAsync();
                await blob.SetPropertiesAsync();

                Uri pageBlobSnapshotUri = new Uri(pageBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + pageBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSnapshotUri);

                AssertAreEqual(pageBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.GetBlobReferenceFromServerAsync("ab");

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                CloudAppendBlob appendBlobSnapshot = await((CloudAppendBlob)blob).CreateSnapshotAsync();
                await blob.SetPropertiesAsync();

                Uri appendBlobSnapshotUri = new Uri(appendBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + appendBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o"));
                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSnapshotUri);

                AssertAreEqual(appendBlobSnapshot.Properties, blob.Properties);
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlobSnapshot.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.Uri);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.Uri);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.Uri);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.StorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.StorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.StorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                string             blockBlobToken = blockBlob.GetSharedAccessSignature(policy);
                StorageCredentials blockBlobSAS   = new StorageCredentials(blockBlobToken);
                Uri        blockBlobSASUri        = blockBlobSAS.TransformUri(blockBlob.Uri);
                StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri);

                string             appendBlobToken = appendBlob.GetSharedAccessSignature(policy);
                StorageCredentials appendBlobSAS   = new StorageCredentials(appendBlobToken);
                Uri        appendBlobSASUri        = appendBlobSAS.TransformUri(appendBlob.Uri);
                StorageUri appendBlobSASStorageUri = appendBlobSAS.TransformUri(appendBlob.StorageUri);

                string             pageBlobToken = pageBlob.GetSharedAccessSignature(policy);
                StorageCredentials pageBlobSAS   = new StorageCredentials(pageBlobToken);
                Uri        pageBlobSASUri        = pageBlobSAS.TransformUri(pageBlob.Uri);
                StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(blockBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(pageBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.BaseUri, appendBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(appendBlobSASUri);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri));
                Assert.IsNull(blob.StorageUri.SecondaryUri);

                client = new CloudBlobClient(container.ServiceClient.StorageUri, blockBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob));
                Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.StorageUri, pageBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudPageBlob));
                Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri));

                client = new CloudBlobClient(container.ServiceClient.StorageUri, appendBlobSAS);
                blob   = await client.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null);

                Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob));
                Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri));
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
Пример #47
0
        public async Task CloudBlobClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync()
        {
            CloudBlobClient    blobClient = GenerateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer = BlobTestBase.GetRandomBuffer(1024 * 1024);

            try
            {
                await container.CreateAsync();

                blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                CloudPageBlob  pageBlob  = container.GetPageBlobReference("blob2");
                blockBlob.StreamWriteSizeInBytes       = 1024 * 1024;
                blockBlob.StreamMinimumReadSizeInBytes = 1024 * 1024;
                pageBlob.StreamWriteSizeInBytes        = 1024 * 1024;
                pageBlob.StreamMinimumReadSizeInBytes  = 1024 * 1024;

                using (var bos = await blockBlob.OpenWriteAsync())
                {
                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await bos.WriteAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await bos.WriteAsync(buffer, 0, buffer.Length);

                    await bos.CommitAsync();
                }

                using (Stream bis = (await blockBlob.OpenReadAsync()))
                {
                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await bis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await bis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }

                using (var bos = await pageBlob.OpenWriteAsync(8 * 1024 * 1024))
                {
                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await bos.WriteAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await bos.WriteAsync(buffer, 0, buffer.Length);

                    await bos.CommitAsync();
                }

                using (Stream bis = (await pageBlob.OpenReadAsync()))
                {
                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await bis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await bis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }

            finally
            {
                blobClient.DefaultRequestOptions.MaximumExecutionTime = null;
                container.DeleteIfExistsAsync().Wait();
            }
        }
Пример #48
0
        public void CloudBlobClientListBlobsSegmentedWithEmptyPrefix()
        {
            string             name          = "bb" + GetRandomContainerName();
            CloudBlobClient    blobClient    = GenerateCloudBlobClient();
            CloudBlobContainer rootContainer = blobClient.GetRootContainerReference();
            CloudBlobContainer container     = blobClient.GetContainerReference(name);

            try
            {
                rootContainer.CreateIfNotExists();
                container.Create();
                List <Uri> preExistingBlobs = rootContainer.ListBlobs().Select(b => b.Uri).ToList();

                List <string> blobNames     = CreateBlobs(container, 3, BlobType.BlockBlob);
                List <string> rootBlobNames = CreateBlobs(rootContainer, 2, BlobType.BlockBlob);

                BlobResultSegment     results;
                BlobContinuationToken token       = null;
                List <Uri>            listedBlobs = new List <Uri>();
                do
                {
                    results = blobClient.ListBlobsSegmented("", token);
                    token   = results.ContinuationToken;

                    foreach (IListBlobItem blob in results.Results)
                    {
                        if (preExistingBlobs.Contains(blob.Uri))
                        {
                            continue;
                        }
                        else
                        {
                            if (blob is CloudPageBlob)
                            {
                                ((CloudPageBlob)blob).Delete();
                            }
                            else
                            {
                                ((CloudBlockBlob)blob).Delete();
                            }

                            listedBlobs.Add(blob.Uri);
                        }
                    }
                }while (token != null);

                Assert.AreEqual(2, listedBlobs.Count);
                do
                {
                    results = container.ListBlobsSegmented("", false, BlobListingDetails.None, null, token, null, null);
                    token   = results.ContinuationToken;

                    foreach (IListBlobItem blob in results.Results)
                    {
                        if (preExistingBlobs.Contains(blob.Uri))
                        {
                            continue;
                        }
                        else
                        {
                            if (blob is CloudPageBlob)
                            {
                                ((CloudPageBlob)blob).Delete();
                            }
                            else
                            {
                                ((CloudBlockBlob)blob).Delete();
                            }

                            listedBlobs.Add(blob.Uri);
                        }
                    }
                }while (token != null);

                Assert.AreEqual(5, listedBlobs.Count);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Пример #49
0
        public void CloudBlobClientListBlobsSegmentedWithPrefixAPM()
        {
            string             name          = "bb" + GetRandomContainerName();
            CloudBlobClient    blobClient    = GenerateCloudBlobClient();
            CloudBlobContainer rootContainer = blobClient.GetRootContainerReference();
            CloudBlobContainer container     = blobClient.GetContainerReference(name);

            try
            {
                rootContainer.CreateIfNotExists();
                container.Create();

                List <string> blobNames     = CreateBlobs(container, 3, BlobType.BlockBlob);
                List <string> rootBlobNames = CreateBlobs(rootContainer, 2, BlobType.BlockBlob);

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult          result;
                    BlobResultSegment     results;
                    BlobContinuationToken token = null;
                    do
                    {
                        result = blobClient.BeginListBlobsSegmented("bb", token,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                        waitHandle.WaitOne();
                        results = blobClient.EndListBlobsSegmented(result);
                        token   = results.ContinuationToken;

                        foreach (CloudBlockBlob blob in results.Results)
                        {
                            blob.Delete();
                            rootBlobNames.Remove(blob.Name);
                        }
                    }while (token != null);
                    Assert.AreEqual(0, rootBlobNames.Count);

                    result = blobClient.BeginListBlobsSegmented("bb", token,
                                                                ar => waitHandle.Set(),
                                                                null);
                    waitHandle.WaitOne();
                    results = blobClient.EndListBlobsSegmented(result);
                    Assert.AreEqual(0, results.Results.Count());
                    Assert.IsNull(results.ContinuationToken);

                    result = blobClient.BeginListBlobsSegmented(name, token,
                                                                ar => waitHandle.Set(),
                                                                null);
                    waitHandle.WaitOne();
                    results = blobClient.EndListBlobsSegmented(result);
                    Assert.AreEqual(0, results.Results.Count());
                    Assert.IsNull(results.ContinuationToken);

                    token = null;
                    do
                    {
                        result = blobClient.BeginListBlobsSegmented(name + "/", token,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                        waitHandle.WaitOne();
                        results = blobClient.EndListBlobsSegmented(result);
                        token   = results.ContinuationToken;

                        foreach (CloudBlockBlob blob in results.Results)
                        {
                            Assert.IsTrue(blobNames.Remove(blob.Name));
                        }
                    }while (token != null);
                    Assert.AreEqual(0, blobNames.Count);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Пример #50
0
 internal CloudPageBlob(BlobAttributes attributes, CloudBlobClient serviceClient)
     : base(attributes, serviceClient)
 {
     throw new System.NotImplementedException();
 }
Пример #51
0
        private void ProcessData()
        {
            //return;

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

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

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            // Retrieve reference to a blob.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasUKBlob = container.GetBlockBlobReference(CinemasUKFileName);
            using (Stream s = cinemasUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(CinemasUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsUKBlob = container.GetBlockBlobReference(FilmsUKFileName);
            using (Stream s = filmsUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(FilmsUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmCinemasUKBlob = container.GetBlockBlobReference(FilmCinemasUKFileName);
            using (Stream s = filmCinemasUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(filmCinemasUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsUKBlob = container.GetBlockBlobReference(CinemaFilmsUKFileName);
            using (Stream s = cinemaFilmsUKBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(cinemaFilmsUK));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemasIEBlob = container.GetBlockBlobReference(CinemasIEFileName);
            using (Stream s = cinemasIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(CinemasIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsIEBlob = container.GetBlockBlobReference(FilmsIEFileName);
            using (Stream s = filmsIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(FilmsIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmCinemasIEBlob = container.GetBlockBlobReference(FilmCinemasIEFileName);
            using (Stream s = filmCinemasIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(filmCinemasIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsIEBlob = container.GetBlockBlobReference(CinemaFilmsIEFileName);
            using (Stream s = cinemaFilmsIEBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(cinemaFilmsIE));
                    }
                }
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob filmsPostersBlob = container.GetBlockBlobReference(MovieFilmPostersFileName);
            using (Stream s = filmsPostersBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        sw.Write(JsonConvert.SerializeObject(MoviePosters));
                    }
                }
            }

            //Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob mediumfilmsPostersBlob = container.GetBlockBlobReference(MediumFilmPostersFileName);
            //using (Stream s = mediumfilmsPostersBlob.OpenWrite())
            //{
            //    using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
            //    {
            //        using (StreamWriter sw = new StreamWriter(gzipStream))
            //        {
            //            sw.Write(JsonConvert.SerializeObject(mediumposters));
            //        }
            //    }
            //}
        }
        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);
            }
        }
Пример #53
0
        // Create a CSV file and save to Blob storage with the Headers required for our Azure Function processing
        // A new request telemetry is created
        // The request is part of the parent request (since)
        public void CreateBlob(string fileName)
        {
            ///////////////////////////////////////////////////
            // Grab existing 
            ///////////////////////////////////////////////////
            Microsoft.ApplicationInsights.TelemetryClient telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();
            telemetryClient.Context.User.AuthenticatedUserId = "*****@*****.**";

            string traceoperation = null;
            string traceparent = null;
            System.Console.WriteLine("telemetryClient.Context.Operation.Id: " + telemetryClient.Context.Operation.Id);
            System.Console.WriteLine("telemetryClient.Context.Session.Id: " + telemetryClient.Context.Session.Id);
            System.Console.WriteLine("telemetryClient.Context.Operation.ParentId: " + telemetryClient.Context.Operation.ParentId);

            Microsoft.ApplicationInsights.DataContracts.RequestTelemetry requestTelemetry = new Microsoft.ApplicationInsights.DataContracts.RequestTelemetry();
            requestTelemetry.Name = "Create Blob: " + fileName;
            //requestTelemetry.Source = requestContext.Replace("appId=",string.Empty);
            requestTelemetry.Timestamp = System.DateTimeOffset.Now;
            requestTelemetry.Context.Operation.Id = traceoperation;
            requestTelemetry.Context.Operation.ParentId = traceparent;
            requestTelemetry.Context.User.AuthenticatedUserId = "*****@*****.**";

            using (var requestBlock = telemetryClient.StartOperation<Microsoft.ApplicationInsights.DataContracts.RequestTelemetry>(requestTelemetry))
            {
                ///////////////////////////////////////////////////
                // Request Telemetry
                ///////////////////////////////////////////////////
                requestBlock.Telemetry.Context.User.AuthenticatedUserId = "*****@*****.**";

                if (!string.IsNullOrWhiteSpace(traceoperation))
                {
                    // Use the existing common operation id
                    requestBlock.Telemetry.Context.Operation.Id = traceoperation;
                    System.Console.WriteLine("[Use existing] traceoperation: " + traceoperation);
                }
                else
                {
                    // Set the traceoperation (we did not know it until now)
                    traceoperation = requestBlock.Telemetry.Context.Operation.Id;
                    System.Console.WriteLine("[Set the] traceoperation = requestBlock.Telemetry.Context.Operation.Id: " + traceoperation);
                }

                if (!string.IsNullOrWhiteSpace(traceparent))
                {
                    // Use the existing traceparent
                    requestBlock.Telemetry.Context.Operation.ParentId = traceparent;
                    System.Console.WriteLine("[Use existing] traceparent: " + traceparent);
                }
                else
                {
                    traceparent = requestBlock.Telemetry.Id;
                    System.Console.WriteLine("[Set the] traceparent = requestBlock.Telemetry.Id: " + traceparent);
                }
                // Store future parent id
                traceparent = requestBlock.Telemetry.Id;
                System.Console.WriteLine("traceparent = requestBlock.Telemetry.Id: " + traceparent);



                ///////////////////////////////////////////////////
                // Create Dependency for future Azure Function processing
                // NOTE: I trick it by giving a Start Time Offset of Now.AddSeconds(1), so it sorts correctly in the Azure Portal UI
                ///////////////////////////////////////////////////
                string operationName = "Dependency: Blob Event";
                // Set the target so it points to the "dependent" app insights account app id
                // string target = "03-disttrace-func-blob | cid-v1:676560d0-81fb-4e5b-bfdd-7da1ad11c866"
                string target = "03-disttrace-func-blob | cid-v1:" + System.Environment.GetEnvironmentVariable("ai_03_disttrace_web_app_appkey");
                string dependencyName = "Dependency Name: Azure Function Blob Trigger";
                Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry dependencyTelemetry =
                   new Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry(
                    operationName, target, dependencyName,
                    "02-disttrace-web-app", System.DateTimeOffset.Now.AddSeconds(1), System.TimeSpan.FromSeconds(2), "200", true);
                dependencyTelemetry.Context.Operation.Id = traceoperation;
                dependencyTelemetry.Context.Operation.ParentId = requestBlock.Telemetry.Id;
                // Store future parent id
                traceparent = dependencyTelemetry.Id;
                System.Console.WriteLine("traceparent = dependencyTelemetry.Id: " + traceparent);
                telemetryClient.TrackDependency(dependencyTelemetry);



                ///////////////////////////////////////////////////
                // Blob code
                ///////////////////////////////////////////////////
                string containerName = "appinsightstest";
                string storageConnectionString = System.Environment.GetEnvironmentVariable("ai_storage_key");
                CloudStorageAccount storageAccount = null;
                System.Console.WriteLine("storageConnectionString: " + storageConnectionString);

                CloudStorageAccount.TryParse(storageConnectionString, out storageAccount);

                System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();

                list.Add("id,date");

                for (int i = 1; i <= 50000; i++)
                {
                    list.Add(i.ToString() + "," + string.Format("{0:MM/dd/yyyy}", System.DateTime.Now));
                }

                var text = string.Join("\n", list.ToArray());

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

                Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

                ///////////////////////////////////////////////////
                // Set the blob's meta data
                // We need the values from the dependency
                ///////////////////////////////////////////////////
                // Request-Context: appId=cid-v1:{The App Id of the current App Insights Account}
                string requestContext = "appId=cid-v1:" + System.Environment.GetEnvironmentVariable("ai_02_disttrace_web_app_appkey");
                System.Console.WriteLine("Blob Metadata -> requestContext: " + requestContext);
                blob.Metadata.Add("RequestContext", requestContext);

                // Request-Id / traceparent: {parent request/operation id} (e.g. the Track Dependency)
                System.Console.WriteLine("Blob Metadata -> RequestId: " + traceparent);
                blob.Metadata.Add("RequestId", traceparent);
                System.Console.WriteLine("Blob Metadata -> traceparent: " + traceparent);
                blob.Metadata.Add("traceparent", traceparent);

                // Traceoperation {common operation id} (e.g. same operation id for all requests in this telemetry pipeline)
                System.Console.WriteLine("Blob Metadata -> traceoperation: " + traceoperation);
                blob.Metadata.Add("traceoperation", traceoperation);


                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text)))
                {
                    blob.UploadFromStreamAsync(memoryStream).Wait();
                }

                requestTelemetry.ResponseCode = "200";
                requestTelemetry.Success = true;
                telemetryClient.StopOperation(requestBlock);
            } // using

            ///////////////////////////////////////////////////
            // For Debugging
            ///////////////////////////////////////////////////
            telemetryClient.Flush();

        } // Create Blob
Пример #54
0
        public static void MyClassCleanup()
        {
            CloudBlobClient client = GenerateCloudBlobClient();

            client.SetServicePropertiesAsync(startProperties).AsTask().Wait();
        }
Пример #55
0
        public async Task CloudBlobClientListBlobsSegmentedWithPrefixAsync()
        {
            string             name          = "bb" + GetRandomContainerName();
            CloudBlobClient    blobClient    = GenerateCloudBlobClient();
            CloudBlobContainer rootContainer = blobClient.GetRootContainerReference();
            CloudBlobContainer container     = blobClient.GetContainerReference(name);

            try
            {
                await rootContainer.CreateIfNotExistsAsync();

                await container.CreateAsync();

                List <string> blobNames = await CreateBlobsAsync(container, 3, BlobType.BlockBlob);

                List <string> rootBlobNames = await CreateBlobsAsync(rootContainer, 2, BlobType.BlockBlob);

                BlobResultSegment     results;
                BlobContinuationToken token = null;
                do
                {
                    results = await blobClient.ListBlobsSegmentedAsync("bb", token);

                    token = results.ContinuationToken;

                    foreach (CloudBlockBlob blob in results.Results)
                    {
                        await blob.DeleteAsync();

                        rootBlobNames.Remove(blob.Name);
                    }
                }while (token != null);
                Assert.AreEqual(0, rootBlobNames.Count);

                results = await blobClient.ListBlobsSegmentedAsync("bb", token);

                Assert.AreEqual(0, results.Results.Count());
                Assert.IsNull(results.ContinuationToken);

                results = await blobClient.ListBlobsSegmentedAsync(name, token);

                Assert.AreEqual(0, results.Results.Count());
                Assert.IsNull(results.ContinuationToken);

                token = null;
                do
                {
                    results = await blobClient.ListBlobsSegmentedAsync(name + "/", token);

                    token = results.ContinuationToken;

                    foreach (CloudBlockBlob blob in results.Results)
                    {
                        Assert.IsTrue(blobNames.Remove(blob.Name));
                    }
                }while (token != null);
                Assert.AreEqual(0, blobNames.Count);
            }
            finally
            {
                container.DeleteIfExistsAsync().Wait();
            }
        }
Пример #56
0
        public async Task CloudBlobClientMaximumExecutionTimeoutAsync()
        {
            CloudBlobClient    blobClient = GenerateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer = BlobTestBase.GetRandomBuffer(80 * 1024 * 1024);

            try
            {
                await container.CreateAsync();

                blobClient.DefaultRequestOptions.MaximumExecutionTime             = TimeSpan.FromSeconds(5);
                blobClient.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024;

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                blockBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024;
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    try
                    {
                        await blockBlob.UploadFromStreamAsync(ms);

                        Assert.Fail();
                    }
                    catch (AggregateException ex)
                    {
#if !FACADE_NETCORE
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message);
#else
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message);
#endif
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                pageBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024;
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    try
                    {
                        await pageBlob.UploadFromStreamAsync(ms);

                        Assert.Fail();
                    }
                    catch (AggregateException ex)
                    {
#if !FACADE_NETCORE
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message);
#else
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message);
#endif
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }
            }
            finally
            {
                blobClient.DefaultRequestOptions.MaximumExecutionTime = null;
                container.DeleteIfExistsAsync().Wait();
            }
        }
Пример #57
0
        public async Task CloudBlobTestAnalyticsRetentionPoliciesAsync()
        {
            CloudBlobClient client = GenerateCloudBlobClient();

            ServiceProperties props = await client.GetServicePropertiesAsync();

            // Set retention policy null with metrics disabled.
            props.Metrics.RetentionDays = null;
            props.Metrics.MetricsLevel  = MetricsLevel.None;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy not null with metrics disabled.
            props.Metrics.RetentionDays = 1;
            props.Metrics.MetricsLevel  = MetricsLevel.Service;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy not null with metrics enabled.
            props.Metrics.MetricsLevel  = MetricsLevel.ServiceAndApi;
            props.Metrics.RetentionDays = 2;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy null with logging disabled.
            props.Logging.RetentionDays     = null;
            props.Logging.LoggingOperations = LoggingOperations.None;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy not null with logging disabled.
            props.Logging.RetentionDays     = 3;
            props.Logging.LoggingOperations = LoggingOperations.None;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy null with logging enabled.
            props.Logging.RetentionDays     = null;
            props.Logging.LoggingOperations = LoggingOperations.All;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());

            // Set retention policy not null with logging enabled.
            props.Logging.RetentionDays     = 4;
            props.Logging.LoggingOperations = LoggingOperations.All;
            await client.SetServicePropertiesAsync(props);

            // Wait for analytics server to update
            await Task.Delay(60 * 1000);

            AssertServicePropertiesAreEqual(props, await client.GetServicePropertiesAsync());
        }
Пример #58
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var message = await result as Activity;

            if (message.Attachments != null)
            {
                var attachment = message.Attachments[0];
                using (HttpClient httpClient = new HttpClient())
                {
                    //  Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.

                    var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);

                    var    contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
                    string filename           = attachment.Name;
                    string dir = AppDomain.CurrentDomain.BaseDirectory; // System.IO.Directory.GetCurrentDirectory();

                    string file = dir + "Uploads";

                    if (!Directory.Exists(file))
                    {
                        DirectoryInfo di = Directory.CreateDirectory(file);
                        //return;
                    }

                    // Try to create the directory.

                    string file1 = dir + "Uploads" + "\\" + filename;

                    FileStream fs = new FileStream(file1, FileMode.Create, FileAccess.Write, FileShare.None);
                    //  FileStream fs = new FileStream(file1, FileMode.Open);
                    //  SaveAttchments(fs, filename);
                    await responseMessage.Content.CopyToAsync(fs).ContinueWith(
                        (copyTask) =>
                    {
                        fs.Close();
                    });


                    string StorageConnectionString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
                    //string SourceFolder = ConfigurationManager.AppSettings["SourceFolder"];
                    string destContainer = ConfigurationManager.AppSettings["destContainer"];

                    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                    CloudBlobContainer blobContainer = cloudBlobClient.GetContainerReference(destContainer);
                    blobContainer.CreateIfNotExists();
                    string         key       = Path.GetFileName(file1);
                    CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(key);
                    using (var fis = System.IO.File.Open(file1, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        blockBlob.UploadFromStream(fis);
                    }


                    await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");

                    await context.PostAsync($"Attachment of {attachment.ContentType} type and size of bytes received.");
                }
            }
            else
            {
                await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
            }

            context.Wait(this.MessageReceivedAsync);
        }
Пример #59
0
        public static void MyClassInitialize(TestContext testContext)
        {
            CloudBlobClient client = GenerateCloudBlobClient();

            startProperties = client.GetServicePropertiesAsync().AsTask().Result;
        }
Пример #60
0
        public async Task ProcessCinemaPerformances(RegionDef region, int cinemaID, List <FilmInfo> films)
        {
            CineworldService  cws            = new CineworldService();
            List <FilmHeader> filmsForCinema = new List <FilmHeader>();

            foreach (var film in films)
            {
                Task <Dates> td = cws.GetDates(region, cinemaID, film.EDI);
                td.Wait();

                HashSet <DateTime> perfDates = new HashSet <DateTime>();
                if (!td.IsFaulted && td.Result != null && td.Result.dates != null)
                {
                    FilmHeader fh = new FilmHeader()
                    {
                        EDI = film.EDI
                    };
                    filmsForCinema.Add(fh);

                    fh.Performances = new List <PerformanceInfo>();

                    foreach (var date in td.Result.dates)
                    {
                        DateTime performanceDate = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);

                        Task <Performances> tp = cws.GetPerformances(region, cinemaID, film.EDI, date);
                        tp.Wait();

                        if (!tp.IsFaulted && tp.Result != null && tp.Result.performances != null)
                        {
                            foreach (var p in tp.Result.performances)
                            {
                                PerformanceInfo pi = new PerformanceInfo();
                                pi.PerformanceTS = performanceDate.Add(DateTime.ParseExact(p.time, "HH:mm", CultureInfo.InvariantCulture).TimeOfDay);
                                pi.Available     = p.available;
                                pi.Type          = p.Type;
                                pi.BookUrl       = new Uri(p.booking_url);
                                fh.Performances.Add(pi);
                            }
                        }
                    }
                }
            }

            //return;

            Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CineStorageConStr"));

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

            // Retrieve a reference to a container.
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobClient.GetContainerReference("data");

            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob cinemaFilmsBlob = container.GetBlockBlobReference(String.Format(FilmsPerCinemaFileName, cinemaID));
            using (Stream s = cinemaFilmsBlob.OpenWrite())
            {
                using (var gzipStream = new GZipStream(s, CompressionMode.Compress))
                {
                    using (StreamWriter sw = new StreamWriter(gzipStream))
                    {
                        await sw.WriteAsync(JsonConvert.SerializeObject(filmsForCinema));
                    }
                }
            }
        }