そこで、S3ストレージを使用する別の方法を試してみることにしました。AWS S3バケットの設定については、こちらの指示に従いました。設定は以下の通りです。
そして、コードは以下の通りです。
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;
}
現在、実際にファイルをアップロードしていないため、これは機能しないだろうと理解しています。ドキュメントで読んだところによると、type、file_name、file_sizeを含むJSONオブジェクトを送信するはずです。APIは、実際のファイル転送に使用するキーとURLを返信してくれるはずです。しかし、この時点で、以下のエラーが発生しています。
{
"errors": [
"The requested URL or resource could not be found."
],
"error_type": "not_found"
}
[
"The requested URL or resource could not be found."
]
APIキーに権限があるか確認しましたが、あります。しかし、念のため新しいものを作成しました。グローバルなものも作成しました。どれも機能しません。同じエラーコードです。何が間違っていますか?
編集、imgオブジェクトは以下の通りです。
{"type":"composer","file_name":"face2.jpg","file_size":17342}
