-
-
Save icebeam7/00d7697ccddb2e3af60e79d251f47be5 to your computer and use it in GitHub Desktop.
Servicios/ServicioStorage
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
using System; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
using Azure.Storage.Blobs; | |
using ICTalkMobileApp.Models; | |
namespace ICTalkMobileApp.Services | |
{ | |
public class ServicioStorage | |
{ | |
private const string cadenaConexionStorage = "REEMPLAZA-CADENA-CONEXION"; | |
private const string nombreContenedor = "imagenes"; | |
private const string funcionBusqueda = "REEMPLAZA-AZURE-FUNCTION"; | |
BlobServiceClient clienteServicio; | |
BlobContainerClient clienteContenedor; | |
public ServicioStorage() | |
{ | |
clienteServicio = new BlobServiceClient(cadenaConexionStorage); | |
try | |
{ | |
clienteContenedor = clienteServicio.CreateBlobContainer(nombreContenedor); | |
} | |
catch (Exception ex) | |
{ | |
clienteContenedor = clienteServicio.GetBlobContainerClient(nombreContenedor); | |
} | |
} | |
public async Task SubirImagen(string imagen) | |
{ | |
using (var fs = File.Open(imagen, FileMode.Open)) | |
{ | |
var nombreBlob = Path.GetFileName(imagen); | |
await clienteContenedor.UploadBlobAsync(nombreBlob, fs); | |
} | |
} | |
public async Task<List<Analisis>> ObtenerImagenesSimilares(string imagen) | |
{ | |
using (var clienteHttp = new HttpClient()) | |
{ | |
var nombre = Path.GetFileName(imagen); | |
var url = $"{funcionBusqueda}&nombre={nombre}"; | |
var respuesta = await clienteHttp.GetAsync(url); | |
if (respuesta.IsSuccessStatusCode) | |
{ | |
var json = await respuesta.Content.ReadAsStringAsync(); | |
var lista = JsonConvert.DeserializeObject<List<Analisis>>(json); | |
return lista; | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment