0

How do I access SoftLayer object storage from a C# application for Android, developed in Visual Studio. I am trying to add a Web Reference in VS so that I can use the storage API services. I have read http://sldn.softlayer.com/reference/objectstorageapi http://docs.openstack.org/developer/swift/api/object_api_v1_overview.html but still can't find how to do this.

growell
  • 1
  • 1

2 Answers2

0

Thanks, greatly appreciated - the next part of the task was to upload a file on the Android device to Object Storage. The code is a bit(!) messy and lacks error checking but hopefully will point anyone else trying to do this in the right direction.

var path = Android.OS.Environment.ExternalStorageDirectory ;
var filename = path + Java.IO.File.Separator + string.Format("{0}", prefix) + "mydata.txt";
string username = "SLOS1234567-1:SL1234567";
string apiKey = "1234567891234567891234567891234567891234567891234567891234567891";
string tokenval, URLval, URLcomp;

//Create a web request for authentication.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://syd01.objectstorage.softlayer.net/auth/v1.0");

//Get the headers associated with the request.
WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

//Add the X-Auth-User header (for OS user) in the request.
myWebHeaderCollection.Add("X-Auth-User", username);

//Add the X-Auth-Key header (for the API key) in the request.
myWebHeaderCollection.Add("X-Auth-Key",apiKey);

//Get the associated response - the auth token and storage URL.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

tokenval = myHttpWebResponse.GetResponseHeader("X-Auth-Token");
URLval = myHttpWebResponse.GetResponseHeader("X-Storage-Url");
URLcomp = URLval + "/mycontainer/myDirectory/" + string.Format("{0}", prefix) + "mydata.txt";

//Upload the file
WebClient wc = new WebClient();
wc.Headers.Add("X-Auth-Token",tokenval);
wc.UploadFile(URLcomp, "PUT", filename);
growell
  • 1
  • 1
-1

For using C# for SoftLayer, there’s the next link available:

https://sldn.softlayer.com/article/C-Sharp

The next link provides Object Storage information for REST:

http://sldn.softlayer.com/blog/waelriac/managing-softlayer-object-storage-through-rest-apis

The next is an example of how C# can be used to interact with the SoftLayer API. The example follows the previous C# link.

using System;
using Newtonsoft.Json;

namespace GetHubNetworkStorage
{
  class Program
  {
    static void Main(string[] args)
    {
      string username = "set me";         
      string apiKey = "set me";

      authenticate authenticate = new authenticate();
      authenticate.username = username;
      authenticate.apiKey = apiKey;

      SoftLayer_AccountService accountService = new SoftLayer_AccountService();
      accountService.authenticateValue = authenticate;

      try
      {
        // The result is an array of SoftLayer_Network_Storage objects and can be either iterated
        // one by one to use the data or being displayed as a JSON value such in this example.
        var hubNetworkStorages = accountService.getHubNetworkStorage();
        string json = JsonConvert.SerializeObject(hubNetworkStorages, Formatting.Indented);
        Console.WriteLine(json);
      }
      catch (Exception e)
      {
        Console.WriteLine("Can't retrieve SoftLayer_Network_Storage information: " + e.Message);
      }
      Console.ReadKey();
    }
  }
}

The next link also might help you if you decide to manage the object-storage-rest-api through curl but wrapped into C# code:

Making a cURL call in C#

Community
  • 1
  • 1