DEV Community

asposewords
asposewords

Posted on

4 4

Uploading a document to Aspose.Words Cloud Storage (C# / .NET)

Before you can use the Aspose.Words Cloud API (or other Aspose Cloud APIs) to read and update existing documents the files will need to be loaded into the Storage in the cloud.

The Aspose.Storage Cloud API can be directly accessed via the REST API but this sample will use the Cloud SDK for .NET to make the code simpler.

To begin you will need the SDK downloaded (either the DLL or the source code) and to sign up for a set of credentials by creating an account and requesting a set of API keys. See https://dashboard.aspose.cloud/

The sample code below shows a simple function to upload a file to Storage by supplying your API keys, local file name and where you want to store the file on Storage.

The essential steps are to:

  1. Open the StorageApi using your app keys
  2. Create a PutCreateRequest and pass in details of the file
  3. Call the PutCreate method
  4. Check the result
using System;
using System.IO;
using Aspose.Storage.Cloud.Sdk.Api;
using Aspose.Storage.Cloud.Sdk.Model;
using Aspose.Storage.Cloud.Sdk.Model.Requests;
namespace AsposeSamples
{
public class StorageSample
{
/// <summary>
/// UploadFile - Upload local file to storage so that API calls can process the file.
/// See https://dashboard.aspose.cloud/ for details on creating your app and getting your App Sid and App Key.
/// </summary>
/// <param name="appSid"></param>
/// <param name="appKey"></param>
/// <param name="localFileName">Local or UNC path to local file name</param>
/// <param name="remoteFileName">Remote file name </param>
/// <param name="remoteFolderName">Remote file path with foward-slash delimiters</param>
/// <param name="storageName">'First Storage' is the default for new apps. See https://dashboard.aspose.cloud/ to manage stoage</param>
/// <returns></returns>
private static bool UploadFile(string appSid, string appKey, string localFileName,
string remoteFileName, string remoteFolderName, string storageName)
{
bool fileOk = false;
// 1 - Open the Storage API using your app Key and Sid
var storageApi = new StorageApi(appKey, appSid);
using (Stream fs = File.OpenRead(localFileName))
{
// 2 - Create a put request with the file details
PutCreateRequest putRequest = new Aspose.Storage.Cloud.Sdk.Model.Requests.PutCreateRequest(
$"{remoteFolderName}/{remoteFileName}", fs, null, storageName
);
// 3 - Upload the file
UploadResponse uploadResponse = storageApi.PutCreate(putRequest);
// 4 - Check the response status and feed that back to calling code.
if (uploadResponse.Status.ToUpper() == "OK")
{
fileOk = true;
}
}
return fileOk;
}
}
}

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay