/// <summary> /// Initiates the SolCat Azure blob data sync. /// </summary> public static void CopyBlobData() { // Authentication Credentials for Azure Storage: var credsSrc = new StorageCredentials( ConfigHelper.GetConfigValue("HubContainerName"), ConfigHelper.GetConfigValue("HubContainerKey")); var credsDest = new StorageCredentials( ConfigHelper.GetConfigValue("NodeContainerKey"), ConfigHelper.GetConfigValue("NodeContainerKey")); // Source Container: Hub (Development) _srcContainer = new CloudBlobContainer( new Uri(ConfigHelper.GetConfigValue("HubContainerUri")), credsSrc); // Destination Container: Node (Production) _destContainer = new CloudBlobContainer( new Uri(ConfigHelper.GetConfigValue("NodeContainerUri")), credsDest); // Set permissions on the container: var permissions = new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Blob}; _srcContainer.SetPermissions(permissions); _destContainer.SetPermissions(permissions); // Call the blob copy master method: CopyBlobs(_srcContainer, _destContainer); }
public string Get() { var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(Constants.ConfigurationKeys.StorageConnection)); var blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(CloudConfigurationManager.GetSetting(Constants.ConfigurationKeys.ContainerName)); container.CreateIfNotExists(); var blobPermissions = new BlobContainerPermissions(); blobPermissions.SharedAccessPolicies.Add(AppPolicyName, new SharedAccessBlobPolicy() { // To ensure SAS is valid immediately, don’t set start time. // This way, you can avoid failures caused by small clock differences. SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5), Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read }); blobPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(blobPermissions); var sasToken = container.GetSharedAccessSignature(new SharedAccessBlobPolicy(), AppPolicyName); return container.Uri + sasToken; }
public static void PackContainerCompareData(StorageBlob.CloudBlobContainer container, Dictionary <string, object> dic) { StorageBlob.BlobContainerPermissions permissions = container.GetPermissions(); dic["PublicAccess"] = permissions.PublicAccess; dic["Permission"] = permissions; dic["LastModified"] = container.Properties.LastModified; }
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); } }
static public void Upload(string filepath, string blobname, string accountName, string accountKey) { try { StorageCredentials creds = new StorageCredentials(accountName, accountKey); CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer sampleContainer = client.GetContainerReference("public-samples"); sampleContainer.CreateIfNotExists(); // for public access //// BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; sampleContainer.SetPermissions(permissions); ///////////////////////// CloudBlockBlob blob = sampleContainer.GetBlockBlobReference(blobname); using (Stream file = File.OpenRead(filepath)) { blob.UploadFromStream(file); } } catch (Exception ex) { Console.WriteLine(ex); } }
public GetUploadLocationResponse Post(GetUploadLocationRequest request) { _log.StartLog(request); try { var contName = "deletable" + Guid.NewGuid().ToString("D").ToLowerInvariant(); var container = _azure.CreateContainerIfDoesNotExist(contName); var blobPolicy = new SharedAccessBlobPolicy { Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List, SharedAccessExpiryTime = DateTimeOffset.Now.AddHours(HOURS_FILE_SHOULD_BE_ACCESSIBLE) }; var permissions = new BlobContainerPermissions(); permissions.SharedAccessPolicies.Add("mypolicy", blobPolicy); permissions.PublicAccess = BlobContainerPublicAccessType.Container; container.SetPermissions(permissions); var sharedAccessSignature = container.GetSharedAccessSignature("mypolicy"); return new GetUploadLocationResponse {Name= container.Name, Location = container.Uri.ToString(), Sas = sharedAccessSignature, Account = _azure.Account}; } catch (Exception e) { _log.Fatal("error", e); Errors.Add(e.Message + " " + e.StackTrace); return new GetUploadLocationResponse {Errors = Errors}; } }
/// <summary> /// set the access level of specified container /// </summary> /// <param name="name">container name</param> /// <param name="accessLevel">access level in ("off", "blob", "container")</param> internal void SetContainerAcl(string name, BlobContainerPublicAccessType accessLevel) { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = accessLevel; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; CloudBlobContainer container = Channel.GetContainerReference(name); if (!Channel.DoesContainerExist(container, requestOptions, OperationContext)) { throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name)); } Channel.SetContainerPermissions(container, permissions, accessCondition, requestOptions, OperationContext); AzureStorageContainer azureContainer = new AzureStorageContainer(container, permissions); if (PassThru) { WriteObjectWithStorageContext(azureContainer); } }
public static bool DeleteFile(string accountName, string accountKey, string containerName, string fileName, bool delSubDirs) { try { CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=" + accountName + ";AccountKey=" + accountKey); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(containerName); container.CreateIfNotExists(); BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(containerPermissions); if (false == delSubDirs) { CloudBlockBlob remoteFile = container.GetBlockBlobReference(fileName); remoteFile.DeleteIfExists(); } if (true == delSubDirs) { foreach (IListBlobItem item in container.ListBlobs(fileName, true)) { CloudBlockBlob blob = (CloudBlockBlob)item; blob.DeleteIfExists(); } } } catch (Exception) { return false; } return true; }
/// <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); } }
/// <summary> /// set the access level of specified container /// </summary> /// <param name="name">container name</param> /// <param name="accessLevel">access level in ("off", "blob", "container")</param> internal async Task SetContainerAcl(long taskId, IStorageBlobManagement localChannel, string name, BlobContainerPublicAccessType accessLevel) { if (!NameUtil.IsValidContainerName(name)) { throw new ArgumentException(String.Format(Resources.InvalidContainerName, name)); } BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = accessLevel; BlobRequestOptions requestOptions = RequestOptions; AccessCondition accessCondition = null; CloudBlobContainer container = localChannel.GetContainerReference(name); if (!await localChannel.DoesContainerExistAsync(container, requestOptions, OperationContext, CmdletCancellationToken)) { throw new ResourceNotFoundException(String.Format(Resources.ContainerNotFound, name)); } await localChannel.SetContainerPermissionsAsync(container, permissions, accessCondition, requestOptions, OperationContext, CmdletCancellationToken); if (PassThru) { WriteCloudContainerObject(taskId, localChannel, container, permissions); } }
/// <summary> /// Function to get Blob container /// </summary> /// <param name="containerName">container to look</param> /// <returns>Blob container</returns> public CloudBlobContainer GetBlobContainer() { // get the container reference var blobContainer = blobClient.GetContainerReference(containerName); try { // Create the container if it does not exist. var options = new BlobRequestOptions { MaximumExecutionTime = TimeSpan.FromSeconds(2), }; if (blobContainer.CreateIfNotExists()) { // Set permissions on the container, if it was created. var containerPermissions = new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }; blobContainer.SetPermissions(containerPermissions); } } catch (Exception ex) { } return blobContainer; }
public StorageBlob.CloudBlobContainer CreateContainer(string containerName, StorageBlob.BlobContainerPublicAccessType permission) { StorageBlob.CloudBlobContainer container = CreateContainer(containerName); StorageBlob.BlobContainerPermissions containerPermission = new StorageBlob.BlobContainerPermissions(); containerPermission.PublicAccess = permission; container.SetPermissions(containerPermission); return(container); }
public azureStorageHelper(string acountName, string accountKey) { sCredentials = new StorageCredentials(acountName, accountKey); storageAccount = new CloudStorageAccount(sCredentials, true); queueClient = storageAccount.CreateCloudQueueClient(); blobClient = storageAccount.CreateCloudBlobClient(); permisions = new BlobContainerPermissions(); permisions.PublicAccess = BlobContainerPublicAccessType.Container; }
public static async Task<string> SaveBinaryToAzureStorage(MobileAppSettingsDictionary settings, string blobId, string blobData ) { string storageAccountName; string storageAccountKey; // Try to get the Azure storage account token from app settings. if (!(settings.TryGetValue("STORAGE_ACCOUNT_NAME", out storageAccountName) | settings.TryGetValue("STORAGE_ACCOUNT_ACCESS_KEY", out storageAccountKey))) { return string.Empty; } // Set the URI for the Blob Storage service. Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", storageAccountName)); // Create the BLOB service client. CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentials(storageAccountName, storageAccountKey)); string ContainerName = "beerdrinkinimages"; // Create a container, if it doesn't already exist. CloudBlobContainer container = blobClient.GetContainerReference(ContainerName); await container.CreateIfNotExistsAsync(); // Create a shared access permission policy. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); // Enable anonymous read access to BLOBs. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(containerPermissions); // Define a policy that gives write access to the container for 1 minute. SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow, SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(1), Permissions = SharedAccessBlobPermissions.Write }; // Get the SAS as a string. var SasQueryString = container.GetSharedAccessSignature(sasPolicy); // Set the URL used to store the image. var avatarUri = string.Format("{0}{1}/{2}", blobEndpoint.ToString(), ContainerName, blobId.ToLower()); // Upload the new image as a BLOB from the string. CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(blobId.ToLower()); blobFromSASCredential.UploadTextAsync(blobData); return avatarUri; }
public void SetupEnvironment() { var blobClient = _acct.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(POST_FILE_CONTAINER); container.CreateIfNotExists(); BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(containerPermissions); }
/// <summary> /// Assign permissions for a download to occur for our readonly permission policy /// </summary> static void AssignDownloadPolicyContainerPermissions(CloudBlobContainer container) { // Secure storage. We will provide read tokens for access to blob data on a per-blob basis. var containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off; containerPermissions.SharedAccessPolicies.Add(DownloadPolicyName, new SharedAccessBlobPolicy { Permissions = SharedAccessBlobPermissions.Read }); container.SetPermissions(containerPermissions); }
public void CreateBlobs(bool createSettingsBlob, bool createModelBlob, int modelId = 1) { if (createSettingsBlob || createModelBlob) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(MockCommandCenter.StorageConnectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); var localContainer = blobClient.GetContainerReference(this.localAzureContainerName); localContainer.CreateIfNotExists(); if (createSettingsBlob) { var settingsBlob = localContainer.GetBlockBlobReference(this.localAzureSettingsBlobName); byte[] settingsContent = this.GetSettingsBlobContent(); settingsBlob.UploadFromByteArray(settingsContent, 0, settingsContent.Length); this.localAzureSettingsBlobUri = settingsBlob.Uri.ToString(); } if (createModelBlob) { var modelBlob = localContainer.GetBlockBlobReference(this.localAzureModelBlobName); byte[] modelContent = this.GetCBModelBlobContent(modelId); modelBlob.UploadFromByteArray(modelContent, 0, modelContent.Length); this.localAzureModelBlobUri = modelBlob.Uri.ToString(); } var locationContainer = blobClient.GetContainerReference(this.localAzureBlobLocationContainerName); locationContainer.CreateIfNotExists(); var publicAccessPermission = new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }; locationContainer.SetPermissions(publicAccessPermission); var metadata = new ApplicationTransferMetadata { ApplicationID = "test", ConnectionString = MockCommandCenter.StorageConnectionString, ExperimentalUnitDuration = 15, IsExplorationEnabled = true, ModelBlobUri = this.localAzureModelBlobUri, SettingsBlobUri = this.localAzureSettingsBlobUri, ModelId = "latest" }; var locationBlob = locationContainer.GetBlockBlobReference(this.token); byte[] locationBlobContent = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(metadata)); locationBlob.UploadFromByteArray(locationBlobContent, 0, locationBlobContent.Length); } }
private async static Task<CloudBlobContainer> ContainerFromStorageAccount(CloudStorageAccount storageAccount, string containerName, bool publicAccess) { // Azure blob storage doesn't like names starting with upper case letters containerName = containerName.ToLowerInvariant(); var permissions = new BlobContainerPermissions { PublicAccess = publicAccess ? BlobContainerPublicAccessType.Blob : BlobContainerPublicAccessType.Off }; var container = storageAccount.CreateCloudBlobClient().GetContainerReference(containerName); await container.CreateIfNotExistsAsync().ConfigureAwait(false); await container.SetPermissionsAsync(permissions).ConfigureAwait(false); return container; }
public static void ClassInit(TestContext context) { // Azure setup if (!String.IsNullOrEmpty(TestCommon.AzureStorageKey)) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(TestCommon.AzureStorageKey); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(testContainer); // Create the container if it doesn't already exist. container.CreateIfNotExists(); // Upload files CloudBlockBlob blockBlob = container.GetBlockBlobReference("office365.png"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(@".\resources\office365.png")) { blockBlob.UploadFromStream(fileStream); } CloudBlobContainer containerSecure = blobClient.GetContainerReference(testContainerSecure); // Create the container if it doesn't already exist. containerSecure.CreateIfNotExists(); // Avoid public access to this test container BlobContainerPermissions bcp = new BlobContainerPermissions(); bcp.PublicAccess = BlobContainerPublicAccessType.Off; containerSecure.SetPermissions(bcp); blockBlob = containerSecure.GetBlockBlobReference("custom.spcolor"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(@".\resources\custom.spcolor")) { blockBlob.UploadFromStream(fileStream); } blockBlob = containerSecure.GetBlockBlobReference("custombg.jpg"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(@".\resources\custombg.jpg")) { blockBlob.UploadFromStream(fileStream); } blockBlob = containerSecure.GetBlockBlobReference("ProvisioningTemplate-2015-03-Sample-01.xml"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(@".\resources\templates\ProvisioningTemplate-2015-03-Sample-01.xml")) { blockBlob.UploadFromStream(fileStream); } } }
protected async Task<CloudBlobContainer> GetBlobContainer(StorageContainerTarget target) { // construct a cloud blob client for the configured storage account var cloudBlobClient = target.StorageAccount.CreateCloudBlobClient(); cloudBlobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(10), 5); // get the target blob container (to store the generated reports) var targetBlobContainer = cloudBlobClient.GetContainerReference(target.ContainerName); await targetBlobContainer.CreateIfNotExistsAsync(); var blobContainerPermissions = new BlobContainerPermissions(); blobContainerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; await targetBlobContainer.SetPermissionsAsync(blobContainerPermissions); return targetBlobContainer; }
public void RunAtAppStartup(string storageAccountName, string storageAccountKey, string containerName) { CloudBlobContainer startCloudBlobContainer = SetUpContainer(storageAccountName, storageAccountKey, containerName); //just in case, check to see if the container exists, // and create it if it doesn't cloudBlobContainer.CreateIfNotExists(); //set access level to "blob", which means user can access the blob // but not look through the whole container //this means the user must have a URL to the blob to access it BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Blob; cloudBlobContainer.SetPermissions(permissions); }
public string GetUploadSasUrl(UploadRequest request) { string imageUri = string.Empty; // Set the URI for the Blob Storage service. Uri blobEndpoint = new Uri(string.Format("https://{0}.blob.core.windows.net", AppSettings.StorageAccountName)); // Create the BLOB service client. CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, storageCred); if (request.ContainerName != null) { // Set the BLOB store container name on the item, which must be lowercase. request.ContainerName = request.ContainerName.ToLower(); // Create a container, if it doesn't already exist. CloudBlobContainer container = blobClient.GetContainerReference(request.ContainerName); container.CreateIfNotExistsAsync(); // Create a shared access permission policy. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); // Enable anonymous read access to BLOBs. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(containerPermissions); // Define a policy that gives write access to the container for 5 minutes. SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow, SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5), Permissions = SharedAccessBlobPermissions.Write }; // Get the SAS as a string. var sasQueryString = container.GetSharedAccessSignature(sasPolicy); // Set the URL used to store the image. imageUri = string.Format("{0}{1}/{2}{3}", blobEndpoint.ToString(), request.ContainerName, request.FileName, sasQueryString); } // Complete the insert operation. return imageUri; }
/// <summary> /// init azure storage container using CloudBlobContainer and BlobContainerPermissions /// </summary> /// <param name="container">CloudBlobContainer object</param> /// <param name="permissions">permissions of container</param> public AzureStorageContainer(CloudBlobContainer container, BlobContainerPermissions permissions) { CloudBlobContainer = container; Permission = permissions; Name = container.Name; if (permissions == null) { PublicAccess = null; } else { PublicAccess = permissions.PublicAccess; } LastModified = container.Properties.LastModified; }
static void CreateSharedAccessPolicy(CloudBlobClient blobClient, CloudBlobContainer container, string policyName) { //Create a new stored access policy and define its constraints. SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy() { SharedAccessExpiryTime = DateTime.UtcNow.AddHours(10), Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List }; //Get the container's existing permissions. BlobContainerPermissions permissions = new BlobContainerPermissions(); //Add the new policy to the container's permissions. permissions.SharedAccessPolicies.Clear(); permissions.SharedAccessPolicies.Add(policyName, sharedPolicy); container.SetPermissions(permissions); }
public static void Create(string account, string key, string containerName, bool publicAccess) { if (string.IsNullOrEmpty(containerName)) return; CloudBlobClient blobClient = Client.GetBlobClient(account, key); Uri uri = new Uri(string.Format(blobClient.BaseUri + "{0}", containerName)); CloudBlobContainer cont = new CloudBlobContainer(uri, blobClient.Credentials); cont.Create(); if (publicAccess) { BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; cont.SetPermissions(permissions); } }
public static void ClassInit(TestContext context) { if (String.IsNullOrEmpty(TestCommon.AzureStorageKey)) { return; } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(TestCommon.AzureStorageKey); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(testContainer); // Create the container if it doesn't already exist. container.CreateIfNotExists(); // Upload files CloudBlockBlob blockBlob = container.GetBlockBlobReference("ProvisioningTemplate-2015-03-Sample-01.xml"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(String.Format(@"{0}\..\..\Resources\Templates\{1}", AppDomain.CurrentDomain.BaseDirectory, "ProvisioningTemplate-2015-03-Sample-01.xml"))) { blockBlob.UploadFromStream(fileStream); } blockBlob = container.GetBlockBlobReference("ProvisioningTemplate-2015-03-Sample-02.xml"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(String.Format(@"{0}\..\..\Resources\Templates\{1}", AppDomain.CurrentDomain.BaseDirectory, "ProvisioningTemplate-2015-03-Sample-02.xml"))) { blockBlob.UploadFromStream(fileStream); } CloudBlobContainer containerSecure = blobClient.GetContainerReference(testContainerSecure); // Create the container if it doesn't already exist. containerSecure.CreateIfNotExists(); // Avoid public access to this test container BlobContainerPermissions bcp = new BlobContainerPermissions(); bcp.PublicAccess = BlobContainerPublicAccessType.Off; containerSecure.SetPermissions(bcp); blockBlob = containerSecure.GetBlockBlobReference("ProvisioningTemplate-2015-03-Sample-02.xml"); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = System.IO.File.OpenRead(String.Format(@"{0}\..\..\Resources\Templates\{1}", AppDomain.CurrentDomain.BaseDirectory, "ProvisioningTemplate-2015-03-Sample-02.xml"))) { blockBlob.UploadFromStream(fileStream); } }
private static void CreateSAS(string containerName) { try { //Create the blob client CloudBlobClient blobClient = storageAcc.CreateCloudBlobClient(); //Get the reference to a container CloudBlobContainer container = blobClient.GetContainerReference(containerName); // Create a permission object BlobContainerPermissions blobPermissions = new BlobContainerPermissions(); // Create a Shared access policy with read/write access to the container for 15 seconds. blobPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessBlobPolicy() { //SAS effective immediately and is valid for 15 seconds SharedAccessExpiryTime = DateTime.UtcNow.AddSeconds(15), Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read }); // Public access is turned off to make it private blobPermissions.PublicAccess = BlobContainerPublicAccessType.Off; // Set the permission policy on the container. container.SetPermissions(blobPermissions); // Get the shared access signature to share with users. string sasToken = container.GetSharedAccessSignature(new SharedAccessBlobPolicy(), "mypolicy"); Write(string.Format("Shared Access URI is : {0}", sasToken)); using (StreamWriter sw = new StreamWriter(@"C:\data\sas.html")) { string url = @"https://attosolstorage.blob.core.windows.net/files/txt/file1.txt"; sw.WriteLine(string.Format("{0}{1}", url, sasToken)); System.Diagnostics.Process.Start(string.Format("{0}{1}", url, sasToken)); } } catch (Exception ex) { Write(ex.Message); } }
//AzureBlobにアクセス public CloudBlobContainer BlobAccess() { // 現状は、研究室サーバにアップロードする場合でしか動作確認できていない。 // WindowsAzureにアップロードする場合は、ConnectionStringを変更しなければならないはずであるが、まだ確認できていません。 String ConnectionString = "UseDevelopmentStorage=true;"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("catscontainer01"); container.CreateIfNotExists(); BlobContainerPermissions permission = new BlobContainerPermissions(); permission.PublicAccess = BlobContainerPublicAccessType.Blob; container.SetPermissions(permission); return container; }
public static void Run() { #region Obtain a client from the connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); Console.WriteLine("Parsed connection string and created blob client."); #endregion #region Obtain reference to docs container CloudBlobContainer docContainer = blobClient.GetContainerReference("docs"); Console.WriteLine("Obtained reference to docs container."); #endregion #region Create blob shared access policy var blobContainerPermissions = new BlobContainerPermissions(); blobContainerPermissions.SharedAccessPolicies.Add("ReadOnlyPolicy", new SharedAccessBlobPolicy { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = null, }); docContainer.SetPermissions(blobContainerPermissions); #endregion #region Get blob share access signature Console.WriteLine("\nObtaining shared access signature."); CloudBlockBlob blockBlob = docContainer.GetBlockBlobReference(@"WhatIsBlobStorage.txt"); var signature = blockBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy { SharedAccessExpiryTime = DateTime.UtcNow.AddDays(10) }, "ReadOnlyPolicy"); Console.WriteLine("{0,-24}:{1}", "Shared Access Signature", signature); Console.WriteLine("{0,-24}:{1}", "Blob URI", blockBlob.Uri.AbsoluteUri); Console.WriteLine("{0,-24}:{1}{2}", "Shared Access URI", blockBlob.Uri.AbsoluteUri, signature); #endregion }
public static async Task<string> UploadFile(string fileName, string container, byte[] fileBytes) { //get configuration data string url = null; string azureBlobProtocol = ConfigurationManager.AppSettings["abs:Protocol"]; string azureBlobAccountName = ConfigurationManager.AppSettings["abs:AccountName"]; string azureBlobAccountkey = ConfigurationManager.AppSettings["abs:AccountKey"]; container = container.Replace(".", ""); // Initialize the Azure account information string connString = string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", azureBlobProtocol, azureBlobAccountName, azureBlobAccountkey); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connString); // Create the blob client, which provides authenticated access to the Blob service. CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); // Get the container reference...create if it does not exist CloudBlobContainer blobContainer = blobClient.GetContainerReference(container); if (!blobContainer.Exists()) await blobContainer.CreateAsync(); CloudBlockBlob blob = blobContainer.GetBlockBlobReference(fileName); // Set permissions on the container. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off; await blobContainer.SetPermissionsAsync(containerPermissions); //upload the file using a memory stream using (MemoryStream stream = new MemoryStream(fileBytes)) { stream.Write(fileBytes, 0, fileBytes.Length); stream.Seek(0, SeekOrigin.Begin); stream.Flush(); blob.UploadFromStream(stream); stream.Close(); //get the url of the blob url = String.Format("{0}://{1}.blob.core.windows.net/{2}/{3}", azureBlobProtocol, azureBlobAccountName, container, fileName); } return url; }
static void Main(string[] args) { CloudStorageAccount storageAccount; if (CloudStorageAccount.TryParse( ConfigurationManager.ConnectionStrings[STORAGE_CONN_STR_KEY_NAME].ConnectionString, out storageAccount)) { // Create the queue to put messages into after uploading images. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); CloudQueue queue = queueClient.GetQueueReference(PICTURE_LOADER_QUEUE_NAME.ToLower()); queue.CreateIfNotExists(); // Create a blob container to upload images to. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(PICTURE_BLOB_CONTAINER_NAME.ToLower()); blobContainer.CreateIfNotExists(); BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Container; blobContainer.SetPermissions(containerPermissions); var imagesPath = System.IO.Directory.GetCurrentDirectory() + @"\Images"; var imageFiles = System.IO.Directory.GetFiles(imagesPath); foreach (string imageFile in imageFiles) { // Upload to blob storage. var blobUri = UploadImage(imageFile, blobContainer); // Add a message to queue storage referencing the blob. if (!string.IsNullOrEmpty(blobUri)) { Console.WriteLine("Adding message to Azure Storage Queue '{0}.", PICTURE_LOADER_QUEUE_NAME.ToLower()); var picture = new Picture() { Name = System.IO.Path.GetFileName(imageFile), PictureUrl = blobUri }; queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(picture))); } Console.WriteLine(); } } }
private void InitializeStorage() { // Open storage account using credentials from .cscfg file. var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString()); // Get context object for working with queues, and Get a reference to the queue. CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); reportRequestsQueue = queueClient.GetQueueReference(ConfigurationManager.AppSettings["ida:QueueReportRequest"].ToString()); reportRequestsQueue.CreateIfNotExists(); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer csvContainer = blobClient.GetContainerReference(ConfigurationManager.AppSettings["ida:BlobReportPublish"].ToString()); csvContainer.CreateIfNotExists(); // create new permissions BlobContainerPermissions perms = new BlobContainerPermissions(); perms.PublicAccess = BlobContainerPublicAccessType.Blob; // blob public access csvContainer.SetPermissions(perms); }
internal void StartCopyBlobTest(Agent agent, bool useUri) { CloudBlobUtil blobUtil = new CloudBlobUtil(CommonStorageAccount); blobUtil.SetupTestContainerAndBlob(); string copiedName = Utility.GenNameString("copied"); if (useUri) { //Set the blob permission, so the copy task could directly copy by uri StorageBlob.BlobContainerPermissions permission = new StorageBlob.BlobContainerPermissions(); permission.PublicAccess = StorageBlob.BlobContainerPublicAccessType.Blob; blobUtil.Container.SetPermissions(permission); } try { if (useUri) { Test.Assert(agent.StartAzureStorageBlobCopy(blobUtil.Blob.Uri.ToString(), blobUtil.ContainerName, copiedName, PowerShellAgent.Context), Utility.GenComparisonData("Start copy blob using source uri", true)); } else { Test.Assert(agent.StartAzureStorageBlobCopy(blobUtil.ContainerName, blobUtil.BlobName, blobUtil.ContainerName, copiedName), Utility.GenComparisonData("Start copy blob using blob name", true)); } Test.Info("Get destination blob in copy task"); StorageBlob.ICloudBlob blob = blobUtil.Container.GetBlobReferenceFromServer(copiedName); Test.Assert(blob != null, "Destination blob should exist after start copy. If not, please check it's a test issue or dev issue."); string sourceUri = CloudBlobUtil.ConvertCopySourceUri(blobUtil.Blob.Uri.ToString()); Test.Assert(blob.BlobType == blobUtil.Blob.BlobType, String.Format("The destination blob type should be {0}, actually {1}.", blobUtil.Blob.BlobType, blob.BlobType)); Test.Assert(blob.CopyState.Source.ToString().StartsWith(sourceUri), String.Format("The source of destination blob should start with {0}, and actually it's {1}", sourceUri, blob.CopyState.Source.ToString())); } finally { blobUtil.CleanupTestContainerAndBlob(); } }
public void GetCopyStateFromUriTest() { blobUtil.SetupTestContainerAndBlob(); string copiedName = Utility.GenNameString("copied"); //Set the blob permission, so the copy task could directly copy by uri StorageBlob.BlobContainerPermissions permission = new StorageBlob.BlobContainerPermissions(); permission.PublicAccess = StorageBlob.BlobContainerPublicAccessType.Blob; blobUtil.Container.SetPermissions(permission); try { Test.Assert(agent.StartAzureStorageBlobCopy(blobUtil.Blob.Uri.ToString(), blobUtil.ContainerName, copiedName, PowerShellAgent.Context), Utility.GenComparisonData("Start copy blob using source uri", true)); Test.Assert(agent.GetAzureStorageBlobCopyState(blobUtil.ContainerName, copiedName, true), "Get copy state in dest container should be successed."); AssertFinishedCopyState(blobUtil.Blob.Uri); } finally { blobUtil.CleanupTestContainerAndBlob(); } }