OCI Use CURL To deal with ObjectStorage - Kevinm-Oracle/Support_Knowlege GitHub Wiki

TLDR - Example curl commands to interact with Oracle OCI Object Storage

Creating the URL

When you browse to an item in a bucket and click on the three dots next to it and select view details you'll find a URL to your item. You can use this item in a browser or in a curl get command. You cannot use this url in a curl PUT command to upload a file. For my item bob.txt the following URL is displayed, also see the image. -

https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/customeroperations/b/HappyKev_FRA/o/bob.txt

From trial and error, for curl to work with a PUT command you have to connect to the swift endpoint. Connecting to the Frankfurt datacenter you want to use swiftobjectstorage.eu-frankfurt-1.oraclecloud.com and not objectstorage.eu-frankfurt-1.oraclecloud.com. See the following for a list of endpoints for all datacenters. Swtich endpoint listing for OCI datacenters

The url should also include /v1/ your tenant name and the object storage bucket name You can find these in the link above from the view object details option

https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/<tenant_name>/<bucket_name>/

Example of a working url

https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/

Get commands to list files and download folders

My bucket is a public bucket and does not require authentication to view or download items.

List Items

To list the items in my bucket you run the following command

curl -X GET https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/

The returned list of items is in JSON format. Piping the output to a JSON interpreter might make reviewing the output simpler. In my case MAC natively lets you add | json_pp to the end and the output will be formatted in a more readable format.

kevimill-mac:~ kevimill$ curl -X GET https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA | json_pp
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   137  100   137    0     0    166      0 --:--:-- --:--:-- --:--:--   166
[
   {
      "hash" : "c1a5298f939e87e8f962a5edfc206918",
      "last_modified" : "2020-01-10T12:21:56.298000",
      "content_type" : null,
      "bytes" : 2,
      "name" : "bob.txt"
   }
]

Download a file

To download a file with curl you add the file name to the end of the command used to list items. See below example to view the contents of bob.txt or download the file bob.txt by adding -O to the end of the curl command

kevimill-mac:~ kevimill$ curl -X GET https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/bob.txt 
Hi

kevimill-mac:~ kevimill$ curl -X GET https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRAob.txt -O
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     2  100     2    0     0      5      0 --:--:-- --:--:-- --:--:--     5

Upload a file

To upload items to the bucket you must be authenticated. For authentication, you'll need an Auth Token, see the last section in this document Where the Auth Tokens come from for help with that. To upload a file you're going to add a -u and a PUT -T to your command to include a user name, password, and file to upload. See example command and example output with the -v added to make the output more exciting

curl -u '[email protected]:7_oOGF)dQC{eYqQjjS4R' -X PUT -T bob.txt https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/bob.txt

kevimill-mac:~ kevimill$ curl -v -u '[email protected]:7_oOGF)dQC{eYqQjjS4R' -X PUT -T bob.txt https://swiftobjectstorage.eu-frankfurt-1.oraclecloud.com/v1/customeroperations/HappyKev_FRA/bob.txt
 HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 201 Created
< Date: Fri, 10 Jan 2020 13:16:24 GMT
< Content-Type: application/octet-stream
< Content-Length: 0
< Connection: keep-alive
< etag: c1a5298f939e87e8f962a5edfc206918
< last-modified: Fri, 10 Jan 2020 13:16:24 GMT
< X-Trans-Id: fra-1:dA1glVO9MmMGkzvCJNT-l-UF1WumuSajl-YZu1V18BDo7IUJZeZE7BHBqwgSnjnq
< X-Timestamp: 1578662184.11300
< opc-request-id: fra-1:dA1glVO9MmMGkzvCJNT-l-UF1WumuSajl-YZu1V18BDo7IUJZeZE7BHBqwgSnjnq
< x-api-id: swift
< access-control-allow-origin: *
< access-control-allow-methods: POST,PUT,GET,HEAD,DELETE,OPTIONS
< access-control-allow-credentials: true
< access-control-expose-headers: access-control-allow-credentials,access-control-allow-methods,access-control-allow-origin,content-length,content-type,date,etag,last-modified,opc-client-info,opc-request-id,x-api-id,x-timestamp,x-trans-id
< 
* Connection #0 to host swiftobjectstorage.eu-frankfurt-1.oraclecloud.com left intact

Where the Auth Tokens come from

Auth Tokens can be created under your user object in the Oracle Cloud web console under Auth Tokens. The token shows up when you create it for you to copy and save. Once you close the box the token can never be seen again in the console. Make sure you save it in a safe place. At this time you're allowed 3 Auth tokens by default. If you require more you'll need to file a limit increase request

⚠️ **GitHub.com Fallback** ⚠️