Uploads file to the cloud. Allows access to the files through url. Its especially usefull for Mobile/Device apps. It minimizes the App footprint on the device.
In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.
ServiceAPI api = new ServiceAPI("<API_KEY>","<SECRET_KEY>");
After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of UploadService, buildUploadService() method needs to be called.
UploadService uploadService = api.buildUploadService();
Once the API instance has been retrieved, You are ready to use the functions available for that particular API.
The methods available under a particular API will return the domain object (like Upload in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.
String name = "TextFile007";
String filePath = "Local file path";
String description = "TextFile_123";
Upload upload = uploadService.uploadFile(name,filePath,UploadFileType.IMAGE,description); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. */
The functions available under File Upload API can throw some exceptions in abnormal conditions. Example of the same has been given below.
E.g. If App developer is requesting for the files by type which is not in database, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2102" and the details as "Files for the user '<userName>' do not exist".
String userName = "Nick";
try
{
Upload upload = uploadService.getAllFilesByUser(userName);
}
catch(App42Exception ex)
{
int appErrorCode = ex.getAppErrorCode();
int httpErrorCode = ex.getHttpErrorCode();
if(appErrorCode == 2102)
{
// Handle here for Not found (Files for the user '<userName>' do not exist.)
}
else if(appErrorCode == 1401)
{
// handle here for Client is not authorized
}
else if(appErrorCode == 1500)
{
// handle here for Internal Server Error
}
String jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/
}
{
"app42Fault": {
"httpErrorCode": 404,
"appErrorCode": 2102,
"message": "Not Found",
"details": "Files for the user 'Nick' do not exist"
}
}
Below are the HTTP Error Codes and their description, the function under the File Upload API can throw.
/* 1400 - BAD REQUEST - The Request parameters are invalid 1401 - UNAUTHORIZED - Client is not authorized 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again */
Below are the Application Error Codes and their description, the function under the File Upload API can throw.
/* 2100 - BAD REQUEST - The request parameters are invalid. File by the name '<name>' already exists. 2101 - NOT FOUND - Files do not exist. 2102 - NOT FOUND - Files for the user '<userName>' do not exist. 2103 - NOT FOUND - The file with the name '<name>' does not exist. 2104 - NOT FOUND - There are no files to remove. 2105 - NOT FOUND - The file with type '<type>' does not exist. 2106 - NOT FOUND - The number of files are less than the specified offset : <offset>. 2107 - NOT FOUND - The number of files for the user '<userName>' are less than the specified offset : <offset>. 2108 - NOT FOUND - The number of files with type '<type>' are less than the specified offset : <offset>. */
Various functions available under File Upload API has been explained below.
Uploads file on the cloud.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2100 - BAD REQUEST - The request parameters are invalid. File by the name '<name>' already exists. */
String name = "TextFile007";
String filePath = "Local file path";
String description = "TextFile_123";
Upload upload = uploadService.uploadFile(name,filePath,UploadFileType.IMAGE,description); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "TextFile007",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
}
}
}
}
}
}
Gets all the files for the App.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2101 - NOT FOUND - Files do not exist. */
Upload upload = uploadService.getAllFiles(); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": [
{
"name": "TextFile01",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
},
{
"name": "TextFile007",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
}
]
}
}
}
}
}
Gets count of all the files for the App
Parameters:
Returns:
Response: App42Response Object
App42Response response = uploadService.getAllFilesCount(); /* returns the App42Response objects. */ boolean success = response.isResponseSuccess(); int totalRecords = response.getTotalRecords(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"totalRecords": 3
}
}
}
Gets all the files By Paging for the App
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2106 - NOT FOUND - The number of files are less than the specified offset : <offset>. */
int max = 1;
int offset = 0;
Upload upload = uploadService.getAllFiles(max,offset); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "FILEName123",
"userName": "Nick",
"type": "AUDIO",
"url": "http://XXXX.mp3",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "desc"
}
}
}
}
}
}
Gets the file based on file name.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2103 - NOT FOUND - The file with the name '<name>' does not exist. */
String name = "TextFile01";
Upload upload = uploadService.getFileByName(name); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "TextFile01",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
}
}
}
}
}
}
Get the files based on file type.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2105 - NOT FOUND - The file with type '<type>' does not exist. */
Upload upload = uploadService.getFilesByType(UploadFileType.IMAGE); /* returns the Upload object. */ ArrayListfileList = upload.getFileList(); for(Upload.File file : fileList) { System.out.println("File Name is " + file.getName()); System.out.println("File Type is " + file.getType()); System.out.println("File Url is " + file.getUrl()); System.out.println("File Description is " + file.getDescription()); } String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": [
{
"name": "TextFile01",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
},
{
"name": "TextFile007",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
}
]
}
}
}
}
}
Get the count of files based on file type.
Parameters:
Returns:
Response: App42Response Object
App42Response response = uploadService.getFilesCountByType(UploadFileType.AUDIO); /* returns the App42Response objects. */ boolean success = response.isResponseSuccess(); int totalRecords = response.getTotalRecords(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"totalRecords": 3
}
}
}
Get the files based on file type by Paging.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2108 - NOT FOUND - The number of files with type '<type>' are less than the specified offset : <offset>. */
int max = 1;
int offset = 0;
Upload upload = uploadService.getFilesByType(UploadFileType.AUDIO,max,offset); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "FILEName123",
"userName": "Nick",
"type": "AUDIO",
"url": "http://XXXX.mp3",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "desc"
}
}
}
}
}
}
Removes the file based on file name.
Parameters:
Returns:
Response: App42Response Object
Exception:
/*
2103 - NOT FOUND - The file with the name {name} does not Exist
*/
String name = "TextFile007"; App42Response response = uploadService.removeFileByName(name); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "TextFile007",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
}
}
}
}
}
Removes all the files for the App
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2104 - NOT FOUND - There are no files to remove. */
App42Response response = uploadService.removeAllFiles(); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": [
{
"name": "FILEName01844",
"userName": "Nick",
"type": "AUDIO",
"url": "http://XXXX.mp3",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Audio123"
},
{
"name": "FILEName01755",
"userName": "Nick",
"type": "AUDIO",
"url": "http://YYYY.mp3",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Audio123"
}
]
}
}
}
}
} Uploads file on the cloud.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2100 - BAD REQUEST - The request parameters are invalid. File by the name '<name>' already exists. */
String name = "File03";
String userName = "Nick";
String filePath = "Local file path";
String description = "TextFile_098";
Upload upload = uploadService.uploadFileForUser(name,userName,filePath,UploadFileType.IMAGE,description); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "File03",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
}
}
}
}
}
Gets the file based on Username.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2102 - NOT FOUND - Files for the user '<userName>' do not exist. 2103 - NOT FOUND - The file with the name '<name>' does not exist. */
String name = "File03";
String userName = "Nick";
Upload upload = uploadService.getFileByUser(name,userName); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "File03",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
}
}
}
}
}
Get all the files based on user name.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2102 - NOT FOUND - Files for the user '<userName>' do not exist. */
String userName = "Nick";
Upload upload = uploadService.getAllFilesByUser(userName); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": [
{
"name": "File02",
"userName": "Nick",
"type": "IMAGE",
"url": "http://XXXX.jpg",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Image_012"
},
{
"name": "File03",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
]
}
}
}
}
}
Gets the count of file based on user name.
Parameters:
Returns:
Response: App42Response Object
String userName = "Nick"; App42Response response = uploadService.getAllFilesCountByUser(userName); /* returns the App42Response objects. */ boolean success = response.isResponseSuccess(); int totalRecords = response.getTotalRecords(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"totalRecords": 3
}
}
}
Gets the file based on user name by Paging.
Parameters:
Returns:
Response: Upload Object
Exception:
/* 2107 - NOT FOUND - The number of files for the user '<userName>' are less than the specified offset : <offset>. */
String userName = "Nick";
int max = 1;
int offset = 0;
Upload upload = uploadService.getAllFilesByUser(userName,max,offset); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "FileName11",
"userName": "Nick",
"type": "IMAGE",
"url": "http://XXXX.jpg",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Image123"
}
}
}
}
}
}
Removes the file based on file name and user name.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2102 - NOT FOUND - Files for the user '<userName>' do not exist. 2103 - NOT FOUND - The file with the name '<name>' does not exist. */
String name = "File03"; String userName = "Nick"; App42Response response = uploadService.removeFileByUser(name, userName); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "File03",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
}
}
}
}
}
Removes the file based on user name.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2102 - NOT FOUND - Files for the user '<userName>' do not exist. */
userName = "Nick"; App42Response response = uploadService.removeAllFilesByUser(userName); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": [
{
"name": "File02",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Image_012"
},
{
"name": "File03",
"userName": "Billy",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "Image_013"
}
]
}
}
}
}
}
Uploads file on the cloud via Stream.
Parameters:
Returns:
Response: Upload Object
String name = "TextFile007";
String imagePath = "Local file path";
String description = "TextFile_123";
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream(imagePath);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Upload upload = uploadService.uploadFile(name,fileInputStream,UploadFileType.IMAGE,description); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "TextFile007",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_123"
}
}
}
}
}
} Uploads file for the user on the cloud via Stream.
Parameters:
Returns:
Response: Upload Object
String name = "File03";
String userName = "Nick";
String imagePath = "Local file path";
String description = "TextFile_098";
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream(imagePath);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Upload upload = uploadService.uploadFileForUser(name,userName,fileInputStream,UploadFileType.IMAGE,description); /* returns the Upload object. */
ArrayList<Upload.File> fileList = upload.getFileList();
for(Upload.File file : fileList)
{
System.out.println("File Name is " + file.getName());
System.out.println("File Type is " + file.getType());
System.out.println("File Url is " + file.getUrl());
System.out.println("File Description is " + file.getDescription());
}
String jsonResponse = upload.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"upload": {
"files": {
"file": {
"name": "File03",
"userName": "Nick",
"type": "TXT",
"url": "http://XXXX.txt",
"tinyUrl": "http://tinyurl.com/XXXX",
"description": "TextFile_098"
}
}
}
}
}
}