Connect to SugarSync through .Net, SugarSync DotNet API, SugarSync Connection with .Net

Download The Source Here

Dear All,

This is My First Post please kindly advise if I made any mistakes thanks.

SugarSync The cloud storage site. It is providing up to 5GB free space to each user. Once I got a requirement that I need to create a UI for SugarSync with SugarSync REST API to upload and download our documents to reduce our DB burden.  I search for help in SugarSync site for communicating with SugarSync REST API. They provided code samples for java not for .net. So I got struck there. Then I started working on it and finally I have create a Library for Communicating with SugarSync REST API. By using this you can upload the documents or images small files anything but it will upload only small files not big and can watch the List of files located in your account. If want means you can download your files. Let’s start.

Prerequisites

1. Required a SugarSync Account you can create here.

2. Required a SugarSync Developer Account can create here.

3. Required a “AccessKeyID” and “Private Access Key” can get from Developer account after login it’s free.

4. Download Source code provided above.

5. Make changes in Default.aspx.cs file replace AccessKeyID and PrivateAccessKey with your accesskeys not mandatory. Old values given by me also will work.

See the bellow mentioned Image of UI I created for communicating with SugarSync account.

Image SugarSunc UI

Uploading A File to SugarSync Account

In this you need to provide the UserName and Password of your SugarSync Account and can click on Browse button and select any file from your computer. Then click on Upload Button. Then it will starts uploading the file to your account. See the bellow code for Uploading file in button click event.

protected void btnUpload_Click(object sender, EventArgs e)
{
string authToken = objSugarBase.getAuthorizationResponse(txtUserName.Text, txtPass.Text, accessKeyId, privateAccesskey);
if (fuFile.HasFile)
{
try
{
string fileName = fuFile.PostedFile.FileName;
string mediaType = Path.GetExtension(fileName);
lblResult.Text = “Upload Status:” + objSugarBase.handleUploadCommand(authToken, fuFile.FileName, mediaType,fileName);
}
catch (Exception ex)
{
throw ex;
}
}
}

In the above code block I am calling a method from SugarSyncAPI library it’s name is “getAuthorizationResponse” we need to provide the Username, Password, AccessKeyID and PrivateAccesskey to that method then It will return us the Authentication token. By Using that authentication token we can upload the file Authentication token is mandatory for uploading or downloading or any other action.

For uploading I am using the method “handleUploadCommand”

It required four parameters One is AuthenticationToke, LocalFilePath, MediaType, FileName. If we provide these values it will upload the media to specified SugarSync Account.

In the SugarSyncAPI library SugarBase is the class which we need to communicate remaining classes are support classes.

Get All Files List From SugarSync Account

For this once again you need to provide the same parameters like UserName and Password, Briefly we need to get the Authentication Token I already explained above how to get that.  Then click on “Get Files List From MagicBriefcase” Button. Then It will executes the following code.

protected void btnFilesList_Click(object sender, EventArgs e)
{
GetMagicBreafcaseContents();
}
public void GetMagicBreafcaseContents()
{
string authToken = objSugarBase.getAuthorizationResponse(txtUserName.Text, txtPass.Text, accessKeyId, privateAccesskey);
List<string> resultFiles = new List<string>();
resultFiles = objSugarBase.GetAllFilesList(authToken);
rptrFileList.DataSource = resultFiles;
rptrFileList.DataBind();
}

Same like previous one We need to call the “GetAllFilesList”method for getting the all files list from SugarSync Account need to provide the Authentication Token for this method.

This method will return the files List<string> I bound that list to Repeater.

Download a File from SugarSync

For downloading also we need Authentication token. Watch bellow mentioned code for Downloading a file.

protected void lbDownlink_Click(object sender, EventArgs e)
{
LinkButton lbDownLink = (LinkButton)sender;
string authToken = objSugarBase.getAuthorizationResponse(txtUserName.Text, txtPass.Text, accessKeyId, privateAccesskey);
WebResponse response = objSugarBase.handleDownloadCommand(authToken,Convert.ToString(lbDownLink.Text));

HttpResponse respo = HttpContext.Current.Response;
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = (response.GetResponseStream()).Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytes);
}
respo.Clear();
respo.ClearContent();
respo.ClearHeaders();
respo.Buffer = true;
respo.AddHeader(“Content-Disposition”, “attachment;filename=\”” + lbDownLink.Text + “\””);
respo.BinaryWrite(ms.ToArray());
respo.End();
}

For downloading the file we need to call the method named “handleDownloadCommand”. It required two parameters One is Authentication Token and File name we can get the file name from repeater. I have passed that to this method. then It will directly ask you for download the file. You can save it in your local machine.

Advertisements
Posted in Uncategorized | 2 Comments