簡體   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