DEO KUMAR DAS
DEO KUMAR DAS

Reputation: 99

How and where shall I add bucket name ,object name,namespace in the code to upload data?

I am not getting where shall I add details like bucket name,namespace and object name to upload data on oracle cloud infrastructure using REST API.The code that I wrote no where mentioned those details that is why I am not sure whether the code is correct or not. Here is the code snippet.

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

Upvotes: 0

Views: 219

Answers (1)

Joe
Joe

Reputation: 2540

As the REST API docs for this operation mention, you need to put the namespace, bucket, and object names in the URI:

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

Upvotes: 1

Related Questions