Synology FileStation API Timeout 에러
Posted at 
2024.08.07 09:24
Synology FileStation API를 사용해 파일 업로드 시 계속 pending 상태인 문제에 대한 해결 방법을 공유합니다.

소개

어쩌다 보니 Synology FileStation API를 이용해 파일을 업로드 & 다운로드해야 할 일이 생겼다.

Synology NAS에서 공식 API를 제공해 주기 때문에 금방 구현할 수 있을줄 알았으나, sid를 받아오는 작업까지는 잘 되는데 파일을 업로드하려고 하면 계속 pending 상태로 대기하는 문제가 발생했다.

해결 방안

Synology FileStation API를 사용하기 위한 순서는 다음과 같다.

  1. 로그인(sid 발급) [/webapi/auth.cgi?api=SYNO.API.Auth&method=login...] GET
  2. 위에서 발급받은 sid를 이용해 API 요청 [/webapi/entry.cgi?_sid={로그인 시 발급받은 sid}&api=SYNO.FileStation.Upload&method=upload...]
  3. 로그아웃 [/webapi/auth.cgi?api=SYNO.API.Auth&method=logout&_sid={로그인 시 발급받은 sid}...] GET

여기서 공식 문서 상으로는 파일 업로드 시 POST Method를 사용하라고 되어 있지만, POST로 요청을 보내면 계속 요청을 보내고 있는 상태에서 무한 대기 상태가 된다.

따라서, PUT Method를 이용해 요청을 보내면 정상적으로 파일 업로드를 할 수 있다.

const uploadQuery = new URLSearchParams();

uploadQuery.set("api", "SYNO.FileStation.Upload");
uploadQuery.set("version", "3");
uploadQuery.set("method", "upload");
uploadQuery.set("create_parents", "true");
uploadQuery.set("overwrite", "skip");
uploadQuery.set("path", "/test");
uploadQuery.set("_sid", sid as string);

const uploadFormData = new FormData();
uploadFormData.append("file", file);

const uploadResponse = await fetch(`${process.env.FILE_UPLOAD_URL}/webapi/entry.cgi?${uploadQuery.toString()}`, {
	method: "PUT",
	body: uploadFormData
})
	.then(async (res) => {
		const data = await res.json();
		console.log(data);

        if (data.success) return data.data;
        else return null;
    })
    .catch(() => null);

프레소
Copyright © PRESSO. All Rights Reserved.