public GetBlobReference ( string blobAddressUri ) : |
||
blobAddressUri | string | The name of the blob, or the absolute URI to the blob. |
return |
public const int CacheControlOneWeekExpiration = 7 * 24 * 60 * 60; // 1 week #endregion Fields #region Methods /// <summary> /// Finds all js and css files in a container and creates a gzip compressed /// copy of the file with ".gzip" appended to the existing blob name /// </summary> public static void EnsureGzipFiles( CloudBlobContainer container, int cacheControlMaxAgeSeconds) { string cacheControlHeader = "public, max-age=" + cacheControlMaxAgeSeconds.ToString(); var blobInfos = container.ListBlobs( new BlobRequestOptions() { UseFlatBlobListing = true }); Parallel.ForEach(blobInfos, (blobInfo) => { string blobUrl = blobInfo.Uri.ToString(); CloudBlob blob = container.GetBlobReference(blobUrl); // only create gzip copies for css and js files string extension = Path.GetExtension(blobInfo.Uri.LocalPath); if (extension != ".css" && extension != ".js") return; // see if the gzip version already exists string gzipUrl = blobUrl + ".gzip"; CloudBlob gzipBlob = container.GetBlobReference(gzipUrl); if (gzipBlob.Exists()) return; // create a gzip version of the file using (MemoryStream memoryStream = new MemoryStream()) { // push the original blob into the gzip stream using (GZipStream gzipStream = new GZipStream( memoryStream, CompressionMode.Compress, CompressionLevel.BestCompression)) using (BlobStream blobStream = blob.OpenRead()) { blobStream.CopyTo(gzipStream); } // the gzipStream MUST be closed before its safe to read from the memory stream byte[] compressedBytes = memoryStream.ToArray(); // upload the compressed bytes to the new blob gzipBlob.UploadByteArray(compressedBytes); // set the blob headers gzipBlob.Properties.CacheControl = cacheControlHeader; gzipBlob.Properties.ContentType = GetContentType(extension); gzipBlob.Properties.ContentEncoding = "gzip"; gzipBlob.SetProperties(); } }); }
private static void UploadBlob(CloudBlobContainer container, string blobName, string filename) { CloudBlob blob = container.GetBlobReference(blobName); using (FileStream fileStream = File.OpenRead(filename)) blob.UploadFromStream(fileStream); }
public void deleteFromBlob(string uri, string blobname) { CloudStorageAccount storageAccount; storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("BlobConnectionString")); blobClient = storageAccount.CreateCloudBlobClient(); blobContainer = blobClient.GetContainerReference(blobname); var blob = blobContainer.GetBlobReference(uri); blob.DeleteIfExists(); }
/// <summary> /// Save the data table to the given azure blob. This will overwrite an existing blob. /// </summary> /// <param name="table">instance of table to save</param> /// <param name="container">conatiner</param> /// <param name="blobName">blob name</param> public static void SaveToAzureBlob(this DataTable table, CloudBlobContainer container, string blobName) { var blob = container.GetBlobReference(blobName); using (BlobStream stream = blob.OpenWrite()) using (TextWriter writer = new StreamWriter(stream)) { table.SaveToStream(writer); } }
public AzureTapeStream(string name, string connectionString, string containerName, ITapeStreamSerializer serializer) { _serializer = serializer; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); blobClient = storageAccount.CreateCloudBlobClient(); container = blobClient.GetContainerReference(containerName); container.CreateIfNotExist(); _blob = container.GetBlobReference(name); }
public AzureStorageSourceDataCollection(CloudBlobContainer container, string blobRootPath) { mContainer = container; mBlobRootPath = blobRootPath; Names .Where(i => mContainer.GetBlobReference(mBlobRootPath + "/" + i.Value + ".txt") != null) .ForEach(i => mStreams.Add(i.Key, ((Func<string, Stream>)GetStream).Curry()(i.Value))); var missing = Required.Except(mStreams.Keys); if (missing.Count() > 0) throw new ArgumentOutOfRangeException("Missing mandatory data."); }
static void UploadWithMd5(CloudBlobContainer container, string name, string path) { var blob = container.GetBlobReference(name); blob.Properties.ContentMD5 = GetMd5(path); semaphore.WaitOne(); var stream = File.OpenRead(path); Interlocked.Increment(ref count); blob.BeginUploadFromStream(stream, (ar) => { blob.EndUploadFromStream(ar); stream.Close(); semaphore.Release(); Interlocked.Decrement(ref count); }, null); }
private void CopyFromBlobToLocal(CloudBlobContainer blobContainer, LocalResource localStorage, string blobName) { CloudBlob node = blobContainer.GetBlobReference(blobName); using (BlobStream nodestream = node.OpenRead()) { var nodefile = Path.Combine(localStorage.RootPath, blobName); if (!System.IO.File.Exists(nodefile)) { using (var fileStream = new FileStream(nodefile, FileMode.CreateNew)) { nodestream.CopyTo(fileStream); fileStream.Flush(); fileStream.Close(); } } } }
// The mutex must already exists public Mutex(CloudBlobContainer container, string mutexName, Exception e) { blob = container.GetBlobReference(mutexName); byte[] b1 = { 1 }; BlobRequestOptions requestOpt = new BlobRequestOptions(); bool keepGoing = true; string oldEtag = ""; int lastChange = 0; do { byte[] b; string eTag; try { blob.FetchAttributes(); eTag = blob.Attributes.Properties.ETag; if (eTag != oldEtag) { lastChange = Environment.TickCount; oldEtag = eTag; } b = blob.DownloadByteArray(); } catch (Exception) { throw e; } requestOpt.AccessCondition = AccessCondition.IfMatch(eTag); if (b[0] == 0 || Environment.TickCount - lastChange > 3000) // on ne peut garder un lock plus de 3 s { try { blob.UploadByteArray(b1, requestOpt); keepGoing = false; } catch (StorageClientException ex) { if (ex.ErrorCode != StorageErrorCode.ConditionFailed) throw; } } else Thread.Sleep(50); // constante arbitraire } while (keepGoing); }
/// <summary> /// Read a data table from azure blob. This will read the entire blob into memory and return a mutable data table. /// </summary> /// <param name="builder">builder</param> /// <param name="container">conatiner</param> /// <param name="blobName">blob name</param> /// <returns>in-memory mutable datatable from blob</returns> public static MutableDataTable ReadFromAzureBlob(this DataTableBuilder builder, CloudBlobContainer container, string blobName) { CloudBlob blob = container.GetBlobReference(blobName); if (!Exists(blob)) { string containerName = container.Name; string accountName = container.ServiceClient.Credentials.AccountName; throw new FileNotFoundException(string.Format("container.blob {0}.{0} does not exist on the storage account '{2}'", containerName, blobName, accountName)); } // We're returning a MutableDataTable (which is in-memory) anyways, so fine to download into an in-memory buffer. // Avoid downloading to a file because Azure nodes may not have a local file resource. string content = blob.DownloadText(); var stream = new StringReader(content); return DataTable.New.Read(stream); }
public void SetUp() { blobSettings = new LeaseBlockBlobSettings { ConnectionString = "UseDevelopmentStorage=true", ContainerName = "test" + Guid.NewGuid().ToString("N"), BlobPath = "lease.blob", ReAquirePreviousTestLease = false, RetryCount = 2, RetryInterval = TimeSpan.FromMilliseconds(250) }; maximumStopDurationEstimateSeconds = 10; var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true"); var client = storageAccount.CreateCloudBlobClient(); container = client.GetContainerReference(blobSettings.ContainerName); container.CreateIfNotExist(); leaseBlob = container.GetBlobReference(blobSettings.BlobPath); leaseBlob.UploadByteArray(new byte[0]); }
public string DownloadText(CloudBlobContainer cloudBlobContainer, string blobName) { return cloudBlobContainer.GetBlobReference(blobName).DownloadText(); }
public byte[] DownloadByteArray(CloudBlobContainer cloudBlobContainer, string blobName) { return cloudBlobContainer.GetBlobReference(blobName).DownloadByteArray(); }
public bool DeleteIfExists(CloudBlobContainer cloudBlobContainer, string blobName) { return cloudBlobContainer.GetBlobReference(blobName).DeleteIfExists(); }
public bool Rename(CloudBlobContainer cloudBlobContainer, string oldBlobName, string newBlobName) { var oldCloudBlob = cloudBlobContainer.GetBlobReference(oldBlobName); cloudBlobContainer.GetBlobReference(newBlobName).CopyFromBlob(oldCloudBlob); return oldCloudBlob.DeleteIfExists(); }
/// <summary> /// Initializes a new instance of the Azure /// </summary> /// <param name="container">Container</param> /// <param name="objId">Object Id</param> public Azure(CloudBlobContainer container, string objId) { this.Path = objId; this.blob = container.GetBlobReference(objId); this.RelativePath = this.blob.Name; }
// Adds a new command file to a jod description after uploading a file to a blob private static void createArgument(string FileName, string commandName, VENUSJobDescription mySimpleJobDescription, CloudBlobContainer blobContainer, string nameOut="") { var commandBlob = blobContainer.GetBlobReference(FileName); commandBlob.UploadFile(FileName); var commandFile = new AzureArgumentSingleReference(); commandFile.Name = commandName; // This has to be the same name as in the application description if ( nameOut == "" ){ commandFile.DataAddress = commandBlob.Uri.AbsoluteUri; } else{ commandFile.DataAddress = blobContainer.GetBlobReference(nameOut).Uri.AbsoluteUri; } commandFile.ConnectionString = UserDataStoreConnectionString; mySimpleJobDescription.JobArgs.Add(commandFile); }
/// <summary> /// Create or Update Blob /// Works with wpsprivate public static void UpdateBlob(string containerName, string blobName, MemoryStream data, string mimeType) { try { CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("RemoteDataStorage"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); var container = new CloudBlobContainer(containerName, blobClient); container.CreateIfNotExist(); // Setup the permissions on the container to be public //var permissions = new BlobContainerPermissions{PublicAccess = BlobContainerPublicAccessType.Container }; //container.SetPermissions(permissions); CloudBlob blob = container.GetBlobReference(blobName); blob.UploadFromStream(data); if (mimeType.Length > 0) { // Set the properties blob.Properties.ContentType = mimeType; blob.SetProperties(); } } catch (Exception sc) { Logger.LogError(string.Format("Error getting contents of blob {0}/{1}: {2}", containerName, blobName, sc.Message)); } }
public static void Init(CloudBlobContainer container, string mutexName) { byte[] b0 = { 0 }; CloudBlob blob = container.GetBlobReference(mutexName); blob.UploadByteArray(b0); }
public static void UploadBlobFile(byte[] fileBytes, string fileName) { try { string storageAccountConnection = string.Empty; storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString(); // If you want to use Windows Azure cloud storage account, use the following // code (after uncommenting) instead of the code above. cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection); // Create the blob client, which provides // authenticated access to the Blob service. blobClient = cloudStorageAccount.CreateCloudBlobClient(); string deploymentPackageFolderString = string.Empty; deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString(); // Get the container reference. blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString); // Create the container if it does not exist. blobContainer.CreateIfNotExist(); // Set permissions on the container. containerPermissions = new BlobContainerPermissions(); // This sample sets the container to have public blobs. Your application // needs may be different. See the documentation for BlobContainerPermissions // for more information about blob container permissions. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); blob = blobContainer.GetBlobReference(fileName); // Open a stream using the cloud object using (BlobStream blobStream = blob.OpenWrite()) { blobStream.Write(fileBytes, 0, fileBytes.Count()); blobStream.Flush(); blobStream.Close(); } } catch (System.Exception ex) { Logger.Write(string.Format("Error in UploadBlobFile() Error: {0}", ex.Message)); } }
public static string ReadBlobFile(string fileName) { // byte[] fileBytes = null; string fileBytes = string.Empty; try { string storageAccountConnection = string.Empty; storageAccountConnection = ConfigurationManager.AppSettings["StorageAccount.ConnectionString"].ToString(); // If you want to use Windows Azure cloud storage account, use the following // code (after uncommenting) instead of the code above. cloudStorageAccount = CloudStorageAccount.Parse(storageAccountConnection); // Create the blob client, which provides // authenticated access to the Blob service. blobClient = cloudStorageAccount.CreateCloudBlobClient(); string deploymentPackageFolderString = string.Empty; deploymentPackageFolderString = ConfigurationManager.AppSettings["DeploymentPackageFolder"].ToString(); // Get the container reference. blobContainer = blobClient.GetContainerReference(deploymentPackageFolderString); // Create the container if it does not exist. blobContainer.CreateIfNotExist(); // Set permissions on the container. containerPermissions = new BlobContainerPermissions(); // This sample sets the container to have public blobs. Your application // needs may be different. See the documentation for BlobContainerPermissions // for more information about blob container permissions. containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); blob = blobContainer.GetBlobReference(fileName); BlobRequestOptions blobReqOptions = new BlobRequestOptions(); blobReqOptions.Timeout = new TimeSpan(0, 5, 0); fileBytes = blob.DownloadText(blobReqOptions); } catch (System.Exception ex) { Logger.Write(string.Format("Error in ReadBlobFile() Error: {0}", ex.Message)); fileBytes = null; } return fileBytes; }
/// <summary> /// Iterates through each blob in the specified container and adds the /// Cache-Control and ContentType headers /// </summary> public static void EnsureStaticFileHeaders( CloudBlobContainer container, int cacheControlMaxAgeSeconds) { string cacheControlHeader = "public, max-age=" + cacheControlMaxAgeSeconds.ToString(); var blobInfos = container.ListBlobs( new BlobRequestOptions() { UseFlatBlobListing = true }); Parallel.ForEach(blobInfos, (blobInfo) => { // get the blob properties and set headers if necessary CloudBlob blob = container.GetBlobReference(blobInfo.Uri.ToString()); blob.FetchAttributes(); var properties = blob.Properties; bool wasModified = false; // see if a content type is defined for the extension string extension = Path.GetExtension(blobInfo.Uri.LocalPath); string contentType = GetContentType(extension); if (String.IsNullOrEmpty(contentType)) { Trace.TraceWarning("Content type not found for extension:" + extension); } else { if (properties.ContentType != contentType) { properties.ContentType = contentType; wasModified = true; } } if (properties.CacheControl != cacheControlHeader) { properties.CacheControl = cacheControlHeader; wasModified = true; } if (wasModified) { blob.SetProperties(); } }); }
private static CloudBlob GetBlobAndVerify(CloudBlobContainer container, string blobName) { CloudBlob blob = container.GetBlobReference(blobName); if (!Exists(blob)) { string containerName = container.Name; string accountName = container.ServiceClient.Credentials.AccountName; throw new FileNotFoundException(string.Format("container.blob {0}.{0} does not exist on the storage account '{2}'", containerName, blobName, accountName)); } return blob; }
private void UploadDirectoryRecursive(string path, CloudBlobContainer container) { string cxmlPath = null; // use 16 threads to upload Parallel.ForEach(EnumerateDirectoryRecursive(path), new ParallelOptions { MaxDegreeOfParallelism = 16 }, (file) => { // save collection-#####.cxml for last if (Path.GetFileName(file).StartsWith("collection-") && Path.GetExtension(file) == ".cxml") { cxmlPath = file; } else { // upload each file, using the relative path as a blob name UploadFile(file, container.GetBlobReference(Path.GetFullPath(file).Substring(path.Length))); } }); // finish up with the cxml itself if (cxmlPath != null) { UploadFile(cxmlPath, container.GetBlobReference(Path.GetFullPath(cxmlPath).Substring(path.Length))); UploadFile(cxmlPath, container.GetBlobReference("collection-current.cxml")); } }
public static MemoryStream readFromBlob(string containerName, string blobName) { MemoryStream blobContent = null; try { CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("RemoteDataStorage"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); var container = new CloudBlobContainer(containerName, blobClient); var blob = container.GetBlobReference(blobName); blobContent = new MemoryStream(); blob.DownloadToStream(blobContent); blobContent.Seek(0, SeekOrigin.Begin); } catch (Exception sc) { Logger.LogError(string.Format("Error getting contents of blob {0}/{1}: {2}", containerName, blobName, sc.Message)); } return blobContent; }
public TaskRunner CreateBlobNotExistsTaskRunner(CloudBlobContainer blobContainer, FileInformation fileInfo) { this._statistics.BlobNotExistCount++; if (fileInfo.SizeInBytes > CloudBlobConstants.FileSizeThresholdInBytes) { return new UploadLargeFileTaskRunner(_messageBus, _fileSystem, fileInfo, blobContainer); } else { return new SingleActionTaskRunner(() => { _messageBus.Publish(new FileProgressedMessage(fileInfo.FullPath, 0)); CloudBlob blob = blobContainer.GetBlobReference(fileInfo.BlobName); blob.UploadFile(fileInfo); _messageBus.Publish(new FileProgressedMessage(fileInfo.FullPath, 1)); }); } }
private static string UploadProfileImage(int id = 0, string username = "", HttpPostedFileBase file = null) { try { DeleteProfileImage("", id); #region Old way /*string directory = "/Admin/Content/img/profile_pics"; string ext = Path.GetExtension(file.FileName).ToLower(); UDF.OpenPermissions(directory); string file_path = Path.Combine(HttpContext.Current.Server.MapPath(directory), Path.GetFileName(username + ext)); if (!allowed_profiletypes.Contains(ext)) { throw new Exception(); } Image img = Image.FromStream(file.InputStream); Size size = new Size(72,72); img = ResizeImage(img, size); img.Save(file_path, System.Drawing.Imaging.ImageFormat.Png);*/ #endregion #region Azure Blob // Set up connection to Windows Azure Storage CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); _BlobClient = storageAccount.CreateCloudBlobClient(); // For large file copies you need to set up a custom timeout period // and using parallel settings appears to spread the copy across multiple threads // if you have big bandwidth you can increase the thread number below // because Azure accepts blobs broken into blocks in any order of arrival. F*****g awesome! _BlobClient.Timeout = new System.TimeSpan(1, 0, 0); _BlobClient.ParallelOperationThreadCount = 2; // Get and create the container _BlobContainer = _BlobClient.GetContainerReference("profile-pictures"); _BlobContainer.CreateIfNotExist(); // Set the permissions on the container to be public _BlobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); // Make a unique blob name string extension = System.IO.Path.GetExtension(file.FileName); string filename = username; // Create the Blob and upload the file CloudBlob blob = _BlobContainer.GetBlobReference(filename + extension); // Create an image object and resize the image to 72x72 Image img = Image.FromStream(file.InputStream); Size size = new Size(72, 72); img = ResizeImage(img, size); // Push the image into a MemoryStream and upload the stream to our blob, f**k this is too much work // why can't we just say here's the image, now put it in a blob! Damn you!!!! MemoryStream stream = new MemoryStream(); img.Save(stream,System.Drawing.Imaging.ImageFormat.Png); byte[] imgBytes = stream.GetBuffer(); stream.Seek(0,SeekOrigin.Begin); blob.UploadFromStream(stream); // Oh yeah, dispose the stream so we don't eat up memory stream.Dispose(); /// Set the metadata into the blob blob.Metadata["FileName"] = filename; blob.Metadata["Submitter"] = "Automated Encoder"; blob.SetMetadata(); // Set the properties blob.Properties.ContentType = file.ContentType; blob.SetProperties(); #endregion return blob.Uri.ToString(); } catch (Exception) { return ""; } }
static void upload(string UUID, string file) { try { string path = Path.GetFullPath(@""+file); BlobContainerPermissions containerPermissions; blobContainer = blobClient.GetContainerReference(UUID); blobContainer.CreateIfNotExist(); containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions); blobContainer = blobClient.GetContainerReference(UUID); CloudBlob blob = blobContainer.GetBlobReference(UUID); Console.WriteLine("Starting file upload"); blob.UploadFile(path); Console.WriteLine("File upload complete\n"); } catch (Exception e) { Console.WriteLine("Error uploading file: " + e); } }
private static void GetFilesRecursive(string path, CloudBlobContainer container) { foreach (var item in Directory.GetFiles(path)) { //the item is under the format F://folder/subfolder/filename.ext //Removing the drive letter first string blobName = item.Substring(4); var blob = container.GetBlobReference(blobName); try { using (FileStream fileStream = new FileStream(item, FileMode.Open)) { blob.UploadFromStream(fileStream); } } catch (IOException ex) { Trace.TraceError(string.Format("MongoHelper.GetFilesRecursive exception during the copy of the file : {0} - Exception message : {1}", item, ex != null ? ex.Message : "Unknown")); } catch (StorageClientException ex) { Trace.TraceError(string.Format("MongoHelper.GetFilesRecursive exception during the copy of the file : {0} - Exception message : {1}", item, ex != null ? ex.Message : "Unknown")); } } GetDirectories(path, container); }
///<summary>Updates the blobs in a storage container.</summary> ///<param name="container">The container to update.</param> ///<param name="newVersion">The new version being uploaded.</param> ///<param name="sourcePath">The directory on the local disk containing the new files.</param> public static void UpdateStorage(CloudBlobContainer container, Version newVersion, string sourcePath) { container.FetchAttributes(); var remoteFiles = container.ListBlobs(Options).Cast<CloudBlob>().ToArray(); var oldBlobs = remoteFiles.ToDictionary( blob => Path.Combine(sourcePath, container.Uri.MakeRelativeUri(blob.Uri).ToString().Replace('/', '\\') ) ); var baseUri = new Uri(sourcePath, UriKind.Absolute); foreach (var file in new DirectoryInfo(sourcePath).EnumerateFiles("*", SearchOption.AllDirectories)) { CloudBlob blob; byte[] hash = file.SHA512Hash(); //If there already is a blob for this file, use it. if (!oldBlobs.TryGetValue(file.FullName, out blob)) { blob = container.GetBlobReference(baseUri.MakeRelativeUri(new Uri(file.FullName, UriKind.Absolute)).ToString()); } else { oldBlobs.Remove(file.FullName); //Remove the blob from the dictionary; all blobs left in the dictionary will be deleted. if (file.Length == blob.Properties.Length && Convert.FromBase64String(blob.Metadata["SHA512"]).SequenceEqual(hash)) continue; //If the blob is identical, don't re-upload it. } blob.Metadata["SHA512"] = Convert.ToBase64String(hash); blob.UploadFile(file.FullName); } foreach (var blob in oldBlobs.Values) blob.Delete(); container.Metadata["Version"] = newVersion.ToString(); container.SetMetadata(); }