Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active December 14, 2020 12:05
Show Gist options
  • Save icebeam7/00d7697ccddb2e3af60e79d251f47be5 to your computer and use it in GitHub Desktop.
Save icebeam7/00d7697ccddb2e3af60e79d251f47be5 to your computer and use it in GitHub Desktop.
Servicios/ServicioStorage
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