Skip to content

Instantly share code, notes, and snippets.

@uno-de-piera
Created August 18, 2019 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uno-de-piera/db8a76ac315fea4596c0dcd784a1e3a3 to your computer and use it in GitHub Desktop.
Save uno-de-piera/db8a76ac315fea4596c0dcd784a1e3a3 to your computer and use it in GitHub Desktop.
import { Storage } from "aws-amplify";
export default class S3Utils {
static async uploadImage(image, folder) {
// parseamos el base64 para obtener los datos que necesitamos para crear la imagen
const base64Parsed = new Buffer(
image.replace(/^data:image\/\w+;base64,/, ""),
"base64"
);
// obtenemos la extensión
const type = image.split(";")[0].split("/")[1];
// generamos un nombre aleatorio
const fileName = `${folder}/${Math.random()
.toString(36)
.substr(2, 15)}.${type}`;
// hacemos la subida a S3 diciéndole a Amplify que el contenido es un base64, así lo retorna Vuetify Image Input
const { key } = await Storage.put(fileName, base64Parsed, {
contentType: "image/jpg",
contentEncoding: "base64"
});
// path del archivo en nuestro bucket de S3
return key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment