Ho pensato di provare un approccio diverso utilizzando lo storage S3. Ho seguito le istruzioni qui per configurare un bucket AWS S3. Ecco le mie impostazioni:
Ed ecco il mio codice:
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;
}
Ora, mi rendo conto che questo attualmente non funzionerà perché non sto effettivamente caricando il file. Da quello che ho letto nella documentazione, dovrei inviare un oggetto json contenente il tipo, il nome del file e la dimensione del file. L’API dovrebbe rispondere con una chiave e un URL da utilizzare per il trasferimento effettivo del file. Ma a questo punto, sto riscontrando il seguente errore:
{
"errors": [
"The requested URL or resource could not be found."
],
"error_type": "not_found"
}
[
“The requested URL or resource could not be found.”
]
Ho controllato la mia chiave API per assicurarmi che avesse i permessi, li ha. Ma ne ho creata una nuova comunque. E una globale. Nessuna sta funzionando. Stesso codice di errore. Cosa sto sbagliando?
Modifica, ecco l’oggetto img:
{"type":"composer","file_name":"face2.jpg","file_size":17342}
