简体   繁体   中英

How we can Upload files into rackspace cloud container with API in C#

I want to Upload files into rackspace cloud container with API in C# and I am using .net 4.0 version. So, how I can create webrequest for this. Even I successfully created containers with the same request but I am not able to create object into my container.

Number of times I tried to upload my file into my container but I am continuously getting error like Unauthorized access and my code is shown below:

HttpWebRequest request = WebRequest.Create(new Uri(authInfo.StorageUrl + "/TestContainer/myfile.txt")) as HttpWebRequest;
request.Method = "PUT";
request.Headers["X-Auth-Token"] = MyToken;
byte[] data = System.IO.File.ReadAllBytes(@"D:\myfile.txt");
request.ContentLength = data.Length;
//request.Headers["Content-Length"] = "512000";
var response = request.GetResponse();

Please tell me what I am doing wrong with this.

You haven't written the bytes to the request stream. Do something like this:

Stream reqStream = request.GetRequestStream();

reqStream.Write(fileBytes, 0, fileBytes.Length);

reqStream.Close();

Webresponse response = request.getresponse();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM