En este post voy a explicar como puedes subir, modificar y borrar archivos de Blob Containers usando la API de Azure.
¿Qué necesito para trabajar con Blob Storages?.
- Necesitas una suscripción de Azure activa.
- Necesitas descargar el package Nuget para trabajar con Storages.
- Necesitas crear un storage dentro de la plataforma Azure y obtener la cadena de conexión.
- Necesitas tener un Blob Container previamente creado.
- Necesitas tener el AccountName y el AccountKey del Storage a la mano.
Subir un archivo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task<Response<BlobContentInfo>> UploadFileAsync( | |
Stream stream, | |
string blobContainerUri, | |
string fileName) | |
{ | |
var completeUri = $"{blobContainerUri}/{fileName}"; | |
var blobUri = new Uri(completeUri); | |
var credentials = | |
new StorageSharedKeyCredential(_accountName, _accountKey); | |
var blobClient = new BlobClient(blobUri, credentials); | |
return blobClient.UploadAsync(stream); | |
} |
Actualizar un archivo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task<Response<BlobContentInfo>> UpdateFileAsync( | |
Stream stream, | |
string blobContainerUri, | |
string fileName) | |
{ | |
var completeUri = $"{blobContainerUri}/{fileName}"; | |
var blobUri = new Uri(completeUri); | |
var credentials = | |
new StorageSharedKeyCredential(_accountName, _accountKey); | |
var blobClient = new BlobClient(blobUri, credentials); | |
return blobClient.UploadAsync(stream,overwrite: true); | |
} |
Borrar un archivo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task<Response> DeleteFileAsync( | |
string blobContainerUri, | |
string fileName) | |
{ | |
var completeUri = $"{blobContainerUri}/{fileName}"; | |
var blobUri = new Uri(completeUri); | |
var credentials = | |
new StorageSharedKeyCredential(_accountName, _accountKey); | |
var blobClient = new BlobClient(blobUri, credentials); | |
return blobClient.DeleteAsync(); | |
} |
Últimas entradas de Gustavo Sánchez (ver todo)
- NVL in SQL Server - 2023-11-01
- ¿Que es Cake Build? - 2023-02-22
- #How to fix error: MSB4019: The imported project «Microsoft.Data.Tools.Schema.SqlTasks.targets» was not found - 2023-02-20