0

我不知道应该在哪里添加存储桶名称、命名空间和对象名称等详细信息,以使用 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()}");
            }
        }
4

1 回答 1

1

正如此操作的REST API 文档所述,您需要将命名空间、存储桶和对象名称放在 URI 中:

var uri = new Uri("https://objectstorage.us-phoenix-1.oraclecloud.com/n/" + Namespace + "/b/" + bucket + "/o/" + objectName);
于 2018-08-01T18:20:26.250 回答