OCI Use Powershell To deal with ObjectStorage - Kevinm-Oracle/Support_Knowlege GitHub Wiki
TLDR - Sample Powershell to list items and upload items to an Oracle Object storage buckets
I was working a case this week where we needed to test the login credentials of a user trying to access a Bucket in OCI Object Storage. I have a sample Curl command you can run to GET a list of bucket content and PUT / Upload files into a bucket. The user with the issue was running a Windows instance which does not have curl natively installed. Lucky for us he had a Linux instance in the same network we could test with. Having not touched PowerShell in ages I figured this was a good excuse to crank out some Powershell.The process started with an attempt to write a script to list the contents of a bucket. Curl commands can be converted to Invoke-WebRequest or Invoke-RestMethod commands in PowerShell. For the Oracle stuff, Invoke-WebRequest did the trick. Roadblock one, what auth credentials do you use? For basic auth to a bucket with Oracle, you use an Auth token; I'll cover these at the end of this topic and the code comments. For now, let us assume we have the credentials
Armed with the correct user name and password I first tried using the -credential option to send the login credentials; that failed. After a bit of web searching, how all great programs are written, I found a post explaining how to send basic auth credentials as part of the header. About 4 lines of code to replace the -u user option in Curl; mutter. The result and working samples of a GET request are below. - Yes, I deleted the Auth token so you cannot use it.
# command in Curl - curl -v -u '[email protected]:41f91XnTHq]Qwi(u>h>g' -X GET https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/customeroperations/HappyKev_PHX
# For credential use your user name and AUTH token.
# You can generate an Auth token from the OCI webconsole under your user account
# Things to Modify
$user = "[email protected]"
$pass = "41f91XnTHq]Qwi(u>h>g"
$uri = "https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/customeroperations/HappyKev_PHX"
# needed to support TLS in older versions of powershell
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Encode the password to shove it into a header then build the auth header
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
# do the actual work, connect to the URL, and make pretty output
$resp = invoke-webrequest -Method GET -uri $uri -Headers $Headers -verbose -UseBasicParsing
$resp
(convertfrom-json $resp.Content) | ft
# command in Curl - curl -v -u '[email protected]:41f91XnTHq]Qwi(u>h>g' -X PUT -T bob.txt https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/customeroperations/HappyKev_PHX/bob.txt
# To use Modify $user $pass, $uri, and $filename
# For credential use your user name and AUTH token.
# You can generate an Auth token from the OCI webconsole under your user account
#Things to modify
$user = "[email protected]"
$pass = "41f91XnTHq]Qwi(u>h>g"
$uri = "https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/customeroperations/HappyKev_PHX"
$filename = "bob.txt" # Name of the file you want to upload
# needed to support TLS in older versions of powershell
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Encode the password to shove it into a header then build the auth header
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
$uriput = $uri+$filename
# do the actual work, connect to the URL, upload the file, list the files
invoke-webrequest -Method PUT -InFile $filename -uri $uriput -Headers $Headers -verbose -UseBasicParsing
convertfrom-json (invoke-webrequest -Method GET -uri $uri -Headers $Headers -UseBasicParsing).content | ft