# API를 통해 사진을 게시하는 방법은?

**URL:** https://meta.discourse.org/t/how-to-post-pictures-via-api/314282
**Category:** Development
**Created:** [6월 29, 2024, 6:49오전 UTC](https://meta.discourse.org/t/how-to-post-pictures-via-api/314282 "2024-06-29T06:49:34Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![maxtim](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maxtim/32/427804_2.png) [@maxtim](https://meta.discourse.org/u/maxtim)
#### Post date: [7월 2, 2024, 10:03오후 UTC](https://meta.discourse.org/t/how-to-post-pictures-via-api/314282/4 "2024-07-02T22:03:39Z")

</div>

그래서 S3 스토리지를 사용하여 다른 방법을 시도해 보았습니다. [여기](https://meta.discourse.org/t/set-up-file-and-image-uploads-to-s3/7229)의 설명을 따라 AWS S3 버킷을 설정했습니다. 제 설정은 다음과 같습니다:

 ![s3settings](https://global.discourse-cdn.com/meta/original/4X/c/5/e/c5e1bfd3dcdfe83d19fced09c479134f7d811dfd.jpeg)

그리고 제 코드는 다음과 같습니다:

```javascript
	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;
	}

```

현재는 실제로 파일을 업로드하지 않고 있으므로 작동하지 않는다는 것을 깨달았습니다. [문서](https://docs.discourse.org/#tag/Uploads/operation/generatePresignedPut)에서 읽은 바로는, type, file\_name, file\_size를 포함하는 json 객체를 보내야 합니다. 그러면 API가 실제 파일 전송에 사용할 키와 url을 반환해야 합니다. 하지만 이 시점에서 다음과 같은 오류가 발생합니다:

```plaintext
{
    "errors": [
        "The requested URL or resource could not be found."
    ],
    "error_type": "not_found"
}

```

```plaintext
[
    "The requested URL or resource could not be found."
]

```

API 키에 권한이 있는지 확인해 보았지만, 권한은 있습니다. 그래도 새로 만들었고, 전역 키도 만들었습니다. 하지만 아무것도 작동하지 않습니다. 동일한 오류 코드가 나옵니다. 제가 무엇을 잘못하고 있나요?

수정: 여기 img 객체가 있습니다:

```plaintext
{"type":"composer","file_name":"face2.jpg","file_size":17342}

```

---

_[View the full topic](https://meta.discourse.org/t/how-to-post-pictures-via-api/314282)._
