Theme
Moving files in and out of an encrypted VFS with ExportFile and ImportFile
INFO
This article uses features of the Ultimate edition of Syncplify Server!, namely at rest encryption of a virtual file system (VFS).
On an encrypted VFS every file a user uploads is encrypted at rest. But what if you need to copy or move those files out of the VFS, as they arrive, for further processing? The local file system functions of SyncJS (CopyFile, MoveFile and the like) copy the file as it is on disk, that is, in its encrypted form, and no other software can read it. The same problem exists in the other direction when a file produced elsewhere must land in an encrypted VFS.
The VFS object solves both with two methods:
ExportFile(vfsFilePath, localFilePath)copies a file from the VFS to the operating system's own file system, decrypting it on the way;ImportFile(localFilePath, targetVfsFilePath)copies a file from the operating system's file system into the VFS, encrypting it on the way.
Both work on unencrypted VFSs too, where they are simply a copy through the VFS layer, which makes them the right tool for every backend, S3 or Azure included. The VFS path is always POSIX and root based (/incoming/report.csv), whatever the operating system; the local path follows the operating system (C:\Data\report.csv on Windows, with the backslashes escaped in the script).
Exporting the file a user just uploaded
Bind this script to the AfterFileUpload event handler. GetCurrentVFS() returns the VFS of the session, and CtxRelPath() the path of the file that triggered the event:
javascript
{
var vfs = GetCurrentVFS();
if (vfs != null) {
var relPath = CtxRelPath();
var fileName = relPath.substring(relPath.lastIndexOf("/") + 1);
var resp = vfs.ExportFile(relPath, "C:\\Processing\\" + fileName);
if (!resp.Ok()) {
Log("Export failed: " + resp.ErrorMsg());
}
}
}Importing a file into the VFS
The mirror image, for example in a script run on a schedule or on another event:
javascript
{
var vfs = GetCurrentVFS();
if (vfs != null) {
var resp = vfs.ImportFile("C:\\Reports\\daily.pdf", "/reports/daily.pdf");
if (!resp.Ok()) {
Log("Import failed: " + resp.ErrorMsg());
}
}
}Two things that changed in v8
If you have scripts written for earlier versions, adjust them:
GetCurrentVFS()is a standalone function; it is no longer a member of the Session object. It can returnnullwhen no VFS has been selected yet, so keep the check, although events such asAfterFileUploadonly fire once a VFS is selected.- Both methods take a full file path on each side. Earlier versions took a file path and a directory, which led to confusion; the target file name is now explicit.
The manual has the details: ExportFile, ImportFile and GetCurrentVFS.