Cloud Storage Components

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Cloud Storage Components

Previous pageReturn to chapter overviewNext page

NG-ConnectionPack provide access to several cloud storage services, which allows the user to store their files in a cloud. The following components are included:

 

TNGGDrive component provide access to Google Drive service.
TNGDropBox component provide access to Dropbox service.
TNGOneDrive component provide access to One Drive service.
TNGBoxNet component provide access to Box.NET service.

 

To use Google Drive service, please ensure that Drive API is enabled for your application, registered as described in Google Registration topic:

 

 

API interface to these services are unified in the NG-ConnectionPack, which makes really easy to support all of the services in your application. All mentioned components are descendants from TNGStorage base class, which allows to operate with TNGCloudFile objects.

 

Browsing Folders and Files

 

TNGStorage.Root property provide access to the root service's folder, where all the user files are reside. The property returns TNGCloudFile object. Such an object can represent both a folder or a file, and its IsFolder property specifies, whether the object represent a folder.

 

In later case the Files function can be used to retrieve folder's children, which are child files and folders:

 

var
  files: TArray<TNGCloudFile>;
  i:     Integer;
begin
  files := MyDropBox.Root.Files;
  for i := 0 to High(files) do
    Memo1.Lines.Add(files[i].Name);
end;

 

Files function is overloaded and the second version allows to retrieve child files and folders via inline callback procedure, which in some cases can simplify application's code:

 

MyDropBox.Root.Files(procedure(F: TNGCloudFile)
begin
 Memo1.Lines.Add(F.Name);
end);
 

Please note that its not recommended to retrieve whole folder tree at once. Generally, user interface applications should retrieve child folders and files only on demand. This way is shown in the Storage demo of NG-ConnectionPack.

 

Following there are list of properties of a TNGCloudFile:

 

Parent and HasParent properties provide access to file or folder parent folder. If the folder is a root folder then HasParent return False and it is not valid to call Parent property.
Name property returns the name of the file or folder as it is reported by the service. The name is a file name, which includes file extension.
MimeType property returns the mime-type of the file. Some services does not provide file's mime-type, so we created a small internal implementation, which determines mime-type using file extension. Independed of the service mime-type of the folder is reported as inode/directory.
IsFolder property specifies, whether TNGCloudFile object represent a file or a folder. In case of a folder, the corresponding folder management methods, such as Files, CreateFolder or CreateFile can be used; in case of a file - file related methods, such as Download and Upload can be used .
Size property returns file size in bytes. For folders it returns zero.
CreatedAt property returns file or folder creation date-time.
HasCreatedAt property specified, whether the service has provided creation date-time.
ModifiedAt property returns last modification date-time of the file or folder.
HasModifiedAt property specifies, whether the service has provided last modification date-time.

 

Creating Files and Folders

 

To create a folder inside some parent folder (including root parent folder) CreateFolder method of the corresponding parent folder object should be used. The method requires to provide a Name for a newly creating folder.

 

To create a file CreateFile method should be called analogously. There two overloaded versions of the CreateFile methods. First version requires AStream object, which will be used to read the files content while uploading:

 

var
  ms: TMemoryStream;
  i:  Integer;
begin
  ms := TMemoryStream.Create;
  try
    for i := 0 to 128 do            // Write some content.
      ms.Write(i, SizeOf(Integer)); //
 
    ms.Seek(0, soFromBeginning);
    MyDropBox.Root.CreateFile('MyNewFile.dat', ms);
  finally
    ms.Free;
  end;
end;

 

The second version has a AFileName parameter and will upload the file directly from the operating system file:

 

var
  path: string;
begin
  path := 'c:\MyFile.dat';
  MyDropBox.Root.CreateFile(ExtractFileName(path), path);
end;

 

Deleting Files and Folder

 

A file or folder can be deleted by calling Delete method of the corresponding TNGCloudFile object:

 

MyFile.Delete;

 

Usually, the folder is not required to be empty when deleting, and will be deleted with all its children. Some services provides trash capabilities, and so, in this case, deleted files or folders will be moved into trash; but this is highly service depended.

 

The root folder, of course, can't be deleted.

 

Managing File Content

 

Existing file's content can be retrieved or changed using Download and Upload methods correspondingly. As with CreateFile method, these methods are also overloaded and allows to work via standard TStream objects or via operating system files using AFileName parameter.

 

Imagine a simple text file is referred by the MyFile object. Then it can be downloaded and shown in TMemo control with the code like this:

 

var
  ms: TMemoryStream;
begin
  ms.Create;
  try
    MyFile.Download(ms);
    ms.Seek(0, soFromBeginning);
    Memo1.Lines.LoadFromStream(ms);
  finally
    ms.Free;
  end;
end;

 

For big files, this operations can take long time, which depends of service and Internet connection speed.