Tutorial: Working with Files in GroupDocs.Editor Cloud

Introduction

In this tutorial, you’ll learn how to perform essential file operations using the GroupDocs.Editor Cloud API. Managing individual files efficiently is a critical skill when working with document editing workflows in the cloud.

  • Download files from cloud storage to your application
  • Upload new files to your cloud storage
  • Delete files when they’re no longer needed
  • Copy files between different locations
  • Move files within your cloud storage structure

Prerequisites

Before starting this tutorial, make sure you have:

  1. A GroupDocs.Editor Cloud subscription (or free trial)
  2. Your Client ID and Client Secret credentials
  3. Basic understanding of REST APIs and cloud storage concepts
  4. Development environment with your preferred programming language (C#, Java, PHP, Ruby, Node.js, or Python)
  5. Corresponding GroupDocs.Editor Cloud SDK installed

Use Case Scenario

Consider a document collaboration platform where users need to upload documents for editing, download edited versions, and manage their document lifecycle. You need to implement robust file handling operations to enable seamless document workflows.

Tutorial Steps

Step 1: Set Up Authentication

Before using any file operations, you need to authenticate with the GroupDocs.Editor Cloud API:

Python Setup

import groupdocs_editor_cloud
from groupdocs_editor_cloud import Configuration

# Create authentication object with your credentials
configuration = Configuration(client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET")

C# Setup

using GroupDocs.Editor.Cloud.Sdk.Api;
using GroupDocs.Editor.Cloud.Sdk.Client;

// Create authentication object
Configuration configuration = new Configuration(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET");

Step 2: Downloading Files from Cloud Storage

Downloading files is a fundamental operation when you need to retrieve documents from cloud storage for viewing, processing, or local editing.

cURL Example

curl -X GET "https://api.groupdocs.cloud/v1.0/editor/storage/file/documents/sample.docx?storageName=MyStorage" -H "accept: multipart/form-data" -H "authorization: Bearer YOUR_ACCESS_TOKEN"

SDK Examples

C# Example
// Create FileApi instance
var fileApi = new FileApi(configuration);

// Download file
var request = new DownloadFileRequest("documents/sample.docx", "MyStorage");
var response = fileApi.DownloadFile(request);

// Save the file to local disk
using (var fileStream = System.IO.File.Create("local-sample.docx"))
{
    response.CopyTo(fileStream);
}

Console.WriteLine("File downloaded successfully to 'local-sample.docx'");
Python Example
# Create FileApi instance
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# Download file
request = groupdocs_editor_cloud.DownloadFileRequest("documents/sample.docx", "MyStorage")
response = file_api.download_file(request)

# Save the file to local disk
with open("local-sample.docx", 'wb') as f:
    for chunk in response:
        f.write(chunk)

print("File downloaded successfully to 'local-sample.docx'")
Java Example
// Create FileApi instance
FileApi fileApi = new FileApi(configuration);

// Download file
DownloadFileRequest request = new DownloadFileRequest("documents/sample.docx", "MyStorage");
File response = fileApi.downloadFile(request);

System.out.println("File downloaded successfully: " + response.getPath());

Step 3: Uploading Files to Cloud Storage

Uploading files allows you to add new documents to your cloud storage, which can later be edited using GroupDocs.Editor.

cURL Example

curl -X POST "https://api.groupdocs.cloud/v1.0/editor/storage/file/documents/new-document.docx?storageName=MyStorage" -H "accept: application/json" -H "authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type: multipart/form-data" -F "file=@local-document.docx"

SDK Examples

C# Example
// Create FileApi instance
var fileApi = new FileApi(configuration);

// Upload file
var request = new UploadFileRequest("documents/new-document.docx", new System.IO.FileStream("local-document.docx", System.IO.FileMode.Open), "MyStorage");
var response = fileApi.UploadFile(request);

// Print results
Console.WriteLine($"File uploaded successfully! Uploaded to: {response.Uploaded[0]}");
Python Example
# Create FileApi instance
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# Upload file
request = groupdocs_editor_cloud.UploadFileRequest("documents/new-document.docx", "local-document.docx", "MyStorage")
response = file_api.upload_file(request)

# Print results
print(f"File uploaded successfully! Uploaded to: {response.uploaded[0]}")
Java Example
// Create FileApi instance
FileApi fileApi = new FileApi(configuration);

// Upload file
java.io.File file = new java.io.File("local-document.docx");
UploadFileRequest request = new UploadFileRequest("documents/new-document.docx", file, "MyStorage");
FilesUploadResult response = fileApi.uploadFile(request);

// Print results
System.out.println("File uploaded successfully! Uploaded to: " + response.getUploaded().get(0));

Step 4: Deleting Files from Cloud Storage

Deleting files helps manage your storage space and remove documents that are no longer needed.

cURL Example

curl -X DELETE "https://api.groupdocs.cloud/v1.0/editor/storage/file/documents/old-version.docx?storageName=MyStorage" -H "accept: application/json" -H "authorization: Bearer YOUR_ACCESS_TOKEN"

SDK Examples

C# Example
// Create FileApi instance
var fileApi = new FileApi(configuration);

// Delete file
var request = new DeleteFileRequest("documents/old-version.docx", "MyStorage");
fileApi.DeleteFile(request);

Console.WriteLine("File deleted successfully");
Python Example
# Create FileApi instance
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# Delete file
request = groupdocs_editor_cloud.DeleteFileRequest("documents/old-version.docx", "MyStorage")
file_api.delete_file(request)

print("File deleted successfully")
Java Example
// Create FileApi instance
FileApi fileApi = new FileApi(configuration);

// Delete file
DeleteFileRequest request = new DeleteFileRequest("documents/old-version.docx", "MyStorage");
fileApi.deleteFile(request);

System.out.println("File deleted successfully");

Step 5: Copying Files in Cloud Storage

Copying files allows you to duplicate documents within your storage, which is useful for creating backups or variations of documents.

cURL Example

curl -X PUT "https://api.groupdocs.cloud/v1.0/editor/storage/file/copy/documents/original.docx?destPath=documents/archive/original.docx&storageName=MyStorage" -H "accept: application/json" -H "authorization: Bearer YOUR_ACCESS_TOKEN"

SDK Examples

C# Example
// Create FileApi instance
var fileApi = new FileApi(configuration);

// Copy file
var request = new CopyFileRequest(
    srcPath: "documents/original.docx",
    destPath: "documents/archive/original.docx",
    srcStorageName: "MyStorage",
    destStorageName: "MyStorage"
);
fileApi.CopyFile(request);

Console.WriteLine("File copied successfully");
Python Example
# Create FileApi instance
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# Copy file
request = groupdocs_editor_cloud.CopyFileRequest(
    src_path="documents/original.docx",
    dest_path="documents/archive/original.docx",
    src_storage_name="MyStorage",
    dest_storage_name="MyStorage"
)
file_api.copy_file(request)

print("File copied successfully")
Java Example
// Create FileApi instance
FileApi fileApi = new FileApi(configuration);

// Copy file
CopyFileRequest request = new CopyFileRequest(
    "documents/original.docx",
    "documents/archive/original.docx",
    "MyStorage",
    "MyStorage"
);
fileApi.copyFile(request);

System.out.println("File copied successfully");

Step 6: Moving Files in Cloud Storage

Moving files allows you to reorganize your documents by relocating them to different folders within your storage structure.

cURL Example

curl -X PUT "https://api.groupdocs.cloud/v1.0/editor/storage/file/move/documents/draft.docx?destPath=documents/final/approved.docx&storageName=MyStorage" -H "accept: application/json" -H "authorization: Bearer YOUR_ACCESS_TOKEN"

SDK Examples

C# Example
// Create FileApi instance
var fileApi = new FileApi(configuration);

// Move file
var request = new MoveFileRequest(
    srcPath: "documents/draft.docx",
    destPath: "documents/final/approved.docx",
    srcStorageName: "MyStorage",
    destStorageName: "MyStorage"
);
fileApi.MoveFile(request);

Console.WriteLine("File moved successfully");
Python Example
# Create FileApi instance
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# Move file
request = groupdocs_editor_cloud.MoveFileRequest(
    src_path="documents/draft.docx",
    dest_path="documents/final/approved.docx",
    src_storage_name="MyStorage",
    dest_storage_name="MyStorage"
)
file_api.move_file(request)

print("File moved successfully")
Java Example
// Create FileApi instance
FileApi fileApi = new FileApi(configuration);

// Move file
MoveFileRequest request = new MoveFileRequest(
    "documents/draft.docx",
    "documents/final/approved.docx",
    "MyStorage",
    "MyStorage"
);
fileApi.moveFile(request);

System.out.println("File moved successfully");

Creating a Complete File Management Workflow

Now that you’ve learned the individual file operations, let’s see how to combine them into a practical workflow. The following example demonstrates a complete document processing cycle:

// File management workflow example in C#
public void ProcessDocument(string sourceFilePath, string fileName, string storageName)
{
    try
    {
        // 1. Upload the document
        Console.WriteLine("1. Uploading document...");
        var uploadRequest = new UploadFileRequest(sourceFilePath, new FileStream(fileName, FileMode.Open), storageName);
        var uploadResult = fileApi.UploadFile(uploadRequest);
        Console.WriteLine($"   Document uploaded to {uploadResult.Uploaded[0]}");
        
        // 2. Create a backup copy in archive folder
        Console.WriteLine("2. Creating backup copy...");
        var destPath = Path.Combine(Path.GetDirectoryName(sourceFilePath), "archive", Path.GetFileName(sourceFilePath));
        var copyRequest = new CopyFileRequest(sourceFilePath, destPath, storageName, storageName);
        fileApi.CopyFile(copyRequest);
        Console.WriteLine($"   Backup created at {destPath}");
        
        // 3. Download the file for local processing
        Console.WriteLine("3. Downloading document for processing...");
        var downloadRequest = new DownloadFileRequest(sourceFilePath, storageName);
        var fileStream = fileApi.DownloadFile(downloadRequest);
        var localPath = Path.GetFileName(sourceFilePath);
        using (var outputStream = File.Create(localPath))
        {
            fileStream.CopyTo(outputStream);
        }
        Console.WriteLine($"   Document downloaded to {localPath}");
        
        // 4. After processing, move the file to a processed folder
        Console.WriteLine("4. Moving document to processed folder...");
        var processedPath = Path.Combine("processed", Path.GetFileName(sourceFilePath));
        var moveRequest = new MoveFileRequest(sourceFilePath, processedPath, storageName, storageName);
        fileApi.MoveFile(moveRequest);
        Console.WriteLine($"   Document moved to {processedPath}");
        
        Console.WriteLine("Document processing workflow complete!");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

This workflow demonstrates a typical document processing scenario where you:

  1. Upload a document to cloud storage
  2. Create a backup copy for safety
  3. Download it for local processing
  4. Move it to a different location after processing

Troubleshooting Tips

  • File Existence: Before performing operations, check if files exist to avoid errors.
  • Path Formatting: Always use forward slashes (/) in file paths for consistency.
  • Special Characters: Be cautious with special characters in filenames, as they may need encoding.
  • Storage Space: Monitor your storage space to ensure you have enough capacity for uploads.
  • Permissions: Ensure your API credentials have the necessary permissions for file operations.
  • Error Handling: Implement robust error handling to manage failed operations gracefully.

What You’ve Learned

In this tutorial, you’ve learned how to:

  • Download files from cloud storage for local processing
  • Upload documents to your cloud storage
  • Delete files to manage your storage efficiently
  • Copy files to create backups or variations
  • Move files to organize your document structure
  • Combine these operations into a complete document workflow

These file management skills are essential for building robust document editing applications with GroupDocs.Editor Cloud.

Further Practice

To reinforce your learning, try these exercises:

  1. Create a file synchronization tool that keeps local and cloud files in sync
  2. Build a document conversion pipeline that processes files through upload, conversion, and download
  3. Implement a file versioning system that maintains multiple versions of documents

Helpful Resources