繁体   English   中英

如何从Visual Studio中开发的Android C#应用程序访问SoftLayer对象存储。

[英]How do I access SoftLayer object storage from a C# application for Android, developed in Visual Studio.

如何从Visual Studio中开发的Android C#应用程序访问SoftLayer对象存储。 我试图在VS中添加Web参考,以便可以使用存储API服务。 我已经阅读了http://sldn.softlayer.com/reference/objectstorageapi http://docs.openstack.org/developer/swift/api/object_api_v1_overview.html,但仍然找不到执行该操作的方法。

非常感谢,非常感谢-任务的下一部分是将Android设备上的文件上传到对象存储。 该代码有点(!)混乱,没有错误检查,但希望会指出其他尝试朝正确方向进行操作的人。

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);

对于将C#用于SoftLayer,存在下一个链接:

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

下一个链接提供REST的对象存储信息:

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

接下来是一个示例,说明如何使用C#与SoftLayer API进行交互。 该示例遵循先前的C#链接。

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();
    }
  }
}

如果您决定通过curl管理并包装在C#代码中,那么下一个链接也可能对您有所帮助:

在C#中进行cURL调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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