如何在代码中添加存储桶名称,对象名称,命名空间以上传数据?

时间:2018-08-01 08:12:21

标签: oracle-cloud-infrastructure

我不知道应在何处添加存储桶名称,命名空间和对象名称之类的详细信息,以使用REST API在oracle云基础架构上上传数据。我在何处写的代码没有提到那些详细信息,这就是为什么我不确定代码正确与否。 这是代码段。

           var tenancyId = ConfigurationManager.AppSettings["BMCTenancyId"];
            var userId = ConfigurationManager.AppSettings["BMCUserId"];
            var fingerprint = ConfigurationManager.AppSettings["BMCFingerprint"];
            var privateKeyPath = ConfigurationManager.AppSettings["BMCPrivateKeyPath"];
            var privateKeyPassphrase = ConfigurationManager.AppSettings["BMCPrivateKeyPassphrase"];
            var bucket= ConfigurationManager.AppSettings["BMCBucket"];
            var Namespace= ConfigurationManager.AppSettings["BMCNamespacet"]; 

            var signer = new RequestSigner(tenancyId, userId, fingerprint, privateKeyPath, privateKeyPassphrase);

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var uri = new Uri($"https://objectstorage.us-phoenix-1.oraclecloud.com/");

            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.Accept = "application/json";
            request.SendChunked = true;
            request.ContentType = "text/plain";

            request.ContentLength =postdata.Length;

        try
        {

            using (var stream = request.GetRequestStream())
            {
                stream.Write(postdata, 0, postdata.Length);

            }
        }
        catch(Exception ex)
        {
            Response.Write("testing"+ex.Message+"Testing");

        }

            request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(postdata));

            signer.SignRequest(request);

            Console.WriteLine($"Authorization header: {request.Headers["authorization"]}");

            ExecuteRequest(request);

            Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", request.ContentLength);
    }

        private static void ExecuteRequest(HttpWebRequest request)
        {
            try
            {
               var webResponse = (HttpWebResponse)request.GetResponse();
                var response = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
                Console.WriteLine($"Response: {response}");
            }
            catch (WebException e)
            {
                Console.WriteLine($"Exception occurred: {e.Message}");
                Console.WriteLine($"Response: {new StreamReader(e.Response.GetResponseStream()).ReadToEnd()}");
            }
        }

1 个答案:

答案 0 :(得分:1)

正如该操作的REST API docs所述,您需要将名称空间,存储区和对象名称放入URI:

var uri = new Uri("https://objectstorage.us-phoenix-1.oraclecloud.com/n/" + Namespace + "/b/" + bucket + "/o/" + objectName);