DEV Community

Andriy Andruhovski for Aspose.PDF

Posted on • Edited on

3 1

3 simple steps to start using Aspose.Cloud Storage

Solving the problems of document processing, we often encounter the problem of getting source documents from different sources and from different providers. Aspose.Cloud Storage is a component of Aspose.Cloud platform, which releases a transparent interface for access to different sources. Now Aspose.Cloud Storage supports popular services from Amazon, Dropbox, Google, Microsoft. Let's see how we can use this component.

Step 1: Create an Aspose.Cloud account

  • We can create a free Aspose.Cloud account using the link "Create a new Account" on http://dashboard.aspose.cloud or sign in with GitHub or Google Account.

SignIn/SignUp Form

  • When registering, for each account an Aspose.Cloud Storage is created. We add another storage later, and now we are trying to access the current storage using C#. To do this we need to get an application credential.

Step 2: Get an Application credentials

Aspose.Cloud Application allows us to manage access to Cloud API. By default, each account has one application named "First App". Using the "My Apps" tab, we can see the list of all applications, change settings or get the credential.

My Apps

The application credentials (App SID/App Key) are placed directly on the "First App" tile, but if you want to change the settings of the "First App", just click on tile heading. for testing purposes, we have enough default settings, so we will proceed to the next step, writing the client application. In this example, we will use a "Console Application" template in Microsoft Visual Studio 2017.

Step 3: Create a Client application

To demonstrate the main concept of the cloud storage working, we will write an application that will synchronize certain local folder with a cloud folder with the same name. So, let's start.

  1. Create a console application (in VS2017 File->New->Project>Windows Classic Desktop->Console App);
  2. Install Aspose.Storage for Cloud SDK (in Package Manager Console: Install-Package Aspose.Storage-Cloud) Install Aspose.Storage for Cloud SDK

Step 3a: Setup an API client

To be able making API requests, we must initialize an API client. It's pretty straightforward: declare StorageApi object and initialize it with AppKey and AppSID.

class Program
{
const string storageName = "First Storage";
const string AppSid = "<Put App SID here>";
const string AppKey = "<Put App Key here>";
private static StorageApi _storageApi = new StorageApi(AppKey, AppSid);
static void Main()
{
}
}
view raw Program.cs hosted with ❤ by GitHub

Step 3b: Write own code using API client

This part of our example will show the usage of most frequent API requests:

  • Check if a file exists
  • Create a folder
  • Get a list of files
  • Create (upload) file to cloud storage

It is worth noting, that all StorageApi methods accept a request object and returns a response object. All response objects have common properties such us Code and Status and we can use there to check if API call was successful.

According to our problem, we need to synchronize certain local folder with a cloud folder with the same name. So first, we must ensure that the cloud folder exists, otherwise create one. Storage API provides GetIsExist and PutCreateFolder.
Thus, checking if the cloud folder would look like this:

static void Main()
{
var folderName = "pdf-demo";
var localFolder = $@"C:\asposedemo\{folderName}\";
var fileExistResponse = _storageApi.GetIsExist(new GetIsExistRequest(folderName, null, storageName));
if (fileExistResponse.Code == 200)
if (fileExistResponse.FileExist.IsFolder == false)
{
var createFolderResponse = _storageApi.PutCreateFolder(new PutCreateFolderRequest(folderName, null, storageName));
if (createFolderResponse.Code != 200)
{
Console.WriteLine("CreateFolder response is not OK!");
Console.WriteLine(createFolderResponse.ToString());
return;
}
}
else
{
// TODO: Write code here
}
else
{
Console.WriteLine("FileExist response response is not OK!");
Console.WriteLine(fileExistResponse.ToString());
}
}
view raw Program.cs hosted with ❤ by GitHub

Next step, we prepare a file list for upload. What will we do? It's easy: we need to get a list of all files from the local folder and a list of files in the cloud. The difference of these lists give us a list of files need to upload.

Finally, we will call PutCreateDocument the loop through the resultant list.

static void Main()
{
var folderName = "pdf-demo";
var localFolder = $@"C:\asposedemo\{folderName}\";
var fileExistResponse = _storageApi.GetIsExist(new GetIsExistRequest(folderName, null, storageName));
if (fileExistResponse.Code == 200)
if (fileExistResponse.FileExist.IsFolder == false)
{
var createFolderResponse = _storageApi.PutCreateFolder(new PutCreateFolderRequest(folderName, null, storageName));
if (createFolderResponse.Code != 200)
{
Console.WriteLine("CreateFolder response is not OK!");
Console.WriteLine(createFolderResponse.ToString());
return;
}
}
else
{
var filesResponse = _storageApi.GetListFiles(new GetListFilesRequest(folderName, storageName));
if (filesResponse.Code == 200)
{
var filesInCloudFolder = filesResponse.Files.Where(f => f.IsFolder == false).Select(item => item.Name).ToList();
var filesInLocalFodler = new DirectoryInfo(localFolder).GetFiles().Select(fi => fi.Name);
foreach (var fileName in filesInLocalFodler.Except(filesInCloudFolder))
{
Console.WriteLine($"Uploading {fileName}");
var fileStream = new FileStream($@"{localFolder}\{fileName}", FileMode.Open);
var uploadResponse = _storageApi.PutCreate(new PutCreateRequest($"/{folderName}/{fileName}", fileStream));
}
}
}
else
{
Console.WriteLine("FileExist response response is not OK!");
Console.WriteLine(fileExistResponse.ToString());
}
}
view raw Program.cs hosted with ❤ by GitHub

Conclusion

  • Q: Is it one-way synchronizer?
  • A: Yes. It showed the main ways of usage Aspose.Cloud Storage, and you can improve it yourself.
  • Q: And what about other storages?
  • A: Learn more in the article "How to Configure 3rd Party Cloud Storages".
  • Q: What's next?
  • A: In the next posts, we will continue to deal with the Aspose.Cloud features.

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (0)

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay