public byte[] GetDocument(Guid documentId) { SetConnection(); var obj = new CF_Object(_conn, _container, _client, documentId.ToString()); try { using (Stream stream = obj.Read()) using (MemoryStream ms = new MemoryStream()) { int count = 0; do { byte[] buf = new byte[1024]; count = stream.Read(buf, 0, 1024); ms.Write(buf, 0, count); } while (stream.CanRead && count > 0); byte[] document = ms.ToArray(); return document; } } catch (ObjectNotFoundException) { throw new ApplicationException(String.Format("Document not found [{0}]", documentId)); } }
public void SaveFile(Stream stream, string fileName) { if (!Authenticate()) throw new InvalidOperationException("Unable to authenticate"); CreateContainer(); var fileObject = new CF_Object(_connection, _container, new CF_Client(), fileName); fileObject.Write(stream); stream.Close(); }
public void StoreDocument(byte[] document, Guid documentId) { SetConnection(); var obj = new CF_Object(_conn, _container, _client, documentId.ToString()); using (MemoryStream ms = new MemoryStream(document)) { obj.Write(ms); } }
public Stream ReadFile(string fileName) { if (!Authenticate()) throw new InvalidOperationException("Unable to authenticate"); var fileObject = new CF_Object(_connection, _container.Name, fileName); return fileObject.Read(); }
protected void btnUpload_Click(object sender, EventArgs e) { string username = CFUsernameText.Text; string api_access_key = CFApiKeyText.Text; string ContainerNameText = CFContainerText.Text; try { //Check to make sure a service net value to selected if (!SnetFalse.Checked & !SnetTrue.Checked) { Error.Text = "Please select true or false for ServiceNet"; } else { //Check to make sure a file is selected. if (FileUpload1.HasFile) { //If service net is checked then continue. if (SnetTrue.Checked) { //create the path to save the file to. string fileName = Path.Combine(Server.MapPath("~/temp/"), FileUpload1.FileName); string path = Server.MapPath("~/temp/"); //ServiceNet value taken from frontend. bool snet = bool.Parse(SnetTrue.Text); //Save the file to local path. FileUpload1.SaveAs(fileName); //Set CloudFiles credentials var userCredentials = new UserCredentials(username, api_access_key); var client = new CF_Client(); Connection conn = new CF_Connection(userCredentials, client); conn.Authenticate(snet); //Set container, and obj values var container = new CF_Container(conn, client, ContainerNameText); var obj = new CF_Object(conn, container, client, FileUpload1.FileName); //CloudFiles binding writing from local object to CloudFiles obj.WriteFromFile(fileName); //Get list of object in container you just uploaded to var list = container.GetObjects(true); CFResultsGrid.DataSource = list; CFResultsGrid.DataBind(); Error.Text = FileUpload1.FileName + " Has Been Uploaded Successfully"; } else if (SnetFalse.Checked) { //create the path to save the file to. string fileName = Path.Combine(Server.MapPath("~/temp/"), FileUpload1.FileName); string path = Server.MapPath("~/temp/"); //ServiceNet value taken from frontend. bool snet = bool.Parse(SnetTrue.Text); //Save the file to local path. FileUpload1.SaveAs(fileName); //Set CloudFiles credentials var userCredentials = new UserCredentials(username, api_access_key); var client = new CF_Client(); Connection conn = new CF_Connection(userCredentials, client); conn.Authenticate(snet); //Set container, and obj values var container = new CF_Container(conn, client, ContainerNameText); var obj = new CF_Object(conn, container, client, FileUpload1.FileName); //CloudFiles binding writing from local object to CloudFiles obj.WriteFromFile(fileName); //Get list of object in container you just uploaded to var list = container.GetObjects(true); CFResultsGrid.DataSource = list; CFResultsGrid.DataBind(); Error.Text = FileUpload1.FileName + " Has Been Uploaded Successfully"; } } } } catch (Exception ex) { Error.Text = "Something went terribly wrong! See below for more info. <br /> <br />" + ex.ToString(); } }
/// <summary> /// Uploads the specified content item to the remote blob storage. /// </summary> /// <param name="content">Descriptor of the item on the remote blob storage.</param> /// <param name="source">The source item's content stream.</param> /// <param name="bufferSize">Size of the upload buffer.</param> /// <returns>The length of the uploaded stream.</returns> public override long Upload(IBlobContent content, Stream source, int bufferSize) { var filename = this.GetBlobName(content); var obj = new CF_Object(this.Connection, this.ContainerBucket, this.Client, filename); obj.Write(source); return obj.ContentLength; }