Connect to storage

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 driveUNC path เช่น \\server\share

FileServer

FieldDescription
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 เช่น DefaultNetworkDriveEx

FileSource

FieldRequiredDescription
RemotePathRequiredPath ของ file source ถ้าเป็น local folder ใช้ชื่อ folder ถ้าเป็น network drive ใช้ UNC path เช่น \\server\share
UsernameOptionalชื่อผู้ใช้สำหรับ authentication ใช้เฉพาะ network drive
PasswordOptionalรหัสผ่านสำหรับ authentication ใช้เฉพาะ network drive
DomainOptionalDomain สำหรับ 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);