Skip to content

Files.com Official Java SDK. Files.com is Cloud Storage, Cloud Gateway, and MFT, All In One. Our built-in storage is fast, affordable, and available in 7 Worldwide Regions. Access Any File on Any Cloud via Files.com's Java client, including Google, Amazon, Microsoft, Box, Dropbox, Amazon, Wasabi, Backblaze, Rackspace, and more.

License

Notifications You must be signed in to change notification settings

Files-com/files-sdk-java

Repository files navigation

Files.com Java Client

The content included here should be enough to get started, but please visit our Developer Documentation Website for the complete documentation.

Introduction

The Files.com Java client library provides convenient access to the Files.com API from JVM based applications.

Installation

Maven

A maven jar is available through maven-central. To use the package add the following to your pom.xml file.

<dependency>
    <groupId>com.files</groupId>
    <artifactId>files-sdk</artifactId>
</dependency>

Gradle

To add the dependency to your Gradle project add this to your

compile group: 'com.files', name: 'files-sdk'

Requirements

The Files.com Java SDK supports all versions of Java beginning with Java 8 (Also known as 1.8).

Explore the files-sdk-java code on GitHub.

Getting Support

The Files.com Support team provides official support for all of our official Files.com integration tools.

To initiate a support conversation, you can send an Authenticated Support Request or simply send an E-Mail to [email protected].

Authentication

Authenticate with an API Key

Authenticating with an API key is the recommended authentication method for most scenarios, and is the method used in the examples on this site.

To use the API or SDKs with an API Key, first generate an API key from the web interface or via the API or an SDK.

Note that when using a user-specific API key, if the user is an administrator, you will have full access to the entire API. If the user is not an administrator, you will only be able to access files that user can access, and no access will be granted to site administration functions in the API.

FilesClient.apiKey = "YOUR_API_KEY";
// Alternatively, you can specify the API key on a per-object
// basis in options HashMap to a model constructor.

HashMap<String, Object> requestOptions = new HashMap<>();
requestOptions.put("api_key", "my-key");
User user = new User(params, requestOptions);

// You may also specify the API key on a per-request basis in
// in the final parameter to static methods.
HashMap<String, Object> requestOptions = new HashMap<>();
requestOptions.put("api_key", "my-key");
User.find(id, params, requestOptions);

Don't forget to replace the placeholder, YOUR_API_KEY, with your actual API key.

Authenticate with a Session

You can also authenticate to the REST API or SDKs by creating a user session using the username and password of an active user. If the user is an administrator, the session will have full access to the entire API. Sessions created from regular user accounts will only be able to access files that user can access, and no access will be granted to site administration functions.

API sessions use the exact same session timeout settings as web interface sessions. When an API session times out, simply create a new session and resume where you left off. This process is not automatically handled by SDKs because we do not want to store password information in memory without your explicit consent.

Logging In

To create a session, the create method is called on the Session object with the user's username and password.

This returns a session object that can be used to authenticate SDK method calls.

HashMap<String, Object> sessionParameters = new HashMap<>()
sessionParameters.put("username", "username");
sessionParameters.put("password", "password");
Session session = Session.create(parameters)

Using a Session

Once a session has been created, you can store the session globally, use the session per object, or use the session per request to authenticate SDK operations.

// You may set the returned session to be used by default for subsequent requests.
FilesClient.session = session;

// Alternatively, you can specify the session ID on a per-object basis
// in the second parameter to a model constructor.
HashMap<String, Object> requestOptions = new HashMap<>();
requestOptions.put("session_id", session.getId());
user = new User(params, requestOptions);

// You may also specify the session ID on a per-request basis in the final parameter to static methods.
HashMap<String, Object> requestOptions = new HashMap<>();
requestOptions.put("session_id", session.getId());
User.find(id, params, requestOptions);

Logging Out

User sessions can be ended calling the destroy method on the session object.

session.destroy()

Configuration

Configuration Options

Base URL

Setting the base URL for the API is required if your site is configured to disable global acceleration. This can also be set to use a mock server in development or CI.

FilesClient.setProperty("apiRoot", "https://MY-SUBDOMAIN.files.com");

Logging

The Files.com SDK is compatible with the standard log4j logging scheme.

Add com.files logger to your Loggers root in the log4j2.xml file.

<Loggers>
    <!-- set preferred level -->
    <Logger name="com.files" level="TRACE" />
    <!-- to enable network request -->
    <Logger name="okhttp3.logging.wire" level="INFO"/>
</Loggers>

Create a resources/log4j2.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <!-- set preferred level -->
        <Logger name="com.files" level="TRACE"/>
        <!-- to enable network request -->
        <Logger name="okhttp3.logging.wire" level="INFO"/>
    </Loggers>
</Configuration>

You can read more about log4j2 configuration.

Sort and Filter

Several of the Files.com API resources have list operations that return multiple instances of the resource. The List operations can be sorted and filtered.

Sorting

The returned data can be sorted by passing in the sort_by method argument.

Each resource has a set of valid fields for sorting and can be sorted by one field at a time.

The argument value is a Java HashMap<String, Object> object that has a property of the resource field name sort on and a value of either "asc" or "desc" to specify the sort order.

Filters

Filters apply selection criteria to the underlying query that returns the results. Filters can be applied individually to select resource fields and/or in a combination with each other. The results of applying filters and filter combinations can be sorted by a single field.

The passed in argument value is a Java HashMap<String, Object> object that has a key of the resource field name to filter on and a passed in value to use in the filter comparison.

Each resource has their own set of valid filters and fields, valid combinations of filters, and sortable fields.

Types of Filters

Exact Filter

filter - find resources that have an exact field value match to a passed in value. (i.e., FIELD_VALUE = PASS_IN_VALUE).

Range Filters

filter_gt - find resources that have a field value that is greater than the passed in value. (i.e., FIELD_VALUE > PASS_IN_VALUE).

filter_gte - find resources that have a field value that is greater than or equal to the passed in value. (i.e., FIELD_VALUE >= PASS_IN_VALUE).

filter_lt - find resources that have a field value that is less than the passed in value. (i.e., FIELD_VALUE < PASS_IN_VALUE).

filter_lte - find resources that have a field value that is less than or equal to the passed in value. (i.e., FIELD_VALUE <= PASS_IN_VALUE).

Pattern Filter

filter_prefix - find resources where the specified field is prefixed by the supplied value. This is applicable to values that are strings.

// users sorted by username
FilesClient.apiKey = "my-key";
HashMap<String, Object> args = new HashMap<>();
HashMap<String, Object> sortArgs = new HashMap<>();
sortArgs.put("username", "asc");
args.put("sort_by", sortArgs);

ListIterator<User> users = User.list(args);
for (User user : users.listAutoPaging()) {
  // Operate on user
  System.out.println(user.username);
}
// non admin users
FilesClient.apiKey = "my-key";
HashMap<String, Object> args = new HashMap<>();
HashMap<String, Object> filterArgs = new HashMap<>();
HashMap<String, Object> sortArgs = new HashMap<>();
filterArgs.put("not_site_admin", true);
sortArgs.put("username", "asc");
args.put("filter", filterArgs);
args.put("sort_by", sortArgs);

ListIterator<User> users = User.list(args);
for (User user : users.listAutoPaging()) {
  // Operate on user
  System.out.println(user.username);
}
// users who haven't logged in since 2024-01-01
FilesClient.apiKey = "my-key";
HashMap<String, Object> args = new HashMap<>();
HashMap<String, Object> filterArgs = new HashMap<>();
HashMap<String, Object> sortArgs = new HashMap<>();
filterArgs.put("last_login_at", "2024-01-01");
sortArgs.put("last_login_at", "asc");
args.put("filter_gte", filterArgs);
args.put("sort_by", sortArgs);

ListIterator<User> users = User.list(args);
for (User user : users.listAutoPaging()) {
  // Operate on user
  System.out.println(user.username);
}
// users who usernames start with 'test'
FilesClient.apiKey = "my-key";
HashMap<String, Object> args = new HashMap<>();
HashMap<String, Object> filterArgs = new HashMap<>();
HashMap<String, Object> sortArgs = new HashMap<>();
filterArgs.put("username", "test");
sortArgs.put("last_login_at", "asc");
args.put("filter_prefix", filterArgs);
args.put("sort_by", sortArgs);

ListIterator<User> users = User.list(args);
for (User user : users.listAutoPaging()) {
  // Operate on user
  System.out.println(user.username);
}
// users who usernames start with 'test' and are not admins
FilesClient.apiKey = "my-key";
HashMap<String, Object> args = new HashMap<>();
HashMap<String, Object> filterPrefixArgs = new HashMap<>();
HashMap<String, Object> filterArgs = new HashMap<>();
HashMap<String, Object> sortArgs = new HashMap<>();
filterPrefixArgs.put("username", "test");
filterArgs.put("not_site_admin", true);
sortArgs.put("last_login_at", "asc");
args.put("filter_prefix", filterPrefixArgs);
args.put("filter", filterArgs);
args.put("sort_by", sortArgs);

ListIterator<User> users = User.list(args);
for (User user : users.listAutoPaging()) {
  // Operate on user
  System.out.println(user.username);
}

Errors

The Files.com Java SDK will return errors by raising exceptions. There are many exception classes defined in the Files SDK that correspond to specific errors.

The raised exceptions come from two categories:

  1. SDK Exceptions - errors that originate within the SDK
  2. API Exceptions - errors that occur due to the response from the Files.com API. These errors are grouped into common error types.

There are several types of exceptions within each category. Exception classes indicate different types of errors and are named in a fashion that describe the general premise of the originating error. More details can be found in the exception object message using the getMessage() method.

Use standard Java exception handling to detect and deal with errors. It is generally recommended to catch specific errors first, then catch the general SdkException exception as a catch-all.

package com.filescom.app;
import java.util.HashMap;

import com.files.models.*;
import com.files.exceptions.*;
import com.files.exceptions.ApiErrorException.*;

public class App
{
    public static void main( String[] args )
    {
        HashMap<String, Object> sessionParameters = new HashMap<>();
        sessionParameters.put("username", "USERNAME");
        sessionParameters.put("password", "BADPASSWORD");

        try{
            Session session = Session.create(sessionParameters);
        }
        catch(NotAuthenticatedException e){
            System.out.println("Authentication Error Occurred (" + e.getClass().getName() +"): " + e.getMessage());
        }
        catch(SdkException e){
            System.out.println("Unknown Error Occurred (" + e.getClass().getName() +"): " + e.getMessage());
        }

        System.out.println( "The End." );
    }
}

Error Types

SDK Errors

SDK errors are general errors that occur within the SDK code. These errors generate exceptions. Each of these exception classes inherit from a standard SdkException base class.

com.files.exceptions.ApiErrorException.ApiConnectionException ->
com.files.exceptions.SdkException ->
RuntimeException
SDK Exception Classes
Exception Class Name Description
ApiConnectionException The Files.com API cannot be reached
AuthenticationException Authentication Failure on the Files.com API
InvalidParameterException A passed in parameter is invalid
InvalidResponseException A bad formed response came back from the API
ServerErrorException The API service responded with a bad response (ie, 5xx)

API Errors

API errors are errors returned by the Files.com API. Each exception class inherits from an error group base class. The error group base class indicates a particular type of error.

com.files.exceptions.ApiErrorException.FolderAdminPermissionRequiredException ->
com.files.exceptions.ApiErrorException.NotAuthorizedException ->
com.files.exceptions.ApiErrorException ->
com.files.exceptions.SdkException ->
RuntimeException
API Exception Classes
Exception Class Name Error Group
AgentUpgradeRequiredException BadRequestException
AttachmentTooLargeException BadRequestException
CannotDownloadDirectoryException BadRequestException
CantMoveWithMultipleLocationsException BadRequestException
DatetimeParseException BadRequestException
DestinationSameException BadRequestException
FolderMustNotBeAFileException BadRequestException
InvalidBodyException BadRequestException
InvalidCursorException BadRequestException
InvalidCursorTypeForSortException BadRequestException
InvalidEtagsException BadRequestException
InvalidFilterAliasCombinationException BadRequestException
InvalidFilterCombinationException BadRequestException
InvalidFilterFieldException BadRequestException
InvalidFilterParamException BadRequestException
InvalidFilterParamValueException BadRequestException
InvalidInputEncodingException BadRequestException
InvalidInterfaceException BadRequestException
InvalidOauthProviderException BadRequestException
InvalidPathException BadRequestException
InvalidReturnToUrlException BadRequestException
InvalidUploadOffsetException BadRequestException
InvalidUploadPartGapException BadRequestException
InvalidUploadPartSizeException BadRequestException
MethodNotAllowedException BadRequestException
NoValidInputParamsException BadRequestException
PartNumberTooLargeException BadRequestException
PathCannotHaveTrailingWhitespaceException BadRequestException
ReauthenticationNeededFieldsException BadRequestException
RequestParamsContainInvalidCharacterException BadRequestException
RequestParamsInvalidException BadRequestException
RequestParamsRequiredException BadRequestException
SearchAllOnChildPathException BadRequestException
UnsupportedCurrencyException BadRequestException
UnsupportedHttpResponseFormatException BadRequestException
UnsupportedMediaTypeException BadRequestException
UserIdInvalidException BadRequestException
UserIdOnUserEndpointException BadRequestException
UserRequiredException BadRequestException
AdditionalAuthenticationRequiredException NotAuthenticatedException
AuthenticationRequiredException NotAuthenticatedException
BundleRegistrationCodeFailedException NotAuthenticatedException
FilesAgentTokenFailedException NotAuthenticatedException
InboxRegistrationCodeFailedException NotAuthenticatedException
InvalidCredentialsException NotAuthenticatedException
InvalidOauthException NotAuthenticatedException
InvalidOrExpiredCodeException NotAuthenticatedException
InvalidSessionException NotAuthenticatedException
InvalidUsernameOrPasswordException NotAuthenticatedException
LockedOutException NotAuthenticatedException
LockoutRegionMismatchException NotAuthenticatedException
OneTimePasswordIncorrectException NotAuthenticatedException
TwoFactorAuthenticationErrorException NotAuthenticatedException
TwoFactorAuthenticationSetupExpiredException NotAuthenticatedException
ApiKeyIsDisabledException NotAuthorizedException
ApiKeyIsPathRestrictedException NotAuthorizedException
ApiKeyOnlyForDesktopAppException NotAuthorizedException
ApiKeyOnlyForMobileAppException NotAuthorizedException
ApiKeyOnlyForOfficeIntegrationException NotAuthorizedException
BillingOrSiteAdminPermissionRequiredException NotAuthorizedException
BillingPermissionRequiredException NotAuthorizedException
BundleMaximumUsesReachedException NotAuthorizedException
CannotLoginWhileUsingKeyException NotAuthorizedException
CantActForOtherUserException NotAuthorizedException
ContactAdminForPasswordChangeHelpException NotAuthorizedException
FilesAgentFailedAuthorizationException NotAuthorizedException
FolderAdminOrBillingPermissionRequiredException NotAuthorizedException
FolderAdminPermissionRequiredException NotAuthorizedException
FullPermissionRequiredException NotAuthorizedException
HistoryPermissionRequiredException NotAuthorizedException
InsufficientPermissionForParamsException NotAuthorizedException
InsufficientPermissionForSiteException NotAuthorizedException
MustAuthenticateWithApiKeyException NotAuthorizedException
NeedAdminPermissionForInboxException NotAuthorizedException
NonAdminsMustQueryByFolderOrPathException NotAuthorizedException
NotAllowedToCreateBundleException NotAuthorizedException
PasswordChangeNotRequiredException NotAuthorizedException
PasswordChangeRequiredException NotAuthorizedException
ReadOnlySessionException NotAuthorizedException
ReadPermissionRequiredException NotAuthorizedException
ReauthenticationFailedException NotAuthorizedException
ReauthenticationFailedFinalException NotAuthorizedException
ReauthenticationNeededActionException NotAuthorizedException
RecaptchaFailedException NotAuthorizedException
SelfManagedRequiredException NotAuthorizedException
SiteAdminRequiredException NotAuthorizedException
SiteFilesAreImmutableException NotAuthorizedException
TwoFactorAuthenticationRequiredException NotAuthorizedException
UserIdWithoutSiteAdminException NotAuthorizedException
WriteAndBundlePermissionRequiredException NotAuthorizedException
WritePermissionRequiredException NotAuthorizedException
ZipDownloadIpMismatchException NotAuthorizedException
ApiKeyNotFoundException NotFoundException
BundlePathNotFoundException NotFoundException
BundleRegistrationNotFoundException NotFoundException
CodeNotFoundException NotFoundException
FileNotFoundException NotFoundException
FileUploadNotFoundException NotFoundException
FolderNotFoundException NotFoundException
GroupNotFoundException NotFoundException
InboxNotFoundException NotFoundException
NestedNotFoundException NotFoundException
PlanNotFoundException NotFoundException
SiteNotFoundException NotFoundException
UserNotFoundException NotFoundException
AlreadyCompletedException ProcessingFailureException
AutomationCannotBeRunManuallyException ProcessingFailureException
BehaviorNotAllowedOnRemoteServerException ProcessingFailureException
BundleOnlyAllowsPreviewsException ProcessingFailureException
BundleOperationRequiresSubfolderException ProcessingFailureException
CouldNotCreateParentException ProcessingFailureException
DestinationExistsException ProcessingFailureException
DestinationFolderLimitedException ProcessingFailureException
DestinationParentConflictException ProcessingFailureException
DestinationParentDoesNotExistException ProcessingFailureException
ExpiredPrivateKeyException ProcessingFailureException
ExpiredPublicKeyException ProcessingFailureException
ExportFailureException ProcessingFailureException
ExportNotReadyException ProcessingFailureException
FailedToChangePasswordException ProcessingFailureException
FileLockedException ProcessingFailureException
FileNotUploadedException ProcessingFailureException
FilePendingProcessingException ProcessingFailureException
FileProcessingErrorException ProcessingFailureException
FileTooBigToDecryptException ProcessingFailureException
FileTooBigToEncryptException ProcessingFailureException
FileUploadedToWrongRegionException ProcessingFailureException
FilenameTooLongException ProcessingFailureException
FolderLockedException ProcessingFailureException
FolderNotEmptyException ProcessingFailureException
HistoryUnavailableException ProcessingFailureException
InvalidBundleCodeException ProcessingFailureException
InvalidFileTypeException ProcessingFailureException
InvalidFilenameException ProcessingFailureException
InvalidPriorityColorException ProcessingFailureException
InvalidRangeException ProcessingFailureException
ModelSaveErrorException ProcessingFailureException
MultipleProcessingErrorsException ProcessingFailureException
PathTooLongException ProcessingFailureException
RecipientAlreadySharedException ProcessingFailureException
RemoteServerErrorException ProcessingFailureException
ResourceLockedException ProcessingFailureException
SubfolderLockedException ProcessingFailureException
TwoFactorAuthenticationCodeAlreadySentException ProcessingFailureException
TwoFactorAuthenticationCountryBlacklistedException ProcessingFailureException
TwoFactorAuthenticationGeneralErrorException ProcessingFailureException
TwoFactorAuthenticationMethodUnsupportedErrorException ProcessingFailureException
TwoFactorAuthenticationUnsubscribedRecipientException ProcessingFailureException
UpdatesNotAllowedForRemotesException ProcessingFailureException
DuplicateShareRecipientException RateLimitedException
ReauthenticationRateLimitedException RateLimitedException
TooManyConcurrentLoginsException RateLimitedException
TooManyConcurrentRequestsException RateLimitedException
TooManyLoginAttemptsException RateLimitedException
TooManyRequestsException RateLimitedException
TooManySharesException RateLimitedException
AgentUnavailableException ServiceUnavailableException
AutomationsUnavailableException ServiceUnavailableException
MigrationInProgressException ServiceUnavailableException
SiteDisabledException ServiceUnavailableException
UploadsUnavailableException ServiceUnavailableException
AccountAlreadyExistsException SiteConfigurationException
AccountOverdueException SiteConfigurationException
NoAccountForSiteException SiteConfigurationException
SiteWasRemovedException SiteConfigurationException
TrialExpiredException SiteConfigurationException
TrialLockedException SiteConfigurationException
UserRequestsEnabledRequiredException SiteConfigurationException

Examples

File Operations

List Root Folder (loads all pages into memory)

Folder.listFor("/", null).all()

List Root Folder with Auto Pagination (loads each page into memory)

for (Folder item : Folder.listFor("/", null).listAutoPaging()) {
    System.out.println(item.path);
}

List Root Folder with Manual Pagination (loads each page into memory)

ListIterator<Folder> listing = Folder.listFor("/", null);
do {
    for (Folder item : listing.loadNextPage()) {
        System.out.println(item.path);
    }
} while (listing.hasNextPage());

Writing a File

// Will upload a file called "test.txt" and print its size
File transferred = File.create("test.txt", null).putLocalFile("test.txt");
System.out.println("TransferredSize:" + transferred.getSize());

If the parent directories do not already exist, they can be automatically created by passing mkdir_parents in the args.

HashMap<String, Object> args = new HashMap<>();
args.put("mkdir_parents", true);
File.create("uploads/test.txt", args).putLocalFile("test.txt");

Passing an InputStream to putInputStream will upload the file in chunks.

// Upload will be parallelized if the destination supports it
java.io.File file = new java.io.File("test.txt");
File.create("test.txt", null).putInputStream(new FileInputStream(file), null);

Reading a File's Text as a InputStream

File file = File.download("test.txt", null);
try(InputStream inputStream = file.getInputStream()) {
    String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
        .lines()
        .collect(Collectors.joining("\n"));
    String fileContents = text;
}

Reading a File and Writing it to Your Local Drive

File file = File.download("test.txt", null);
file.saveAsLocalFile("/tmp/");

Miscellaneous

Comparing Case-Insensitive Files and Paths

For related documentation see Case Sensitivity Documentation.

if (PathUtils.isSame("Fïłèńämê.Txt", "filename.txt")) {
    System.out.println("Paths are the same");
}

Mock Server

Files.com publishes a Files.com API server, which is useful for testing your use of the Files.com SDKs and other direct integrations against the Files.com API in an integration test environment.

It is a Ruby app that operates as a minimal server for the purpose of testing basic network operations and JSON encoding for your SDK or API client. It does not maintain state and it does not deeply inspect your submissions for correctness.

Eventually we will add more features intended for integration testing, such as the ability to intentionally provoke errors.

Download the server as a Docker image via Docker Hub.

The Source Code is also available on GitHub.

A README is available on the GitHub link.

About

Files.com Official Java SDK. Files.com is Cloud Storage, Cloud Gateway, and MFT, All In One. Our built-in storage is fast, affordable, and available in 7 Worldwide Regions. Access Any File on Any Cloud via Files.com's Java client, including Google, Amazon, Microsoft, Box, Dropbox, Amazon, Wasabi, Backblaze, Rackspace, and more.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published