OCI Use Powershell To deal with ObjectStorage – Public bucket

TLDR – How to download and upload to OCI object Storage with PowerShell

I had a case a few weeks ago where the customer was asking how to download and upload a file from object storage using a preauthentication key. The customer’s goal was to download and install software from object storage using a PowerShell script. I’d written PowerShell to deal with Object Storage using an Auth token befor, thinking a public bucket would not be too difficult, I took the ticket  The following code is the end result of that ticket.

Screenshot 2020-02-27 at 12.47.42.png


Download from Public Bucket

The annoying part of the process is PowerShell TLS1.2 support.  The code will work with a default PowerShell install. You have to run as admin and add TLS1.2 support. You’ll most likely hit  the error “The underlying connection was closed: An unexpected error occurred on a send“; Which is a Powershell error indicating TLS1.2 is not supported in your session. To enable TLS1.2; run the following as admin: # [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls1. 

After TLS is enabled the URL needs to be changed.  When you click on an Item in Object storage and select Object Details you’ll find the object URL. Copy the URL from Details then modify it. Change objectstorage.<datacenter> to swiftobjectstorage.<datacenter>, and change the end a bit from /b/HappyKev_FRA/o/bob-1.txt to /v1/customeroperations/HappyKev_FRA/bob-1.txt where CustomerOperations is the tenant name and HappyKev_FRA is the object storage bucket. See before and after in the code below as an example.

# Download a file in a public bucket
# URL from webconsole -- URL Path (URI): https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/customeroperations/b/HappyKev_FRA/o/bob-1.txt
# If error "The underlying connection was closed: An unexpected error occurred on a send" run the following :
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$uri = "https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/bob-1.txt"
$filename = "c:\bob-1.txt"
Invoke-WebRequest -Method GET -uri $uri -outfile $filename


Upload to Public bucket

Uploading to the bucket is a bit simpler, no need to make major modifications to the URL. You use the pre-authenticated URL as provided by the OCI web console, only needing to add a filename to the end.  To obtain the URL; In the console open the bucket, click on Pre-Authentication Keys, the click Create Pre-Authenticated Request.

Take the URL from the above process, place it into the code below and under $URI, add the file name to the end, and add the filename to the $filename variable, and happy uploading.

# upload a file with a preauthentication token
# URL from webconsole -- URL Path (URI): https://objectstorage.eu-frankfurt-1.oraclecloud.com/p/8Nj5ZcAE3z_x9V5N4PxWd0SFFyz7c524NLRNszt4_sU/n/customeroperations/b/HappyKev_FRA/o/
# working curl curl -v -X PUT -T bob.pub https://objectstorage.eu-frankfurt-1.oraclecloud.com/p/8Nj5ZcAE3z_x9V5N4PxWd0SFFyz7c524NLRNszt4_sU/n/customeroperations/b/HappyKev_FRA/o/
# If error "The underlying connection was closed: An unexpected error occurred on a send" run the following :
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$uri = "https://objectstorage.eu-frankfurt-1.oraclecloud.com/p/8Nj5ZcAE3z_x9V5N4PxWd0SFFyz7c524NLRNszt4_sU/n/customeroperations/b/HappyKev_FRA/o/bob-2.txt"
$filename = "c:\bob\bob-2.txt"
invoke-webrequest -Method PUT -InFile $filename -uri $uri -verbose -UseBasicParsing


Leave a Reply