Complete Guide: Working with Storage in GroupDocs.Viewer Cloud
If you’re building a document management system or just need to keep tabs on your cloud storage, you’ve come to the right place. This GroupDocs.Viewer Cloud storage tutorial will walk you through everything you need to know about managing your cloud storage like a pro.
What You’ll Master in This Tutorial
By the end of this guide, you’ll confidently handle these essential storage operations:
- Check if a storage exists in GroupDocs Cloud (and why this matters)
- Verify if a file or folder exists before performing operations
- Monitor storage space usage to avoid surprises
- Retrieve file versions information for better version control
Before We Dive In
Here’s what you’ll need to get started:
- A GroupDocs Cloud account (grab a free trial here if you don’t have one)
- Your Client ID and Client Secret credentials (found in your dashboard)
- Basic understanding of REST APIs and your preferred programming language
- Development environment with the GroupDocs.Viewer Cloud SDK installed
Don’t worry if you’re new to GroupDocs – we’ll explain everything step by step with real code examples.
Why Storage Management Matters
Before jumping into the code, let’s talk about why storage management is crucial for your application. Picture this: you’re building an enterprise document management system, and suddenly your app starts throwing errors because files can’t be found, or worse, you’ve hit your storage limit without warning.
Efficient storage management prevents these headaches by letting you:
- Validate storage locations before operations
- Check file existence to avoid “file not found” errors
- Monitor usage to prevent service interruptions
- Track document versions for compliance and audit trails
Now, let’s dive into the practical implementation.
1. How to Check If Storage Exists
First things first – let’s learn how to verify that a specific storage exists in your GroupDocs Cloud account. This is especially handy when you’re working with multiple storage locations or need to validate user-provided storage names.
Why Check Storage Existence?
Think of it like knocking on a door before entering. You want to make sure the storage is there and accessible before trying to perform operations on it. This simple check can save you from cryptic error messages later.
Step-by-Step Process
Here’s how the storage existence check works:
- Authenticate with the GroupDocs.Viewer Cloud API
- Specify the storage name you want to check
- Execute the storage existence request
- Process the boolean result (true if exists, false if not)
cURL Example
curl -X GET "https://api.groupdocs.cloud/v2.0/viewer/storage/MyStorage/exist" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
SDK Examples
Let’s see how to implement this check using various programming languages:
C# Implementation
using GroupDocs.Viewer.Cloud.Sdk.Api;
using GroupDocs.Viewer.Cloud.Sdk.Client;
using GroupDocs.Viewer.Cloud.Sdk.Model.Requests;
using System;
namespace GroupDocs.Viewer.Cloud.Examples
{
class CheckStorageExistExample
{
public static void Run()
{
// Authenticate with the API
var configuration = new Configuration(Common.MyAppSid, Common.MyAppKey);
var apiInstance = new StorageApi(configuration);
try
{
// Create storage existence request
var request = new StorageExistsRequest(Common.MyStorage);
// Execute the request
var response = apiInstance.StorageExists(request);
// Process the result
Console.WriteLine($"Storage '{Common.MyStorage}' exists: {response.Exists}");
}
catch (Exception e)
{
Console.WriteLine("Error checking storage existence: " + e.Message);
}
}
}
}
Java Implementation
package examples;
import com.groupdocs.cloud.viewer.api.*;
import com.groupdocs.cloud.viewer.client.ApiException;
import com.groupdocs.cloud.viewer.model.*;
import com.groupdocs.cloud.viewer.model.requests.*;
public class CheckStorageExistExample {
public static void main(String[] args) {
// Authenticate with the API
StorageApi apiInstance = new StorageApi(Common.AppSID, Common.AppKey);
try {
// Create storage existence request
StorageExistsRequest request = new StorageExistsRequest(Common.MyStorage);
// Execute the request
StorageExist response = apiInstance.storageExists(request);
// Process the result
System.out.println("Storage '" + Common.MyStorage + "' exists: " + response.getExists());
} catch (ApiException e) {
System.err.println("Error checking storage existence: " + e.getMessage());
e.printStackTrace();
}
}
}
Python Implementation
import groupdocs_viewer_cloud
from groupdocs_viewer_cloud import StorageExistsRequest
# Authenticate with the API
configuration = groupdocs_viewer_cloud.Configuration(Common.app_sid, Common.app_key)
api_instance = groupdocs_viewer_cloud.StorageApi(configuration)
try:
# Create storage existence request
request = StorageExistsRequest(Common.my_storage)
# Execute the request
response = api_instance.storage_exists(request)
# Process the result
print(f"Storage '{Common.my_storage}' exists: {response.exists}")
except groupdocs_viewer_cloud.ApiException as e:
print(f"Error checking storage existence: {e.message}")
Try It Yourself
Here’s what you should do next:
- Replace the storage name with your actual storage name
- Run the code and verify that the check was successful
- Test with a non-existent storage name to see how the API handles it
- Implement this check in your application before performing storage operations
Pro Tip: Cache the results of storage existence checks for a few minutes to improve performance, especially if you’re checking the same storage repeatedly.
2. How to Check If File or Folder Exists
Now let’s tackle something you’ll use constantly – checking if a specific file or folder exists in your cloud storage. This is absolutely essential before performing operations like viewing, downloading, or processing documents.
Why Check File/Folder Existence?
Imagine trying to open a document that doesn’t exist – your users would get frustrated with cryptic error messages. By checking existence first, you can provide meaningful feedback like “Document not found” or “Please upload the file first.”
Step-by-Step Process
Here’s how to check if a file or folder exists:
- Authenticate with the GroupDocs.Viewer Cloud API
- Specify the path of the file or folder you want to check
- Optionally specify the storage name (uses default if not provided)
- Execute the object existence request
- Process the response with existence status and object type information
cURL Example
curl -X GET "https://api.groupdocs.cloud/v2.0/viewer/storage/exist/viewerdocs/document.docx?storageName=MyStorage" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
SDK Examples
Let’s see how to implement file/folder existence checking:
C# Implementation
using GroupDocs.Viewer.Cloud.Sdk.Api;
using GroupDocs.Viewer.Cloud.Sdk.Client;
using GroupDocs.Viewer.Cloud.Sdk.Model.Requests;
using System;
namespace GroupDocs.Viewer.Cloud.Examples
{
class CheckObjectExistsExample
{
public static void Run()
{
// Authenticate with the API
var configuration = new Configuration(Common.MyAppSid, Common.MyAppKey);
var apiInstance = new StorageApi(configuration);
try
{
// Create object existence request
var request = new ObjectExistsRequest(
"viewerdocs/document.docx", // Object path
Common.MyStorage // Storage name
);
// Execute the request
var response = apiInstance.ObjectExists(request);
// Process the result
Console.WriteLine($"Object exists: {response.Exists}");
if (response.Exists)
{
Console.WriteLine($"Is folder: {response.IsFolder}");
}
}
catch (Exception e)
{
Console.WriteLine("Error checking object existence: " + e.Message);
}
}
}
}
Java Implementation
package examples;
import com.groupdocs.cloud.viewer.api.*;
import com.groupdocs.cloud.viewer.client.ApiException;
import com.groupdocs.cloud.viewer.model.*;
import com.groupdocs.cloud.viewer.model.requests.*;
public class CheckObjectExistsExample {
public static void main(String[] args) {
// Authenticate with the API
StorageApi apiInstance = new StorageApi(Common.AppSID, Common.AppKey);
try {
// Create object existence request
ObjectExistsRequest request = new ObjectExistsRequest(
"viewerdocs/document.docx", // Object path
Common.MyStorage, // Storage name
null // Version ID (optional)
);
// Execute the request
ObjectExist response = apiInstance.objectExists(request);
// Process the result
System.out.println("Object exists: " + response.getExists());
if (response.getExists()) {
System.out.println("Is folder: " + response.getIsFolder());
}
} catch (ApiException e) {
System.err.println("Error checking object existence: " + e.getMessage());
e.printStackTrace();
}
}
}
Python Implementation
import groupdocs_viewer_cloud
from groupdocs_viewer_cloud import ObjectExistsRequest
# Authenticate with the API
configuration = groupdocs_viewer_cloud.Configuration(Common.app_sid, Common.app_key)
api_instance = groupdocs_viewer_cloud.StorageApi(configuration)
try:
# Create object existence request
request = ObjectExistsRequest(
"viewerdocs/document.docx", # Object path
Common.my_storage # Storage name
)
# Execute the request
response = api_instance.object_exists(request)
# Process the result
print(f"Object exists: {response.exists}")
if response.exists:
print(f"Is folder: {response.is_folder}")
except groupdocs_viewer_cloud.ApiException as e:
print(f"Error checking object existence: {e.message}")
Try It Yourself
Here’s your action plan:
- Replace the object path with a file or folder in your storage
- Run the code and verify the existence check was successful
- Test with both files and folders to see the different responses
- Experiment with non-existent objects to understand error handling
Real-World Tip: Use this check before any file operation to provide better user experience. For example, you could show a “File not found” message instead of a generic API error.
3. How to Monitor Storage Space Usage
Let’s talk about something that can make or break your application – monitoring storage space usage. Running out of storage space unexpectedly is like running out of gas on a highway – not fun for anyone involved.
Why Monitor Storage Usage?
Storage monitoring helps you:
- Prevent service interruptions due to storage limits
- Plan capacity for future growth
- Identify storage-heavy users or processes
- Optimize costs by understanding usage patterns
Step-by-Step Process
Here’s how to check your storage usage:
- Authenticate with the GroupDocs.Viewer Cloud API
- Optionally specify the storage name (uses default if not provided)
- Execute the disc usage request
- Process the response containing total and used storage information
cURL Example
curl -X GET "https://api.groupdocs.cloud/v2.0/viewer/storage/disc?storageName=MyStorage" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
SDK Examples
Let’s implement storage usage monitoring:
C# Implementation
using GroupDocs.Viewer.Cloud.Sdk.Api;
using GroupDocs.Viewer.Cloud.Sdk.Client;
using GroupDocs.Viewer.Cloud.Sdk.Model.Requests;
using System;
namespace GroupDocs.Viewer.Cloud.Examples
{
class GetDiscUsageExample
{
public static void Run()
{
// Authenticate with the API
var configuration = new Configuration(Common.MyAppSid, Common.MyAppKey);
var apiInstance = new StorageApi(configuration);
try
{
// Create disc usage request
var request = new GetDiscUsageRequest(Common.MyStorage);
// Execute the request
var response = apiInstance.GetDiscUsage(request);
// Process the result
Console.WriteLine($"Total space: {FormatSize(response.TotalSize)} bytes");
Console.WriteLine($"Used space: {FormatSize(response.UsedSize)} bytes");
// Calculate percentage used
var percentUsed = (double)response.UsedSize / response.TotalSize * 100;
Console.WriteLine($"Usage: {percentUsed:F2}%");
}
catch (Exception e)
{
Console.WriteLine("Error getting disc usage: " + e.Message);
}
}
// Helper method to format size for readability
private static string FormatSize(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1) {
order++;
len = len / 1024;
}
return String.Format("{0:0.##} {1}", len, sizes[order]);
}
}
}
Java Implementation
package examples;
import com.groupdocs.cloud.viewer.api.*;
import com.groupdocs.cloud.viewer.client.ApiException;
import com.groupdocs.cloud.viewer.model.*;
import com.groupdocs.cloud.viewer.model.requests.*;
import java.text.DecimalFormat;
public class GetDiscUsageExample {
public static void main(String[] args) {
// Authenticate with the API
StorageApi apiInstance = new StorageApi(Common.AppSID, Common.AppKey);
try {
// Create disc usage request
GetDiscUsageRequest request = new GetDiscUsageRequest(Common.MyStorage);
// Execute the request
DiscUsage response = apiInstance.getDiscUsage(request);
// Process the result
System.out.println("Total space: " + formatSize(response.getTotalSize()) + " bytes");
System.out.println("Used space: " + formatSize(response.getUsedSize()) + " bytes");
// Calculate percentage used
double percentUsed = (double) response.getUsedSize() / response.getTotalSize() * 100;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Usage: " + df.format(percentUsed) + "%");
} catch (ApiException e) {
System.err.println("Error getting disc usage: " + e.getMessage());
e.printStackTrace();
}
}
// Helper method to format size for readability
private static String formatSize(long bytes) {
String[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.length - 1) {
order++;
len = len / 1024;
}
DecimalFormat df = new DecimalFormat("#.##");
return df.format(len) + " " + sizes[order];
}
}
Python Implementation
import groupdocs_viewer_cloud
from groupdocs_viewer_cloud import GetDiscUsageRequest
# Authenticate with the API
configuration = groupdocs_viewer_cloud.Configuration(Common.app_sid, Common.app_key)
api_instance = groupdocs_viewer_cloud.StorageApi(configuration)
try:
# Create disc usage request
request = GetDiscUsageRequest(Common.my_storage)
# Execute the request
response = api_instance.get_disc_usage(request)
# Process the result
print(f"Total space: {format_size(response.total_size)}")
print(f"Used space: {format_size(response.used_size)}")
# Calculate percentage used
percent_used = (response.used_size / response.total_size) * 100
print(f"Usage: {percent_used:.2f}%")
except groupdocs_viewer_cloud.ApiException as e:
print(f"Error getting disc usage: {e.message}")
# Helper function to format size for readability
def format_size(bytes):
sizes = ["B", "KB", "MB", "GB", "TB"]
i = 0
while bytes >= 1024 and i < len(sizes) - 1:
bytes /= 1024
i += 1
return f"{bytes:.2f} {sizes[i]}"
Try It Yourself
Here’s what to do next:
- Run the code to check your current storage usage
- Compare the results with your account dashboard
- Implement monitoring logic that alerts when usage exceeds 80%
- Set up regular checks to track usage trends over time
Production Tip: Consider implementing automated alerts when storage usage crosses certain thresholds (like 70%, 85%, and 95%). This gives you time to react before hitting limits.
4. How to Retrieve File Versions Information
File versioning is like having a time machine for your documents. Let’s learn how to retrieve information about different versions of files in your cloud storage – essential for document management systems that need to track changes over time.
Why Track File Versions?
File version tracking helps you:
- Maintain audit trails for compliance requirements
- Restore previous versions when needed
- Track document evolution over time
- Identify the latest version of important documents
Step-by-Step Process
Here’s how to get file version information:
- Authenticate with the GroupDocs.Viewer Cloud API
- Specify the file path for which to retrieve versions
- Optionally specify the storage name
- Execute the file versions request
- Process the response containing detailed version information
cURL Example
curl -X GET "https://api.groupdocs.cloud/v2.0/viewer/storage/version/viewerdocs/document.docx?storageName=MyStorage" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
SDK Examples
Let’s implement file version retrieval:
C# Implementation
using GroupDocs.Viewer.Cloud.Sdk.Api;
using GroupDocs.Viewer.Cloud.Sdk.Client;
using GroupDocs.Viewer.Cloud.Sdk.Model.Requests;
using System;
namespace GroupDocs.Viewer.Cloud.Examples
{
class GetFileVersionsExample
{
public static void Run()
{
// Authenticate with the API
var configuration = new Configuration(Common.MyAppSid, Common.MyAppKey);
var apiInstance = new StorageApi(configuration);
try
{
// Create file versions request
var request = new GetFileVersionsRequest(
"viewerdocs/document.docx", // File path
Common.MyStorage // Storage name
);
// Execute the request
var response = apiInstance.GetFileVersions(request);
// Process the result
Console.WriteLine($"File versions count: {response.Value.Count}");
// Print version details
foreach (var version in response.Value)
{
Console.WriteLine($"Version: {version.VersionId}");
Console.WriteLine($" Is latest: {version.IsLatest}");
Console.WriteLine($" Name: {version.Name}");
Console.WriteLine($" Modified date: {version.ModifiedDate}");
Console.WriteLine($" Size: {version.Size} bytes");
Console.WriteLine($" Path: {version.Path}");
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error getting file versions: " + e.Message);
}
}
}
}
Java Implementation
package examples;
import com.groupdocs.cloud.viewer.api.*;
import com.groupdocs.cloud.viewer.client.ApiException;
import com.groupdocs.cloud.viewer.model.*;
import com.groupdocs.cloud.viewer.model.requests.*;
public class GetFileVersionsExample {
public static void main(String[] args) {
// Authenticate with the API
StorageApi apiInstance = new StorageApi(Common.AppSID, Common.AppKey);
try {
// Create file versions request
GetFileVersionsRequest request = new GetFileVersionsRequest(
"viewerdocs/document.docx", // File path
Common.MyStorage // Storage name
);
// Execute the request
FileVersions response = apiInstance.getFileVersions(request);
// Process the result
System.out.println("File versions count: " + response.getValue().size());
// Print version details
for (FileVersion version : response.getValue()) {
System.out.println("Version: " + version.getVersionId());
System.out.println(" Is latest: " + version.getIsLatest());
System.out.println(" Name: " + version.getName());
System.out.println(" Modified date: " + version.getModifiedDate());
System.out.println(" Size: " + version.getSize() + " bytes");
System.out.println(" Path: " + version.getPath());
System.out.println();
}
} catch (ApiException e) {
System.err.println("Error getting file versions: " + e.getMessage());
e.printStackTrace();
}
}
}
Python Implementation
import groupdocs_viewer_cloud
from groupdocs_viewer_cloud import GetFileVersionsRequest
# Authenticate with the API
configuration = groupdocs_viewer_cloud.Configuration(Common.app_sid, Common.app_key)
api_instance = groupdocs_viewer_cloud.StorageApi(configuration)
try:
# Create file versions request
request = GetFileVersionsRequest(
"viewerdocs/document.docx", # File path
Common.my_storage # Storage name
)
# Execute the request
response = api_instance.get_file_versions(request)
# Process the result
print(f"File versions count: {len(response.value)}")
# Print version details
for version in response.value:
print(f"Version: {version.version_id}")
print(f" Is latest: {version.is_latest}")
print(f" Name: {version.name}")
print(f" Modified date: {version.modified_date}")
print(f" Size: {version.size} bytes")
print(f" Path: {version.path}")
print()
except groupdocs_viewer_cloud.ApiException as e:
print(f"Error getting file versions: {e.message}")
Try It Yourself
Here’s your next steps:
- Replace the file path with a document in your storage
- Run the code and verify that version information is retrieved
- Test with files that have multiple versions to see the complete history
- Build version comparison or reversion features using this information
Enterprise Tip: Consider implementing a version cleanup policy that automatically removes old versions after a certain period to manage storage costs effectively.
Common Issues and Troubleshooting
When working with GroupDocs.Viewer Cloud storage operations, you might run into these common scenarios. Here’s how to handle them like a pro:
Authentication Problems
Issue: API returns 401 Unauthorized error
What’s happening: Your credentials aren’t valid or your token has expired
Solution:
- Double-check your Client ID and Client Secret in the dashboard
- Verify your token hasn’t expired (tokens typically last 24 hours)
- Make sure you’re using the correct authentication endpoint
Code Example for Token Refresh:
// C# example of handling token refresh
try
{
var response = apiInstance.StorageExists(request);
}
catch (ApiException ex) when (ex.ErrorCode == 401)
{
// Token expired, refresh and retry
configuration.RefreshToken();
var response = apiInstance.StorageExists(request);
}
Storage Not Found Errors
Issue: API returns an error when specifying a non-existent storage
What’s happening: The storage name doesn’t exist in your account
Solution:
- Verify the storage name exactly matches what’s in your dashboard
- Check for typos (storage names are case-sensitive)
- Use the default storage if you’re unsure
Permission Issues
Issue: API returns 403 Forbidden when accessing storage information
What’s happening: Your account doesn’t have the necessary permissions
Solution:
- Check your account plan limits
- Verify you have access to the specific storage
- Contact support if you believe you should have access
Rate Limiting Issues
Issue: Too many API calls result in rate limiting (429 Too Many Requests)
What’s happening: You’re making requests too quickly
Solution:
- Implement exponential backoff retry logic
- Cache storage information to reduce API calls
- Use batch operations when possible
Example Rate Limiting Handler:
import time
import random
def api_call_with_retry(api_function, max_retries=3):
for attempt in range(max_retries):
try:
return api_function()
except groupdocs_viewer_cloud.ApiException as e:
if e.status == 429 and attempt < max_retries - 1:
# Wait with exponential backoff
wait_time = (2 ** attempt) + random.uniform(0, 1)
time.sleep(wait_time)
continue
raise
Network Connectivity Issues
Issue: Intermittent connection timeouts or network errors
What’s happening: Network connectivity problems
Solution:
- Implement retry logic with exponential backoff
- Set appropriate timeout values for your network conditions
- Consider using connection pooling for better performance
File Path Issues
Issue: “File not found” errors when you know the file exists
What’s happening: Incorrect file path format or encoding issues
Solution:
- Use forward slashes (/) for path separators
- Ensure proper URL encoding for special characters
- Check that the file path is relative to the storage root
Best Practices for Production Environments
Performance Optimization
Cache Storage Information: Don’t check storage existence repeatedly. Cache the results for a reasonable time (5-10 minutes).
// Simple caching example
private static Dictionary<string, (bool exists, DateTime cached)> _storageCache = new();
public bool IsStorageExists(string storageName)
{
if (_storageCache.TryGetValue(storageName, out var cached) &&
DateTime.Now - cached.cached < TimeSpan.FromMinutes(5))
{
return cached.exists;
}
var exists = CheckStorageExistsFromAPI(storageName);
_storageCache[storageName] = (exists, DateTime.Now);
return exists;
}
Batch Operations: When checking multiple files, batch your requests when possible to reduce API calls.
Connection Pooling: Use HTTP connection pooling to improve performance for multiple requests.
Error Handling Strategy
Graceful Degradation: When storage operations fail, provide meaningful fallbacks instead of crashing.
def safe_storage_check(storage_name):
try:
return check_storage_exists(storage_name)
except Exception as e:
# Log the error but don't crash the application
logger.error(f"Storage check failed for {storage_name}: {e}")
# Return a safe default or use fallback logic
return False
User-Friendly Messages: Convert API errors into user-friendly messages.
Security Considerations
Credential Management: Never hardcode credentials in your application. Use environment variables or secure configuration management.
Token Security: Store access tokens securely and implement proper token refresh logic.
Input Validation: Always validate file paths and storage names before making API calls.
What You’ve Accomplished
Congratulations! You’ve just mastered the essential storage operations in GroupDocs.Viewer Cloud. Here’s what you can now do:
- Validate storage locations before performing operations
- Check file and folder existence to provide better user experience
- Monitor storage usage to prevent service interruptions
- Track file versions for document management and compliance
These skills form the foundation for building robust document management applications that handle storage operations gracefully.
Next Steps and Advanced Usage
Now that you’ve got the basics down, here are some advanced implementations you might want to explore:
Build a Storage Dashboard
Create a monitoring dashboard that displays:
- Real-time storage usage with visual indicators
- Storage usage trends over time
- Alerts when approaching storage limits
- File activity and version history
Implement Version Control Features
Use the file versions API to build:
- Document comparison between versions
- Automated version cleanup policies
- Version rollback functionality
- Change tracking and audit logs
Create a Storage Explorer
Build a file explorer interface that:
- Shows storage hierarchy with folders and files
- Displays file existence status before operations
- Provides search functionality across storage locations
- Offers bulk operations with existence validation
Implement Smart Storage Management
Develop intelligent storage features like:
- Automatic storage optimization recommendations
- Predictive storage usage alerts
- Smart file organization based on usage patterns
- Automated backup strategies for critical documents
Real-World Implementation Examples
Let’s look at how you might integrate these storage operations into real applications:
Document Management System Integration
public class DocumentManager
{
private readonly StorageApi _storageApi;
public async Task<bool> PrepareDocumentForViewing(string documentPath)
{
// Check if file exists before processing
var exists = await CheckFileExists(documentPath);
if (!exists)
{
throw new DocumentNotFoundException($"Document {documentPath} not found");
}
// Check storage usage before large operations
var usage = await GetStorageUsage();
if (usage.PercentUsed > 90)
{
await NotifyAdministrators("Storage usage critical");
}
return true;
}
}
Multi-Tenant Application Storage
class TenantStorageManager:
def __init__(self, tenant_id):
self.storage_name = f"tenant_{tenant_id}_storage"
def validate_tenant_setup(self):
# Ensure tenant storage exists
if not self.check_storage_exists(self.storage_name):
raise TenantSetupError(f"Storage not configured for tenant")
# Check tenant storage quota
usage = self.get_storage_usage(self.storage_name)
if usage.percent_used > 95:
raise QuotaExceededError("Tenant storage quota exceeded")
Performance Tips for High-Volume Applications
Caching Strategy
Implement intelligent caching to reduce API calls:
public class StorageCache {
private final Map<String, CachedResult> cache = new ConcurrentHashMap<>();
private final Duration cacheTimeout = Duration.ofMinutes(5);
public boolean isFileExists(String path) {
CachedResult cached = cache.get(path);
if (cached != null && !cached.isExpired(cacheTimeout)) {
return cached.exists;
}
boolean exists = checkFileExistsFromAPI(path);
cache.put(path, new CachedResult(exists, Instant.now()));
return exists;
}
}
Async Operations
Use asynchronous operations for better performance:
public async Task<StorageHealthReport> GenerateStorageReport()
{
var tasks = new List<Task>();
// Run multiple storage checks concurrently
var storageExistsTask = CheckStorageExistsAsync();
var usageTask = GetStorageUsageAsync();
var criticalFilesTask = CheckCriticalFilesAsync();
await Task.WhenAll(storageExistsTask, usageTask, criticalFilesTask);
return new StorageHealthReport
{
StorageExists = await storageExistsTask,
Usage = await usageTask,
CriticalFilesStatus = await criticalFilesTask
};
}
Troubleshooting Specific Scenarios
Scenario: Large File Upload Failures
Problem: Files fail to upload and you’re not sure if it’s a storage issue
Diagnostic Steps:
- Check storage existence first
- Verify available storage space
- Confirm file doesn’t already exist (preventing conflicts)
def diagnose_upload_failure(file_path, storage_name):
print("Diagnosing upload failure...")
# Step 1: Check storage exists
if not check_storage_exists(storage_name):
return "ERROR: Target storage doesn't exist"
# Step 2: Check available space
usage = get_storage_usage(storage_name)
if usage.percent_used > 95:
return "ERROR: Insufficient storage space"
# Step 3: Check if file already exists
if check_file_exists(file_path, storage_name):
return "WARNING: File already exists - use different name or overwrite"
return "Storage diagnostics passed - check file size and network"
Scenario: Version Control Conflicts
Problem: Multiple users editing the same document causing version conflicts
Solution: Implement version-aware operations
public class VersionAwareDocumentHandler
{
public async Task<SaveResult> SaveDocumentSafely(string path, byte[] content, string expectedVersionId)
{
// Get current versions
var versions = await GetFileVersions(path);
var latestVersion = versions.FirstOrDefault(v => v.IsLatest);
// Check for conflicts
if (latestVersion?.VersionId != expectedVersionId)
{
return SaveResult.Conflict(latestVersion.VersionId);
}
// Safe to save
return await SaveDocument(path, content);
}
}
Advanced Monitoring and Alerting
Storage Health Monitoring
class StorageHealthMonitor:
def __init__(self):
self.thresholds = {
'warning': 80, # 80% usage
'critical': 95 # 95% usage
}
def monitor_storage_health(self, storage_name):
usage = self.get_storage_usage(storage_name)
health_status = self.evaluate_health(usage)
if health_status['alert_level'] != 'ok':
self.send_alert(storage_name, health_status)
return health_status
def evaluate_health(self, usage):
percent_used = (usage.used_size / usage.total_size) * 100
if percent_used >= self.thresholds['critical']:
return {
'alert_level': 'critical',
'message': f'Storage {percent_used:.1f}% full - immediate action required'
}
elif percent_used >= self.thresholds['warning']:
return {
'alert_level': 'warning',
'message': f'Storage {percent_used:.1f}% full - monitor closely'
}
else:
return {
'alert_level': 'ok',
'message': f'Storage {percent_used:.1f}% full - healthy'
}
Automated Storage Cleanup
public class StorageCleanupService {
public void performScheduledCleanup(String storageName) {
try {
// Check if cleanup is needed
DiscUsage usage = getStorageUsage(storageName);
double percentUsed = (double) usage.getUsedSize() / usage.getTotalSize() * 100;
if (percentUsed > 85) {
// Clean up old file versions
cleanupOldVersions(storageName);
// Remove temporary files
cleanupTempFiles(storageName);
// Archive old documents
archiveOldDocuments(storageName);
}
} catch (Exception e) {
logger.error("Cleanup failed for storage: " + storageName, e);
}
}
}
Testing Your Storage Operations
Unit Testing Examples
[Test]
public async Task StorageExists_WithValidStorage_ReturnsTrue()
{
// Arrange
var mockApi = new Mock<IStorageApi>();
mockApi.Setup(x => x.StorageExists(It.IsAny<StorageExistsRequest>()))
.ReturnsAsync(new StorageExist { Exists = true });
var service = new StorageService(mockApi.Object);
// Act
var result = await service.CheckStorageExists("valid-storage");
// Assert
Assert.IsTrue(result);
}
[Test]
public async Task GetStorageUsage_WhenStorageNearCapacity_LogsWarning()
{
// Arrange
var mockApi = new Mock<IStorageApi>();
mockApi.Setup(x => x.GetDiscUsage(It.IsAny<GetDiscUsageRequest>()))
.ReturnsAsync(new DiscUsage { TotalSize = 1000, UsedSize = 850 }); // 85% used
var mockLogger = new Mock<ILogger>();
var service = new StorageService(mockApi.Object, mockLogger.Object);
// Act
await service.MonitorStorageUsage("test-storage");
// Assert
mockLogger.Verify(x => x.LogWarning(It.IsAny<string>()), Times.Once);
}
Integration Testing
import unittest
from unittest.mock import patch
class StorageIntegrationTests(unittest.TestCase):
def setUp(self):
self.storage_service = StorageService(test_config)
@patch('groupdocs_viewer_cloud.StorageApi.storage_exists')
def test_storage_exists_integration(self, mock_storage_exists):
# Arrange
mock_storage_exists.return_value.exists = True
# Act
result = self.storage_service.check_storage_exists("test-storage")
# Assert
self.assertTrue(result)
mock_storage_exists.assert_called_once()
def test_file_exists_with_real_api(self):
# This test uses real API - only run in integration environment
if not self.is_integration_environment():
self.skipTest("Skipping integration test")
# Test with known file in test storage
result = self.storage_service.check_file_exists("test-files/sample.pdf")
self.assertTrue(result)
Further Practice Challenges
Ready to level up your skills? Try these hands-on challenges:
Beginner Challenges
- Storage Dashboard: Create a simple web page that displays storage usage with a progress bar
- File Validator: Build a utility that checks if a list of files exists before starting a batch operation
- Storage Reporter: Generate a daily report showing storage usage trends
Intermediate Challenges
- Multi-Storage Manager: Build a service that manages multiple storage locations with load balancing
- Version Cleaner: Create an automated service that removes old file versions based on policies
- Storage Migration Tool: Build a tool that safely migrates files between storage locations
Advanced Challenges
- Predictive Storage Analytics: Implement machine learning to predict when storage will be full
- Distributed Storage Coordinator: Build a system that coordinates storage across multiple regions
- Real-time Storage Monitor: Create a real-time monitoring system with WebSocket updates
Get Help and Stay Connected
Need assistance or want to share your storage management solutions?
- **Product Page ** - Overview and features
- **Documentation ** - Complete API reference
- **Live Demo ** - Try it without coding
- **API Reference ** - Interactive API explorer
- **Blog ** - Latest tips and tutorials
- **Support Forum ** - Get help from the community
- **Free Trial ** - Start building today