Upload file in the cloud storage

Availability: SharpBox 1.0

The following example shows how to upload a file in the root of an existing cloud storage. This example uses the implemented drop box storage provider, so it needs also a valid dropbox configuration. To get the sample up and running an own dropbox account is neccessary. Additionally a valid application key and secret is needed. If you don't have one, use our test-application.
            // instanciate a new credentials object, e.g. for dropbox
            DropBox.DropBoxCredentials credentials = new AppLimit.CloudComputing.SharpBox.DropBox.DropBoxCredentials();

            // attach the application information 
            credentials.ConsumerKey = "TheApplicationKey";
            credentials.ComsumerSecret = "TheApplicationSecret";

            // add the account information
            credentials.UserName = "myaccount@dropbox.com";
            credentials.Password = "mypassword";

            // instanciate a cloud storage configuration, e.g. for dropbox
            DropBox.DropBoxConfiguration configuration = DropBox.DropBoxConfiguration.GetStandardConfiguration();

            // instanciate the cloudstorage manager
            CloudStorage storage = new CloudStorage();

            // open the connection to the storage
            storage.Open(configuration, credentials);            

            // get the root entry of the cloud storage 
            ICloudDirectoryEntry root = storage.GetRoot();
            if (root == null)
            {
                Console.WriteLine("No root object found");
            }
            else
            {
                // create the file
                ICloudFileSystemEntry file = storage.CreateFile(root, "up.load");

                // build the data stream
                Stream data = new MemoryStream();

                // build a stream read
                StreamWriter writer = new StreamWriter(data);
                writer.WriteLine("Hello World");

                // reset stream
                data.Position = 0;

                // upload data
                file.GetDataTransferAccessor().Transfer(data, nTransferDirection.nUpload, null, null);
            }

            // close the cloud storage connection
            if (storage.IsOpened)
            {
                storage.Close();
            }
        }

Last edited Aug 6, 2011 at 5:37 AM by dei79, version 5

Comments

SergeyDubinets Dec 17, 2011 at 6:35 AM 
This doesn't compile.
DropBoxCredentials you removed in 1.2.0. as well as UsreName and Password
ComsumerSecret you never had.

redtalon Sep 21, 2011 at 11:55 AM 
Trying the example I get the error "NotSupportedException"

at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestStreamHelper.GetResponseInternal(WebRequest request)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestStreamHelper.GetResponse(WebRequest request)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestService.GetWebResponse(WebRequest request)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestService.PerformWebRequest2(WebRequest request, Object context, Stream content, HttpStatusCode& code, WebException& errorInfo)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestService.PerformWebRequest2(WebRequest request, Object context, HttpStatusCode& code, WebException& errorInfo)
at AppLimit.CloudComputing.SharpBox.StorageProvider.DropBox.Logic.DropBoxStorageProviderService.CommitUploadStream(Object[] arg)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestStream.PerformeDisposeOperations(Stack`1 stack)
at AppLimit.CloudComputing.SharpBox.Common.Net.Web.WebRequestStream.System.IDisposable.Dispose()
at AppLimit.CloudComputing.SharpBox.StorageProvider.GenericStorageProviderService.UploadResourceContent(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry, Stream targetDataStream, FileOperationProgressChanged progressCallback, Object progressContext)
at AppLimit.CloudComputing.SharpBox.StorageProvider.BaseObjects.BaseFileEntryDataTransfer.Transfer(Stream targetDataStream, nTransferDirection direction, FileOperationProgressChanged progressCallback, Object progressContext)
at AudioPhone7.Dropbox.butUploadFile_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Occurs on the .transfer method, anyone help?

kasparov Jul 20, 2011 at 6:50 AM 
Same here, I do something like this (GetContentStream doesn't seem to exist any more)...

ICloudFileSystemEntry file = storage.CreateFile(null, @"tew.txt");
System.IO.Stream stream = file.GetDataTransferAccessor().GetUploadStream(1000);
new System.IO.StreamWriter(stream).WriteLine("SDLKJF");
stream.Close();

file doesn't show up

Kalpal Apr 25, 2011 at 1:14 AM 
You need to add a / to your folder path. Rather use this example:

// instanciate a cloud storage configuration, e.g. for dropbox
DropBoxConfiguration configuration = DropBoxConfiguration.GetStandardConfiguration();

// instanciate the cloudstorage manager
CloudStorage storage = new CloudStorage();

ICloudStorageAccessToken StorageAccessToken = storage.Open(configuration, credentials);

storage.UploadFile(Path, "/YourPath",this.FileUpload1.FileName);

// close the cloud storage connection
if (storage.IsOpened)
{
storage.Close();
}

sanaanicolas Nov 18, 2010 at 2:12 PM 
does anyone knows why upload method doesn't work
no exceptions and everything works just the file doesn't occur