Así que pensé en probar un enfoque diferente utilizando el almacenamiento S3. Seguí las instrucciones aquí para configurar un bucket AWS S3. Aquí están mis configuraciones:
Y aquí está mi código:
async uploadExternalImage(imageReferences: string[]): Promise<string[]> {
const imageUrls: string[] = [];
for (const ref of imageReferences) {
const filePath = this.app.metadataCache.getFirstLinkpathDest(ref, this.activeFile.name)?.path;
if (filePath) {
const file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
if (file) {
try {
const url = `${this.settings.baseUrl}/uploads/generate-presigned-put.json`;
//const imgfile = await this.app.vault.readBinary(file);
const img = {
type: "composer",
file_name: file.name,
file_size: file.stat.size,
}
console.log(JSON.stringify(img));
const headers = {
"Content-Type": "application/json",
"Api-Key": this.settings.apiKey,
"Api-Username": this.settings.disUser,
};
const response = await requestUrl({
url: url,
method: "POST",
body: JSON.stringify(img),
throw: false,
headers: headers
})
console.log(response.json)
} catch (error) {
console.error(`Error uploading: ${error}`);
//console.log(response.json)
}
} else {
console.error('error')
}
} else {
console.error('error')
}
}
return imageUrls;
}
Ahora, me doy cuenta de que esto actualmente no funcionará porque aún no estoy subiendo el archivo. Por lo que leí en la documentación, enviaría un objeto JSON que contiene el tipo, el nombre del archivo y el tamaño del archivo. La API debería responder con una clave y una URL para que yo la use para la transferencia real del archivo. Pero en este punto, estoy obteniendo el siguiente error:
{
"errors": [
"La URL o el recurso solicitado no se pudieron encontrar."
],
"error_type": "not_found"
}
[
“La URL o el recurso solicitado no se pudieron encontrar.”
]
Revisé mi clave API para asegurarme de que tuviera permisos, los tiene. Pero creé una nueva de todos modos. Y una global. Ninguna está funcionando. Mismo código de error. ¿Qué estoy haciendo mal?
Editar, aquí está el objeto img:
{"type":"composer","file_name":"face2.jpg","file_size":17342}
