public async Task <string> UploadFile(Stream fileStream, string filePath) { BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(uploadContainerName); await containerClient.CreateIfNotExistsAsync(); BlobClient blobClient = containerClient.GetBlobClient(filePath); if (await blobClient.ExistsAsync()) { string folder = Path.GetDirectoryName(filePath); string originalFileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath); string extension = Path.GetExtension(filePath); int fileCount = 2; while (await blobClient.ExistsAsync()) { string fileName = originalFileNameWithoutExtension + fileCount.ToString() + extension; filePath = Path.Combine(folder, fileName); blobClient = containerClient.GetBlobClient(filePath); fileCount++; } } await blobClient.UploadAsync(fileStream); return(filePath); }
public async Task UploadAudioFileToStorage(string file) { await _blobClient.UploadAsync(file); if (!await _blobClient.ExistsAsync()) { throw new RequestFailedException($"Can not find file: {file}"); } TestContext.WriteLine($"Uploaded audio file to : {file}"); }
public async Task <string> Add(byte[] file, string container, string filePath, string contentType = null) { BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(container); BlobClient blobClient = containerClient.GetBlobClient(filePath); if (await blobClient.ExistsAsync()) { var properties = await blobClient.GetPropertiesAsync(); if (properties.GetHashCode() == filePath.GetHashCode()) { return(blobClient.Name); } } _logger.LogInformation($"Uploading to Blob Storage:\n\t {blobClient.Uri}\n"); contentType ??= filePath.GetContentType(); var memoryStream = new MemoryStream(file); await blobClient.UploadAsync(memoryStream, new BlobHttpHeaders { ContentType = contentType }); _logger.LogInformation(blobClient.Name); return(filePath); }
private static async Task <string> GetRuntimeKey(ILogger log) { string runtimeKey = string.Empty; var path = Path.Join(Environment.GetEnvironmentVariable("HOME", EnvironmentVariableTarget.Process), Constants.qnamakerFolderPath); var filePath = Path.Join(path, Constants.keyBlobName + ".txt"); // Check for kbid in local file system if (File.Exists(filePath)) { runtimeKey = File.ReadAllText(filePath); } else { BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(Constants.kbContainerName); BlobClient keyBlobClient = containerClient.GetBlobClient(Constants.keyBlobName); // Check blob for kbid if (await keyBlobClient.ExistsAsync()) { BlobDownloadInfo download = await keyBlobClient.DownloadAsync(); using (var streamReader = new StreamReader(download.Content)) { while (!streamReader.EndOfStream) { runtimeKey = await streamReader.ReadLineAsync(); } } } } return(runtimeKey); }
public async Task <string> UploadFile(string containerName, string fileName, byte[] bytes, bool overwrite = true) { // Create the container if not exists and return a container client object BlobContainerClient containerClient = new BlobContainerClient(_connectionString, containerName); await containerClient.CreateIfNotExistsAsync(PublicAccessType.Blob); // Get a reference to a blob BlobClient blobClient = containerClient.GetBlobClient(fileName); if (!overwrite) { string newFileName; int count = 1; while (await blobClient.ExistsAsync()) { newFileName = $"{Path.GetFileNameWithoutExtension(fileName)}-{count++}{Path.GetExtension(fileName)}"; blobClient = containerClient.GetBlobClient(newFileName); } } // Open the file and upload its data await using var uploadFileStream = new MemoryStream(bytes); await blobClient.UploadAsync(uploadFileStream, true); return(blobClient.Uri.AbsoluteUri); }
public async Task <bool> ExistsBlobAsync(string containerName, string blobName) { var blobClient = new BlobClient(this.ConnectionString, containerName, blobName); var existsInfo = await blobClient.ExistsAsync(); return(existsInfo.Value); }
//TODO : Change this for filestream version public async Task <PackageUploadResult> UploadPackageAsync(string packageName, Stream fileStream) { string connectionString = ConnectionString; BlobContainerClient container = new BlobContainerClient(connectionString, "packagecontainer"); await container.CreateIfNotExistsAsync(); string blobName = packageName + ".zip"; BlobClient blob = container.GetBlobClient(blobName); if (await blob.ExistsAsync()) { return(new PackageUploadResult { IsSuccess = false, ErrorMessage = $"Package name, {packageName} is already taken.", }); } await blob.UploadAsync(fileStream); return(new PackageUploadResult { IsSuccess = true, ErrorMessage = "", }); }
public async Task <IActionResult> Download(string id) { var directory = Path.Combine(Directory.GetCurrentDirectory(), "Photos"); var filename = Path.Combine(directory, id); var mimeType = "application/octet-stream"; // Local //var stream = new FileStream(filename, FileMode.Open); // Azure Storage BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient("photos"); Stream stream = null; if (await containerClient.ExistsAsync()) { BlobClient blobClient = containerClient.GetBlobClient(id); if (await blobClient.ExistsAsync()) { stream = new MemoryStream(); BlobDownloadInfo download = await blobClient.DownloadAsync(); await download.Content.CopyToAsync(stream); stream.Seek(0, SeekOrigin.Begin); } } if (stream == null) { return(NotFound()); // returns a NotFoundResult with Status404NotFound response. } return(File(stream, mimeType)); // returns a FileStreamResult }
public static async Task DeleteBlobAsync(BlobMetadata blobMetadata, ILogger log) { log.LogInformation($"Delete queue process item: {blobMetadata}"); string containerEndpoint = string.Format("https://{0}.blob.core.windows.net/{1}", Common.GetEnvironmentVariable("SA_NAME"), blobMetadata.ContainerName); var blobPathAndName = $"{blobMetadata.BlobPath}/{blobMetadata.BlobName}"; BlobContainerClient sourceBlobContainerClient = new BlobContainerClient(new Uri(containerEndpoint), new DefaultAzureCredential()); try { if (await sourceBlobContainerClient.ExistsAsync()) { BlobClient blob = sourceBlobContainerClient.GetBlobClient(blobPathAndName); if (await blob.ExistsAsync()) { await blob.DeleteAsync(); log.LogInformation($"Blob {blobMetadata.BlobName} is deleted"); } } } catch (RequestFailedException) { log.LogInformation($"Failed to complete blob deleting operation: {blobMetadata}"); throw; } }
public static async Task ArchiveBlobAsync(string blobName, ILogger log) { log.LogInformation($"Archiving blob: {blobName}"); string containerEndpoint = string.Format("https://{0}.blob.core.windows.net/archive", Common.GetEnvironmentVariable("SA_NAME")); BlobContainerClient sourceBlobContainerClient = new BlobContainerClient(new Uri(containerEndpoint), new DefaultAzureCredential()); try { if (await sourceBlobContainerClient.ExistsAsync()) { BlobClient blob = sourceBlobContainerClient.GetBlobClient(blobName); if (await blob.ExistsAsync()) { await blob.SetAccessTierAsync(AccessTier.Archive); log.LogInformation($"Access tier set to Archive. Blob name: {blobName}"); } } } catch (RequestFailedException) { log.LogInformation($"Failed to complete blob archiving operation: {blobName}"); throw; } }
protected void SendFile(BlobClient blob, string fileNameAndPath) { if (!File.Exists(fileNameAndPath)) { throw new CantSendFileDataWhenFileDoesNotExistException(fileNameAndPath); } if (!overwrite && blob.ExistsAsync().Result) { return; } using (StreamReader sr = new StreamReader(fileNameAndPath)) { var uploadOptions = new BlobUploadOptions() { HttpHeaders = new BlobHttpHeaders() { ContentHash = ComputeHash(fileNameAndPath) }, TransferOptions = new global::Azure.Storage.StorageTransferOptions() { InitialTransferSize = 50000000 } }; blob.Upload(sr.BaseStream, uploadOptions); } }
public async Task <bool> ExistsFile(string fileName) { BlobClient blobClient = _containerClient.GetBlobClient(fileName); var exists = await blobClient.ExistsAsync(); return(exists); }
public async Task <string> DownloadDocumentAsync(string fileName) { var content = String.Empty; if (_containerExists) { BlobClient blobClient = _containerClient.GetBlobClient(fileName); var exists = await blobClient.ExistsAsync(); if (exists.Value) { // Open the file and upload its data BlobDownloadInfo download = await blobClient.DownloadAsync(); using (MemoryStream memoryStream = new MemoryStream()) { await download.Content.CopyToAsync(memoryStream); content = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); memoryStream.Close(); } } } return(content); }
public override async Task <ScriptSecrets> ReadAsync(ScriptSecretsType type, string functionName) { string secretsContent = null; string blobPath = GetSecretsBlobPath(type, functionName); try { BlobClient secretBlobClient = Container.GetBlobClient(blobPath); if (await secretBlobClient.ExistsAsync()) { var downloadResponse = await secretBlobClient.DownloadAsync(); using (StreamReader reader = new StreamReader(downloadResponse.Value.Content)) { secretsContent = reader.ReadToEnd(); } } } catch (Exception ex) { LogErrorMessage("read", ex); throw; } return(string.IsNullOrEmpty(secretsContent) ? null : ScriptSecretSerializer.DeserializeSecrets(type, secretsContent)); }
public static async Task <IActionResult> UpdateTodo( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = Route + "/{id}")] HttpRequest req, [Blob(BlobPath + "/{id}.json", Connection = "AzureWebJobsStorage")] BlobClient blob, ILogger log, string id) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var updated = JsonConvert.DeserializeObject <TodoUpdateModel>(requestBody); if (!await blob.ExistsAsync()) { return(new NotFoundResult()); } var existingText = await blob.DownloadTextAsync(); var existingTodo = JsonConvert.DeserializeObject <Todo>(existingText); existingTodo.IsCompleted = updated.IsCompleted; if (!string.IsNullOrEmpty(updated.TaskDescription)) { existingTodo.TaskDescription = updated.TaskDescription; } await blob.UploadTextAsync(JsonConvert.SerializeObject(existingTodo)); return(new OkObjectResult(existingTodo)); }
// </snippet_CopyFile> // <snippet_CopyFileToBlob> //------------------------------------------------- // Copy a file from a share to a blob //------------------------------------------------- public async Task CopyFileToBlobAsync(string shareName, string sourceFilePath, string containerName, string blobName) { Uri fileSasUri = GetFileSasUri(shareName, sourceFilePath, DateTime.UtcNow.AddHours(24), ShareFileSasPermissions.Read); // Get a reference to the file we created previously ShareFileClient sourceFile = new ShareFileClient(fileSasUri); // Ensure that the source file exists if (await sourceFile.ExistsAsync()) { // Get the connection string from app settings string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; // Get a reference to the destination container BlobContainerClient container = new BlobContainerClient(connectionString, containerName); // Create the container if it doesn't already exist await container.CreateIfNotExistsAsync(); BlobClient destBlob = container.GetBlobClient(blobName); await destBlob.StartCopyFromUriAsync(sourceFile.Uri); if (await destBlob.ExistsAsync()) { Console.WriteLine($"File {sourceFile.Name} copied to blob {destBlob.Name}"); } } }
// virtual for unit testing public virtual async Task <string> Read(string uri) { // Note: ContainerStartContextSasUri will always be available for Zip based containers. // But the blob pointed to by the uri will not exist until the container is specialized. // When the blob doesn't exist it just means the container is waiting for specialization. // Don't treat this as a failure. var blobClientOptions = new BlobClientOptions(); blobClientOptions.Retry.Mode = RetryMode.Fixed; blobClientOptions.Retry.MaxRetries = 3; blobClientOptions.Retry.Delay = TimeSpan.FromMilliseconds(500); var blobClient = new BlobClient(new Uri(uri), blobClientOptions); if (await blobClient.ExistsAsync(cancellationToken: _cancellationToken)) { var downloadResponse = await blobClient.DownloadAsync(cancellationToken : _cancellationToken); using (StreamReader reader = new StreamReader(downloadResponse.Value.Content, true)) { string content = reader.ReadToEnd(); return(content); } } return(string.Empty); }
public async Task <bool> Exists(string blob) { BlobClient blobClient = containerClient.GetBlobClient(blob); var res = await blobClient.ExistsAsync(); return(res.Value); }
public async Task <bool> ExistsAsync(string name) { BlobClient client = this.containerClient.GetBlobClient(name); Response <bool> response = await client.ExistsAsync(); return(response.Value); }
private static async Task <string> GetObjectAsync(BlobContainerClient client, string fileName, CancellationToken token) { try { BlobClient blobClient = client.GetBlobClient(fileName); if (await blobClient.ExistsAsync()) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { await blobClient.DownloadToAsync(ms, token); ms.Position = 0; using (System.IO.StreamReader sr = new System.IO.StreamReader(ms)) return(await sr.ReadToEndAsync()); } } } catch (Exception ex) { throw; } return(null); }
/// <inheritdoc/> public async Task <string> DownloadBlobAsync(string blobName) { BlobClient blobClient = this.blobContainerClient.GetBlobClient(blobName); bool isExists = await blobClient.ExistsAsync().ConfigureAwait(false); if (isExists) { var blob = await blobClient.DownloadAsync().ConfigureAwait(false); byte[] streamArray = new byte[blob.Value.ContentLength]; long numBytesToRead = blob.Value.ContentLength; int numBytesRead = 0; int maxBytesToRead = 10; do { if (numBytesToRead < maxBytesToRead) { maxBytesToRead = (int)numBytesToRead; } int n = blob.Value.Content.Read(streamArray, numBytesRead, maxBytesToRead); numBytesRead += n; numBytesToRead -= n; }while (numBytesToRead > 0); return(Convert.ToBase64String(streamArray)); } else { this.logger.TraceWarning($"BlobStorageClient - Method: {nameof(this.DownloadBlobAsync)} - No blob found with name {blobName}."); return(null); } }
public async Task <BlobContainerInfo> CloneContainer(string sourceContainerName, string targetContainerName) { try { // create target container BlobContainerClient targetContainerClient = new BlobContainerClient(connectionString, targetContainerName); BlobContainerInfo targetContainer = await targetContainerClient.CreateAsync(); // get source container BlobContainerClient sourceContainerClient = blobServiceClient.GetBlobContainerClient(sourceContainerName); // blobs from source container await foreach (BlobItem blob in sourceContainerClient.GetBlobsAsync()) { // create a blob client for the source blob BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation // to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. await lease.AcquireAsync(TimeSpan.FromSeconds(60)); // Get a BlobClient representing the destination blob with a unique name. BlobClient destBlob = targetContainerClient.GetBlobClient(sourceBlob.Name); // Start the copy operation. await destBlob.StartCopyFromUriAsync(sourceBlob.Uri); // Update the source blob's properties. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); } } } return(targetContainer); } catch (RequestFailedException e) { Console.WriteLine("HTTP error code {0}: {1}", e.Status, e.ErrorCode); Console.WriteLine(e.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public async Task <bool> Exists(Blob blob) { var blobClient = new BlobClient(new Uri(blob.Identifier)); var containerName = blobClient.BlobContainerName; var blobName = blobClient.Name; blobClient = new BlobClient(_connectionString, containerName, blobName); return((await blobClient.ExistsAsync()).Value); }
public async Task RemoveAsync(ProductRef product) { BlobClient blob = await this.OpenBlobClient(product); if ((await blob.ExistsAsync())) { await blob.DeleteAsync(); } }
public async Task <bool> BlobExistsAsync(string connectionString, string containerName, string blobName, CancellationToken cancellationToken) { _logger.LogInformation("BlobExistsAsync: {0}/{1}/{2}", connectionString, containerName, blobName); var client = new BlobClient(connectionString, containerName, blobName); var response = await client.ExistsAsync(cancellationToken).ConfigureAwait(false); return(response.Value); }
public async Task DownloadModelAsync(Guid runId, Stream destination) { BlobClient blobClient = this.modelRepositoryClient.GetBlobClient(this.modelPathGenerator.GetModelName(runId)); if (!await blobClient.ExistsAsync()) { throw new FileNotFoundException($"No model exists for Run ID {runId}"); } await blobClient.DownloadToAsync(destination); }
public async Task RemoveBlobAsync(string containerName, string blobUrl, CancellationToken cancellationToken = default) { BlobClient blobClient = azureStorageClientFactory.GetBlobClient(containerName, blobUrl); if (!await blobClient.ExistsAsync(cancellationToken)) { throw new ArgumentException($"Blob {blobUrl} is not available."); } await blobClient.DeleteAsync(cancellationToken : cancellationToken); }
/// <inheritdoc/> public async Task <IImageCacheResolver> GetAsync(string key) { BlobClient blob = this.container.GetBlobClient(key); if (!await blob.ExistsAsync()) { return(null); } return(new AzureBlobStorageCacheResolver(blob)); }
private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo info, ILogger log) { try { // Get the name of the first blob in the container to use as the source. string blobName = info.fileName; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. //await lease.AcquireAsync(TimeSpan.FromSeconds(100)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); log.LoggerInfo($"Lease state: {sourceProperties.LeaseState}", info); Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null); // Get a BlobClient representing the destination blob BlobClient destBlob = destContainer.GetBlobClient(blobName);//destContainer.GetBlobClient(blob_sas_uri.ToString()); // Start the copy operation. await destBlob.StartCopyFromUriAsync(blob_sas_uri); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); } } } catch (RequestFailedException ex) { log.LoggerError($"RequestFailedException: {ex.Message}", ex?.StackTrace, info); Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }
public async Task <bool> CheckUploadFileExistence(UserArc userArc, PictureTypeEnum typeEnum, String imageFileUri) { String[] imageUriArray = imageFileUri.Split('/'); String imageFileName = WebUtility.UrlDecode(imageUriArray[imageUriArray.Length - 1]); BlobContainerClient blobContainer = await GetOrCreateCloudBlobContainer(GetUploadPicContainerName(), PublicAccessType.None); BlobClient imageBlob = blobContainer.GetBlobClient(imageFileName); bool isExist = await imageBlob.ExistsAsync(); return(isExist); }