AtlasX Web Service รองรับการจัดการไฟล์ได้หลายแหล่งพร้อมกัน ทั้ง local folder และ network drive โดยกำหนดค่าไว้ใน configuration และเรียกใช้งานผ่าน IDirectoryAccessService
การตั้งค่า
กำหนด file sources ใน appsettings.json
{
"App": {
"FileServer": {
"DefaultFileSource": "Default",
"FileSourceParameter": "APP_FILE_SOURCE",
"FilePathParameter": "APP_FILE_PATH",
"FileSource": {
"Default": {
"RemotePath": "FilesLocalStorage"
},
"NetworkDriveEx": {
"RemotePath": "\\\\mynetworkdrive\\d$\\Shared Files",
"Username": "admin",
"Password": "password",
"Domain": ""
}
}
}
}
}ระบบรองรับ file source 2 ประเภท
| ประเภท | RemotePath |
| Local folder | ชื่อ folder เช่น FilesLocalStorage |
| Network drive | UNC path เช่น \\server\share |
FileServer
| Field | Description |
DefaultFileSource | ชื่อ file source ที่ใช้เป็นค่าเริ่มต้นเมื่อไม่ระบุ file source ตอนเรียกใช้งาน |
FileSourceParameter | กำหนดชื่อ key ที่ใช้ระบุ file source ตอนเรียก CreateDirectoryAccess() ถ้ากำหนดเป็น APP_FILE_SOURCE ก็ต้องส่ง key ชื่อนี้เข้าไปใน parameters |
FilePathParameter | กำหนดชื่อ key ที่ใช้ระบุ path ตอนเรียก CreateDirectoryAccess() ถ้ากำหนดเป็น APP_FILE_PATH ก็ต้องส่ง key ชื่อนี้เข้าไปใน parameters |
FileSource | รายการ file sources ทั้งหมด โดยใช้ชื่อเป็น key เช่น Default, NetworkDriveEx |
FileSource
| Field | Required | Description |
RemotePath | Required | Path ของ file source ถ้าเป็น local folder ใช้ชื่อ folder ถ้าเป็น network drive ใช้ UNC path เช่น \\server\share |
Username | Optional | ชื่อผู้ใช้สำหรับ authentication ใช้เฉพาะ network drive |
Password | Optional | รหัสผ่านสำหรับ authentication ใช้เฉพาะ network drive |
Domain | Optional | Domain สำหรับ authentication ใช้เฉพาะ network drive ที่ต้องการ domain |
Service Registration
ลงทะเบียน DirectoryAccess เข้า DI container ใน Program.cs
using AtlasX.Engine.RemoteDirectory.Services;
builder.Services.AddDirectoryAccess(builder.Configuration, "App:FileServer");การใช้งาน
Inject IDirectoryAccessService
public class MyService
{
private readonly IDirectoryAccessService _directoryAccess;
public MyService(IDirectoryAccessService directoryAccess)
{
_directoryAccess = directoryAccess;
}
}Save Files
// Default file source
var directory = _directoryAccess.GetFileSource();
directory.PathName = "uploads/images";
directory.SaveFile("photo.jpg", fileBytes);
// ระบุ file source
var directory = _directoryAccess.GetFileSource("NetworkDriveEx");
directory.PathName = "uploads/documents";
directory.SaveFile("report.pdf", fileBytes);Get File
var directory = _directoryAccess.GetFileSource();
directory.PathName = "uploads/images";
var stream = directory.GetFile("photo.jpg");Create Directory Access from Parameters
กรณีที่ต้องการให้ผู้เรียกใช้งานกำหนด file source และ path เองตอน runtime ใช้ CreateDirectoryAccess() แทน GetFileSource()
var parameters = new Dictionary<string, object>
{
{ "APP_FILE_SOURCE", "NetworkDriveEx" }, // ระบุว่าใช้ NetworkDriveEx
{ "APP_FILE_PATH", "uploads/documents" }
};
var directory = _directoryAccess.CreateDirectoryAccess(parameters);
directory.SaveFile("report.pdf", fileBytes);